Skip to content

Commit

Permalink
Resolve Conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
babaktaremi committed Apr 2, 2024
2 parents 0265c47 + fa0d436 commit 91d6207
Show file tree
Hide file tree
Showing 17 changed files with 652 additions and 126 deletions.
14 changes: 14 additions & 0 deletions src/API/CleanArc.Web.Api/Controllers/V1/Order/OrderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,18 @@ public async Task<IActionResult> GetUserOrders()

return base.OperationResult(query);
}

[HttpPut("UpdateOrder")]
public async Task<IActionResult> UpdateOrder(UpdateUserOrderCommand model)
{
model.UserId=base.UserId;

var command = await sender.Send(model);

return base.OperationResult(command);
}

[HttpDelete("DeleteAllUserOrders")]
public async Task<IActionResult> DeleteAllUserOrders()
=> base.OperationResult(await sender.Send(new DeleteUserOrdersCommand(base.UserId)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ public async Task<IActionResult> CreateUser(UserCreateCommand model)
return base.OperationResult(command);
}

//[HttpPost("ConfirmPhoneNumber")]
//public async Task<IActionResult> ConfirmPhoneNumber(ConfirmUserPhoneNumberViewModel model)
//{
// var command = await _mediator.Send(new ConfirmPhoneNumberCommand(model.UserKey, model.Code));

// return OperationResult(command);
//}

[HttpPost("TokenRequest")]
public async Task<IActionResult> TokenRequest(UserTokenRequestQuery model)
Expand Down
5 changes: 2 additions & 3 deletions src/API/CleanArc.Web.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
.AddWebFrameworkServices();

builder.Services.RegisterValidatorsAsServices();
builder.Services.AddExceptionHandler<ExceptionHandler>();


#region Plugin Services Configuration
Expand All @@ -84,9 +85,7 @@
app.UseDeveloperExceptionPage();
}

app.UseCustomExceptionHandler();


app.UseExceptionHandler(_=>{});
app.UseSwaggerAndUI();

app.MapCarter();
Expand Down

This file was deleted.

56 changes: 56 additions & 0 deletions src/API/CleanArc.WebFramework/Middlewares/ExceptionHandler.cs
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public interface IOrderRepository
Task AddOrderAsync(Order order);
Task<List<Order>> GetAllUserOrdersAsync(int userId);
Task<List<Order>> GetAllOrdersWithRelatedUserAsync();
Task<Order> GetUserOrderByIdAndUserIdAsync(int userId,int orderId,bool trackEntity);
Task DeleteUserOrdersAsync(int userId);
}
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);
}
}
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>>;
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);
}
}
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;
}
};
1 change: 1 addition & 0 deletions src/Core/CleanArc.Domain/Entities/Order/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace CleanArc.Domain.Entities.Order;
public class Order:BaseEntity
{
public string OrderName { get; set; }
public bool IsDeleted { get; set; }

#region Navigation Properties

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ internal class OrderConfig:IEntityTypeConfiguration<Order>
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.HasOne(c => c.User).WithMany(c => c.Orders).HasForeignKey(c => c.UserId);

builder.HasQueryFilter(c => !c.IsDeleted);
}
}
Loading

0 comments on commit 91d6207

Please sign in to comment.