-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
652 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 0 additions & 83 deletions
83
src/API/CleanArc.WebFramework/Middlewares/CustomExceptionHandlerMiddleware.cs
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
src/API/CleanArc.WebFramework/Middlewares/ExceptionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using CleanArc.Application.Models.ApiResult; | ||
using FluentValidation; | ||
using Microsoft.AspNetCore.Diagnostics; | ||
using Microsoft.AspNetCore.Http; | ||
using System.ComponentModel.DataAnnotations; | ||
using CleanArc.SharedKernel.Extensions; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace CleanArc.WebFramework.Middlewares; | ||
|
||
public class ExceptionHandler(ILogger<ExceptionHandler> logger,IWebHostEnvironment environment) : IExceptionHandler | ||
{ | ||
|
||
public async ValueTask<bool> TryHandleAsync(HttpContext context, Exception exception, CancellationToken cancellationToken) | ||
{ | ||
if (environment.IsDevelopment()) | ||
return false; | ||
|
||
if (exception is FluentValidation.ValidationException validationException) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status422UnprocessableEntity; | ||
|
||
var errors = new Dictionary<string, List<string>>(); | ||
|
||
foreach (var validationExceptionError in validationException.Errors) | ||
{ | ||
if (!errors.ContainsKey(validationExceptionError.PropertyName)) | ||
errors.Add(validationExceptionError.PropertyName, new List<string>() { validationExceptionError.ErrorMessage }); | ||
else | ||
errors[validationExceptionError.PropertyName].Add(validationExceptionError.ErrorMessage); | ||
|
||
} | ||
|
||
var apiResult = new ApiResult<IDictionary<string, List<string>>>(false, ApiResultStatusCode.EntityProcessError, errors, ApiResultStatusCode.EntityProcessError.ToDisplay()); | ||
|
||
context.Response.ContentType = "application/problem+json"; | ||
await context.Response.WriteAsJsonAsync(apiResult, cancellationToken: cancellationToken); | ||
|
||
return true; | ||
} | ||
|
||
logger.LogError(exception,"Error captured in global exception handler"); | ||
|
||
context.Response.StatusCode = StatusCodes.Status500InternalServerError; | ||
|
||
context.Response.ContentType = "application/problem+json"; | ||
var response = new ApiResult(false, | ||
ApiResultStatusCode.ServerError, "Server Error"); | ||
context.Response.StatusCode = StatusCodes.Status500InternalServerError; | ||
await context.Response.WriteAsJsonAsync(response, cancellationToken: cancellationToken); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Core/CleanArc.Application/Features/Order/Commands/DeleteUserOrdersCommand.Handler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using CleanArc.Application.Contracts.Persistence; | ||
using CleanArc.Application.Models.Common; | ||
using Mediator; | ||
|
||
namespace CleanArc.Application.Features.Order.Commands; | ||
|
||
public class DeleteUserOrdersCommandHandler(IUnitOfWork unitOfWork) : IRequestHandler<DeleteUserOrdersCommand,OperationResult<bool>> | ||
{ | ||
public async ValueTask<OperationResult<bool>> Handle(DeleteUserOrdersCommand request, CancellationToken cancellationToken) | ||
{ | ||
await unitOfWork.OrderRepository.DeleteUserOrdersAsync(request.UserId); | ||
|
||
return OperationResult<bool>.SuccessResult(true); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/Core/CleanArc.Application/Features/Order/Commands/DeleteUserOrdersCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using CleanArc.Application.Models.Common; | ||
using Mediator; | ||
|
||
namespace CleanArc.Application.Features.Order.Commands; | ||
|
||
public record DeleteUserOrdersCommand(int UserId):IRequest<OperationResult<bool>>; |
25 changes: 25 additions & 0 deletions
25
src/Core/CleanArc.Application/Features/Order/Commands/UpdateUserOrderCommand.Handler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using CleanArc.Application.Contracts.Persistence; | ||
using CleanArc.Application.Models.Common; | ||
using Mediator; | ||
|
||
namespace CleanArc.Application.Features.Order.Commands; | ||
|
||
public class UpdateUserOrderCommandHandler(IUnitOfWork unitOfWork) : IRequestHandler<UpdateUserOrderCommand,OperationResult<bool>> | ||
{ | ||
|
||
|
||
public async ValueTask<OperationResult<bool>> Handle(UpdateUserOrderCommand request, CancellationToken cancellationToken) | ||
{ | ||
var order = await unitOfWork.OrderRepository.GetUserOrderByIdAndUserIdAsync(request.UserId, request.OrderId, | ||
true); | ||
|
||
if(order is null) | ||
return OperationResult<bool>.NotFoundResult("Specified Order not found"); | ||
|
||
order.OrderName=request.OrderName; | ||
|
||
await unitOfWork.CommitAsync(); | ||
|
||
return OperationResult<bool>.SuccessResult(true); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Core/CleanArc.Application/Features/Order/Commands/UpdateUserOrderCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Text.Json.Serialization; | ||
using CleanArc.Application.Models.Common; | ||
using CleanArc.SharedKernel.ValidationBase; | ||
using CleanArc.SharedKernel.ValidationBase.Contracts; | ||
using FluentValidation; | ||
using Mediator; | ||
|
||
namespace CleanArc.Application.Features.Order.Commands; | ||
|
||
public record UpdateUserOrderCommand | ||
(int OrderId, string OrderName) : IRequest<OperationResult<bool>>,IValidatableModel<UpdateUserOrderCommand> | ||
{ | ||
[JsonIgnore] | ||
public int UserId { get; set; } | ||
|
||
public IValidator<UpdateUserOrderCommand> ValidateApplicationModel(ApplicationBaseValidationModelProvider<UpdateUserOrderCommand> validator) | ||
{ | ||
validator.RuleFor(c => c.OrderId).NotEmpty().GreaterThan(0); | ||
validator.RuleFor(c => c.OrderName).NotEmpty().NotNull(); | ||
|
||
return validator; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.