diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index cff302e17..600b756fa 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -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 diff --git a/src/Application/Common/ExceptionHandlers/DbExceptionHandler.cs b/src/Application/Common/ExceptionHandlers/DbExceptionHandler.cs index 6a3b3ce2d..2918a73e8 100644 --- a/src/Application/Common/ExceptionHandlers/DbExceptionHandler.cs +++ b/src/Application/Common/ExceptionHandlers/DbExceptionHandler.cs @@ -4,7 +4,8 @@ namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers; public class DbExceptionHandler : IRequestExceptionHandler - where TRequest : IRequest + where TRequest : IRequest> + where TResponse : Result where TException : DbUpdateException { private readonly ILogger> _logger; @@ -17,14 +18,7 @@ public DbExceptionHandler(ILogger state, CancellationToken cancellationToken) { - TResponse response = Activator.CreateInstance(); - if (response is Result result) - { - result.Succeeded = false; - result.Errors = GetErrors(exception); - state.SetHandled(response); - } - + state.SetHandled((TResponse)Result.Failure(GetErrors(exception))); return Task.CompletedTask; } diff --git a/src/Application/Common/ExceptionHandlers/ServerExceptionHandler.cs b/src/Application/Common/ExceptionHandlers/ServerExceptionHandler.cs index 1066ba641..7bd1cd996 100644 --- a/src/Application/Common/ExceptionHandlers/ServerExceptionHandler.cs +++ b/src/Application/Common/ExceptionHandlers/ServerExceptionHandler.cs @@ -1,6 +1,7 @@ namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers; public class ServerExceptionHandler : IRequestExceptionHandler - where TRequest : IRequest + where TRequest : IRequest> + where TResponse: Result where TException : ServerException { private readonly ILogger> _logger; @@ -12,13 +13,7 @@ public ServerExceptionHandler(ILogger state, CancellationToken cancellationToken) { - var response = Activator.CreateInstance(); - if (response is Result result) - { - result.Succeeded = false; - result.Errors = new string[] { exception.Message }; - state.SetHandled(response); - } + state.SetHandled((TResponse)Result.Failure(new string[] { exception.Message })); return Task.CompletedTask; } diff --git a/src/Application/Common/ExceptionHandlers/ValidationExceptionHandler.cs b/src/Application/Common/ExceptionHandlers/ValidationExceptionHandler.cs index 219b62142..ab4d336ec 100644 --- a/src/Application/Common/ExceptionHandlers/ValidationExceptionHandler.cs +++ b/src/Application/Common/ExceptionHandlers/ValidationExceptionHandler.cs @@ -1,6 +1,7 @@ namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers; public class ValidationExceptionHandler : IRequestExceptionHandler - where TRequest : IRequest + where TRequest : IRequest> + where TResponse: Result where TException : ValidationException { private readonly ILogger> _logger; @@ -12,13 +13,7 @@ public ValidationExceptionHandler(ILogger state, CancellationToken cancellationToken) { - var response = Activator.CreateInstance(); - 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.Failure(exception.Errors.Select(x => x.ErrorMessage).Distinct().ToArray())); return Task.CompletedTask; } } diff --git a/src/Application/Common/Interfaces/IResult.cs b/src/Application/Common/Interfaces/IResult.cs index 31f9b0104..413097c8d 100644 --- a/src/Application/Common/Interfaces/IResult.cs +++ b/src/Application/Common/Interfaces/IResult.cs @@ -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 : IResult { diff --git a/src/Application/Common/Models/PaginatedList.cs b/src/Application/Common/Models/PaginatedList.cs index d977fd55c..a34846846 100644 --- a/src/Application/Common/Models/PaginatedList.cs +++ b/src/Application/Common/Models/PaginatedList.cs @@ -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> CreateAsync(IQueryable source, int pageNumber, int pageSize) { @@ -30,10 +30,10 @@ public static async Task> CreateAsync(IQueryable source, } public class PaginatedList : PaginatedList { - public List Items { get; } + public IReadOnlyCollection Items { get; } public PaginatedList( - List items, + IReadOnlyCollection items, int count, int pageNumber, int pageSize) : base(count, pageNumber, pageSize) => Items = items; @@ -47,25 +47,3 @@ public static async Task> CreateAsync(IQueryable source, int return new PaginatedList(items, count, pageIndex, pageSize); } } - -//public class PaginatedList -//{ -// public List Items { get; } -// public int PageIndex { get; } -// public int TotalPages { get; } -// public int TotalCount { get; } - -// public PaginatedList(List 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; - - -//} diff --git a/src/Application/Common/Models/Result.cs b/src/Application/Common/Models/Result.cs index 45b5e6e5b..eb6fd725c 100644 --- a/src/Application/Common/Models/Result.cs +++ b/src/Application/Common/Models/Result.cs @@ -15,9 +15,9 @@ internal Result(bool succeeded, IEnumerable 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[] { }); diff --git a/src/Blazor.Server.UI/Pages/Products/_ProductFormDialog.razor b/src/Blazor.Server.UI/Pages/Products/_ProductFormDialog.razor index 06af09bae..26c5becab 100644 --- a/src/Blazor.Server.UI/Pages/Products/_ProductFormDialog.razor +++ b/src/Blazor.Server.UI/Pages/Products/_ProductFormDialog.razor @@ -209,6 +209,7 @@ if (!_form!.IsValid) return; + var result = await Mediator.Send(Model); if (result.Succeeded) diff --git a/src/Infrastructure/Middlewares/ExceptionHandlingMiddleware.cs b/src/Infrastructure/Middlewares/ExceptionHandlingMiddleware.cs index 9ef6d08e8..edd174a3f 100644 --- a/src/Infrastructure/Middlewares/ExceptionHandlingMiddleware.cs +++ b/src/Infrastructure/Middlewares/ExceptionHandlingMiddleware.cs @@ -38,7 +38,7 @@ 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) { @@ -46,7 +46,7 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next) 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: