Skip to content

Commit

Permalink
Some modifications on ExceptionHandler class. Added UpdateAsync with …
Browse files Browse the repository at this point in the history
…Where expression overload . added update and delete endpoints for orders. Added IsDeleted flag on Order entity
  • Loading branch information
babaktaremi committed Nov 26, 2023
1 parent cf0d730 commit fa0d436
Show file tree
Hide file tree
Showing 15 changed files with 599 additions and 38 deletions.
4 changes: 2 additions & 2 deletions CleanArcTemplate.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<metadata>

<id>Bobby.CleanArcTemplate</id>
<version>5.0.1</version>
<version>5.0.2</version>
<title>Clean Architecture Solution Template</title>
<authors>BabakTaremi</authors>
<description>Clean Architecture Solution Template for and .NET 8. With many features available out of the box</description>
<summary>
Ready to develop template based on clean architecture principles. Supports ASP NET Core Identity integrated with JWE tokens, OTP authentication, stand alone plugin development, CQRS pattern using MediatR library and dynamic permission management system out of the box
</summary>
<releaseNotes>
Update to NET8, bug fixes, Package updates and added minimal API support with carter module
Some modifications on ExceptionHandler class. Added UpdateAsync with Where expression overload . added update and delete endpoints for orders. Added IsDeleted flag on Order entity
</releaseNotes>


Expand Down
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)));
}
6 changes: 3 additions & 3 deletions src/API/CleanArc.WebFramework/Middlewares/ExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class ExceptionHandler(ILogger<ExceptionHandler> logger,IWebHostEnvironme

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;
Expand Down Expand Up @@ -42,9 +45,6 @@ public async ValueTask<bool> TryHandleAsync(HttpContext context, Exception excep

context.Response.StatusCode = StatusCodes.Status500InternalServerError;

if (environment.IsDevelopment())
return false;

context.Response.ContentType = "application/problem+json";
var response = new ApiResult(false,
ApiResultStatusCode.ServerError, "Server Error");
Expand Down
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 fa0d436

Please sign in to comment.