Skip to content

Commit

Permalink
[chore]: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-oberaj committed May 23, 2024
1 parent 8e322fa commit 9ee55f6
Show file tree
Hide file tree
Showing 140 changed files with 944 additions and 779 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/build_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
on:
push:
branches:
- master
- master
pull_request:
branches:
- '**'
- '**'

jobs:
build:
name: Restore, Build and Test
Expand All @@ -18,21 +18,21 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'

- name: Restore Packages
run: dotnet restore

- name: Build
run: dotnet build --configuration Release

- name: Test
run: dotnet test --configuration Release --no-build --verbosity normal --logger trx --collect:"XPlat Code Coverage" --settings coverlet.runsettings

- name: Combine Coverage Reports
uses: danielpalme/[email protected]
with:
Expand All @@ -43,7 +43,7 @@ jobs:
title: "Code Coverage"
tag: "${{ github.run_number }}_${{ github.run_id }}"
toolpath: "reportgeneratortool"

- name: Upload Combined Coverage XML
uses: actions/upload-artifact@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion StudioManager.API.Contracts/Common/NamedBaseDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ public class NamedBaseDto
{
public Guid Id { get; init; }
public string Name { get; init; } = null!;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace StudioManager.API.Contracts.EquipmentTypes;

public sealed record EquipmentTypeReadDto(Guid Id, string Name);
public sealed record EquipmentTypeReadDto(Guid Id, string Name);
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace StudioManager.API.Contracts.EquipmentTypes;

public sealed record EquipmentTypeWriteDto(string Name);
public sealed record EquipmentTypeWriteDto(string Name);
8 changes: 7 additions & 1 deletion StudioManager.API.Contracts/Equipments/EquipmentReadDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

namespace StudioManager.API.Contracts.Equipments;

public sealed record EquipmentReadDto(Guid Id, string Name, int Quantity, int InitialQuantity, string ImageUrl, EquipmentTypeReadDto EquipmentType);
public sealed record EquipmentReadDto(
Guid Id,
string Name,
int Quantity,
int InitialQuantity,
string ImageUrl,
EquipmentTypeReadDto EquipmentType);
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace StudioManager.API.Contracts.Equipments;

public sealed record EquipmentWriteDto(string Name, Guid EquipmentTypeId, int Quantity/*, byte[] Image*/);
public sealed record EquipmentWriteDto(string Name, Guid EquipmentTypeId, int Quantity /*, string ImageUrl*/);
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public sealed class PaginationDetailsDto
public int Limit { get; set; }
public int Page { get; set; }
public int Total { get; set; }
}
}
11 changes: 4 additions & 7 deletions StudioManager.API.Contracts/Pagination/PaginationDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public sealed class PaginationDto
{
public const int DefaultLimit = 50;
public const int DefaultPage = 1;

private int? _limit;
private int? _page;

Expand All @@ -26,11 +26,8 @@ public int? Page

public int GetOffset()
{
if (!Page.HasValue)
{
return 0;
}

if (!Page.HasValue) return 0;

return Limit!.Value * (Page!.Value - 1);
}
}
}
2 changes: 1 addition & 1 deletion StudioManager.API.Contracts/Pagination/PagingResultDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ public sealed class PagingResultDto<TData>
{
public List<TData> Data { get; set; } = default!;
public PaginationDetailsDto Pagination { get; set; } = default!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public sealed record ReservationReadDto(
DateOnly StartDate,
DateOnly EndDate,
int Quantity,
NamedBaseDto Equipment);
NamedBaseDto Equipment);
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace StudioManager.API.Contracts.Reservations;

public sealed record ReservationWriteDto(DateOnly StartDate, DateOnly EndDate, int Quantity, Guid EquipmentId);
public sealed record ReservationWriteDto(DateOnly StartDate, DateOnly EndDate, int Quantity, Guid EquipmentId);
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,11 @@ private static async Task ReturnReservationsAsync(DbContextBase dbContext, Cance

var reservations = await dbContext.GetReservationsAsync(filter, cancellationToken);

if (reservations.Count == 0)
{
return;
}

if (reservations.Count == 0) return;

foreach (var reservation in reservations)
{
reservation.AddDomainEvent(new EquipmentReturnedEvent(reservation.EquipmentId, 0));
}

await dbContext.SaveChangesAsync(cancellationToken);
}
}
}
36 changes: 12 additions & 24 deletions StudioManager.API/Base/CoreController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace StudioManager.API.Base;
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[Produces(contentType: "application/json", "application/problem+json")]
[Produces("application/json", "application/problem+json")]
[ExcludeFromCodeCoverage]
//[Authorize(Policy = "AuthorizedUser")]
public abstract class CoreController(ISender sender) : ControllerBase
Expand All @@ -22,7 +22,7 @@ internal async Task<IResult> SendAsync(IRequest<CommandResult> request)

return CreateResult(result);
}

internal async Task<IResult> SendAsync<T>(IRequest<QueryResult<T>> request)
{
var result = await sender.Send(request);
Expand All @@ -40,50 +40,38 @@ private static IResult CreateResult<T>(IRequestResult<T> requestResult)
_ => FromUnexpectedErrorResult(requestResult)
};
}

private static IResult FromSucceededResult<T>(IRequestResult<T> requestResult)
{
if (!requestResult.Succeeded)
{
throw new InvalidOperationException(EX.SUCCESS_FROM_ERROR);
}

if (!requestResult.Succeeded) throw new InvalidOperationException(EX.SUCCESS_FROM_ERROR);

return Results.Ok(requestResult.Data);
}

private static IResult FromNotFoundResult<T>(IRequestResult<T> requestResult)
{
if (requestResult.Succeeded)
{
throw new InvalidOperationException(EX.ERROR_FROM_SUCCESS);
}
if (requestResult.Succeeded) throw new InvalidOperationException(EX.ERROR_FROM_SUCCESS);

return Results.Problem(
statusCode: StatusCodes.Status404NotFound,
detail: requestResult.Error);
}

private static IResult FromConflictResult<T>(IRequestResult<T> requestResult)
{
if (requestResult.Succeeded)
{
throw new InvalidOperationException(EX.ERROR_FROM_SUCCESS);
}
if (requestResult.Succeeded) throw new InvalidOperationException(EX.ERROR_FROM_SUCCESS);

return Results.Problem(
statusCode: StatusCodes.Status409Conflict,
detail: requestResult.Error);
}

private static IResult FromUnexpectedErrorResult<T>(IRequestResult<T> requestResult)
{
if (requestResult.Succeeded)
{
throw new InvalidOperationException(EX.ERROR_FROM_SUCCESS);
}
if (requestResult.Succeeded) throw new InvalidOperationException(EX.ERROR_FROM_SUCCESS);

return Results.Problem(
statusCode: StatusCodes.Status500InternalServerError,
detail: requestResult.Error);
}
}
}
5 changes: 3 additions & 2 deletions StudioManager.API/Behaviours/RequestLoggingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class RequestLoggingBehavior<TRequest, TResponse>(
: IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
Expand All @@ -39,4 +40,4 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
typeof(TRequest).Name, elapsed, DateTime.UtcNow);
}
}
}
}
17 changes: 6 additions & 11 deletions StudioManager.API/Behaviours/ValidationBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ public sealed class ValidationBehavior<TRequest, TResponse>(
: IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken)
{
if (!validators.Any())
{
return await next();
}

if (!validators.Any()) return await next();

var context = new ValidationContext<TRequest>(request);

var validationFailures = await Task.WhenAll(
Expand All @@ -31,11 +29,8 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
validationFailure.ErrorMessage))
.ToList();

if (errors.Count != 0)
{
throw new ValidationException(errors);
}
if (errors.Count != 0) throw new ValidationException(errors);

return await next();
}
}
}
20 changes: 7 additions & 13 deletions StudioManager.API/Common/GlobalExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public async ValueTask<bool> TryHandleAsync(
CancellationToken cancellationToken)
{
if (exception is ValidationException validationException)
{
return await HandleValidationExceptionAsync(httpContext, validationException, cancellationToken);
}

logger.LogError(exception, "[ERROR]: Error occurred while handling request {@Request}",exception.TargetSite?.DeclaringType?.FullName);

logger.LogError(exception, "[ERROR]: Error occurred while handling request {@Request}",
exception.TargetSite?.DeclaringType?.FullName);

var problemDetails = new ProblemDetails
{
Expand All @@ -34,8 +33,9 @@ public async ValueTask<bool> TryHandleAsync(
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
return true;
}

private static async Task<bool> HandleValidationExceptionAsync(HttpContext httpContext, ValidationException exception, CancellationToken cancellationToken)

private static async Task<bool> HandleValidationExceptionAsync(HttpContext httpContext,
ValidationException exception, CancellationToken cancellationToken)
{
var problemDetails = new ValidationProblemDetails(GroupValidationErrors(exception.Errors))
{
Expand All @@ -56,17 +56,11 @@ private static Dictionary<string, string[]> GroupValidationErrors(IEnumerable<Va
var final = new Dictionary<string, string[]>();

foreach (var error in validationErrors)
{
if (final.TryGetValue(error.PropertyName, out var value))
{
final[error.PropertyName] = value.Append(error.ErrorMessage).ToArray();
}
else
{
final[error.PropertyName] = [error.ErrorMessage];
}
}

return final;
}
}
}
11 changes: 3 additions & 8 deletions StudioManager.API/Common/NamedSwaggerGenOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ public void Configure(SwaggerGenOptions options)
{
// add swagger document for every API version discovered
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(
description.GroupName,
CreateVersionInfo(description));
}
}

public void Configure(string? name, SwaggerGenOptions options)
{
Configure(options);
}

private static OpenApiInfo CreateVersionInfo(
ApiVersionDescription description)
{
Expand All @@ -35,10 +33,7 @@ private static OpenApiInfo CreateVersionInfo(
Title = "Studio Manager API " + description.GroupName,
Version = description.ApiVersion.ToString()
};
if (description.IsDeprecated)
{
info.Description += " This API version has been deprecated.";
}
if (description.IsDeprecated) info.Description += " This API version has been deprecated.";
return info;
}
}
}
Loading

0 comments on commit 9ee55f6

Please sign in to comment.