Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ jobs:

# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
# name: Autobuild
# uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
Expand Down
12 changes: 3 additions & 9 deletions src/Application/Common/ExceptionHandlers/DbExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;

public class
DbExceptionHandler<TRequest, TResponse, TException> : IRequestExceptionHandler<TRequest, TResponse, TException>
where TRequest : IRequest<Result>
where TRequest : IRequest<Result<int>>
where TResponse : Result<int>
where TException : DbUpdateException
{
private readonly ILogger<DbExceptionHandler<TRequest, TResponse, TException>> _logger;
Expand All @@ -17,14 +18,7 @@ public DbExceptionHandler(ILogger<DbExceptionHandler<TRequest, TResponse, TExcep
public Task Handle(TRequest request, TException exception, RequestExceptionHandlerState<TResponse> state,
CancellationToken cancellationToken)
{
TResponse response = Activator.CreateInstance<TResponse>();
if (response is Result result)
{
result.Succeeded = false;
result.Errors = GetErrors(exception);
state.SetHandled(response);
}

state.SetHandled((TResponse)Result<int>.Failure(GetErrors(exception)));
return Task.CompletedTask;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
public class ServerExceptionHandler<TRequest, TResponse, TException> : IRequestExceptionHandler<TRequest, TResponse, TException>
where TRequest : IRequest<Result>
where TRequest : IRequest<Result<int>>
where TResponse: Result<int>
where TException : ServerException
{
private readonly ILogger<ServerExceptionHandler<TRequest, TResponse, TException>> _logger;
Expand All @@ -12,13 +13,7 @@ public ServerExceptionHandler(ILogger<ServerExceptionHandler<TRequest, TResponse

public Task Handle(TRequest request, TException exception, RequestExceptionHandlerState<TResponse> state, CancellationToken cancellationToken)
{
var response = Activator.CreateInstance<TResponse>();
if (response is Result result)
{
result.Succeeded = false;
result.Errors = new string[] { exception.Message };
state.SetHandled(response);
}
state.SetHandled((TResponse)Result<int>.Failure(new string[] { exception.Message }));
return Task.CompletedTask;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
public class ValidationExceptionHandler<TRequest, TResponse, TException> : IRequestExceptionHandler<TRequest, TResponse, TException>
where TRequest : IRequest<Result>
where TRequest : IRequest<Result<int>>
where TResponse: Result<int>
where TException : ValidationException
{
private readonly ILogger<ValidationExceptionHandler<TRequest, TResponse, TException>> _logger;
Expand All @@ -12,13 +13,7 @@ public ValidationExceptionHandler(ILogger<ValidationExceptionHandler<TRequest, T

public Task Handle(TRequest request, TException exception, RequestExceptionHandlerState<TResponse> state, CancellationToken cancellationToken)
{
var response = Activator.CreateInstance<TResponse>();
if(response is Result result)
{
result.Succeeded = false;
result.Errors = exception.Errors.Select(x => x.ErrorMessage).Distinct().ToArray();
state.SetHandled(response);
}
state.SetHandled((TResponse)Result<int>.Failure(exception.Errors.Select(x => x.ErrorMessage).Distinct().ToArray()));
return Task.CompletedTask;
}
}
4 changes: 2 additions & 2 deletions src/Application/Common/Interfaces/IResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace CleanArchitecture.Blazor.Application.Common.Interfaces;

public interface IResult
{
string[] Errors { get; set; }
string[] Errors { get; init; }

bool Succeeded { get; set; }
bool Succeeded { get; init; }
}
public interface IResult<out T> : IResult
{
Expand Down
30 changes: 4 additions & 26 deletions src/Application/Common/Models/PaginatedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public PaginatedList(int count, int pageNumber, int pageSize)
public int TotalPages { get; }
public int TotalCount { get; }

public bool HasPreviousPage => this.PageNumber > 1;
public bool HasPreviousPage => PageNumber > 1;

public bool HasNextPage => this.PageNumber < this.TotalPages;
public bool HasNextPage => PageNumber < TotalPages;

public static async Task<PaginatedList<T>> CreateAsync<T>(IQueryable<T> source, int pageNumber, int pageSize)
{
Expand All @@ -30,10 +30,10 @@ public static async Task<PaginatedList<T>> CreateAsync<T>(IQueryable<T> source,
}
public class PaginatedList<T> : PaginatedList
{
public List<T> Items { get; }
public IReadOnlyCollection<T> Items { get; }

public PaginatedList(
List<T> items,
IReadOnlyCollection<T> items,
int count,
int pageNumber,
int pageSize) : base(count, pageNumber, pageSize) => Items = items;
Expand All @@ -47,25 +47,3 @@ public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}

//public class PaginatedList<T>
//{
// public List<T> Items { get; }
// public int PageIndex { get; }
// public int TotalPages { get; }
// public int TotalCount { get; }

// public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
// {
// PageIndex = pageIndex;
// TotalPages = (int)Math.Ceiling(count / (double)pageSize);
// TotalCount = count;
// Items = items;
// }

// public bool HasPreviousPage => PageIndex > 1;

// public bool HasNextPage => PageIndex < TotalPages;


//}
4 changes: 2 additions & 2 deletions src/Application/Common/Models/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ internal Result(bool succeeded, IEnumerable<string> errors)
Errors = errors.ToArray();
}

public bool Succeeded { get; set; }
public bool Succeeded { get; init; }

public string[] Errors { get; set; }
public string[] Errors { get; init; }

public string ErrorMessage => string.Join(", ", Errors??new string[] { });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@

if (!_form!.IsValid)
return;

var result = await Mediator.Send(Model);

if (result.Succeeded)
Expand Down
4 changes: 2 additions & 2 deletions src/Infrastructure/Middlewares/ExceptionHandlingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
}
if (!string.IsNullOrEmpty(exception.Message))
{
responseModel.Errors=new string[] { exception.Message };
responseModel= await Result.FailureAsync(new string[] { exception.Message });
}
switch (exception)
{
case ServerException e:
response.StatusCode = (int)e.StatusCode;
if (e.ErrorMessages is not null)
{
responseModel.Errors = e.ErrorMessages.ToArray();
responseModel = await Result.FailureAsync(e.ErrorMessages.ToArray());
}
break;
case KeyNotFoundException:
Expand Down