From 68bdc8d728a1c96bfda17178693620fd02bc5f53 Mon Sep 17 00:00:00 2001 From: Khanbala Rashidov <50279392+KhanbalaRashidov@users.noreply.github.com> Date: Tue, 2 Jul 2024 17:54:41 +0400 Subject: [PATCH] Refactor code --- .../Behaviors/LoggingBehavior.cs | 1 + .../Basket/DeleteBasket/DeleteBasketHandler.cs | 5 +---- .../Basket/Basket.API/Data/BasketRepository.cs | 2 ++ .../Basket.API/Data/CachedBasketRepository.cs | 1 + .../Basket/Basket.API/Models/ShoppingCart.cs | 5 +---- .../DeleteProduct/DeleteProductHandler.cs | 5 +---- .../Discount.Grpc/Data/DiscountContext.cs | 7 +------ .../Discount.Grpc/Services/DiscountService.cs | 3 +++ .../Extensions/OrderExtensions.cs | 18 ++++++------------ .../Commands/CreateOrder/CreateOrderHandler.cs | 1 + .../Commands/DeleteOrder/DeleteOrderCommand.cs | 5 +---- .../Ordering.Domain/Abstractions/Aggregate.cs | 5 +---- 12 files changed, 20 insertions(+), 38 deletions(-) diff --git a/src/BuildingBlocks/BuildingBlocks/Behaviors/LoggingBehavior.cs b/src/BuildingBlocks/BuildingBlocks/Behaviors/LoggingBehavior.cs index 5650bce5..0ca31c0b 100644 --- a/src/BuildingBlocks/BuildingBlocks/Behaviors/LoggingBehavior.cs +++ b/src/BuildingBlocks/BuildingBlocks/Behaviors/LoggingBehavior.cs @@ -26,6 +26,7 @@ public async Task Handle(TRequest request, RequestHandlerDelegate { - public DeleteBasketCommandValidator() - { - RuleFor(x => x.UserName).NotEmpty().WithMessage("UserName is required"); - } + public DeleteBasketCommandValidator() => RuleFor(x => x.UserName).NotEmpty().WithMessage("UserName is required"); } public class DeleteBasketCommandHandler(IBasketRepository repository) diff --git a/src/Services/Basket/Basket.API/Data/BasketRepository.cs b/src/Services/Basket/Basket.API/Data/BasketRepository.cs index bcc81fc0..9b1d5cbf 100644 --- a/src/Services/Basket/Basket.API/Data/BasketRepository.cs +++ b/src/Services/Basket/Basket.API/Data/BasketRepository.cs @@ -14,6 +14,7 @@ public async Task StoreBasket(ShoppingCart basket, CancellationTok { session.Store(basket); await session.SaveChangesAsync(cancellationToken); + return basket; } @@ -21,6 +22,7 @@ public async Task DeleteBasket(string userName, CancellationToken cancella { session.Delete(userName); await session.SaveChangesAsync(cancellationToken); + return true; } } diff --git a/src/Services/Basket/Basket.API/Data/CachedBasketRepository.cs b/src/Services/Basket/Basket.API/Data/CachedBasketRepository.cs index ca57e811..9c723176 100644 --- a/src/Services/Basket/Basket.API/Data/CachedBasketRepository.cs +++ b/src/Services/Basket/Basket.API/Data/CachedBasketRepository.cs @@ -15,6 +15,7 @@ public async Task GetBasket(string userName, CancellationToken can var basket = await repository.GetBasket(userName, cancellationToken); await cache.SetStringAsync(userName, JsonSerializer.Serialize(basket), cancellationToken); + return basket; } diff --git a/src/Services/Basket/Basket.API/Models/ShoppingCart.cs b/src/Services/Basket/Basket.API/Models/ShoppingCart.cs index 870888a8..4218ab54 100644 --- a/src/Services/Basket/Basket.API/Models/ShoppingCart.cs +++ b/src/Services/Basket/Basket.API/Models/ShoppingCart.cs @@ -6,10 +6,7 @@ public class ShoppingCart public List Items { get; set; } = new(); public decimal TotalPrice => Items.Sum(x => x.Price * x.Quantity); - public ShoppingCart(string userName) - { - UserName = userName; - } + public ShoppingCart(string userName) => UserName = userName; //Required for Mapping public ShoppingCart() diff --git a/src/Services/Catalog/Catalog.API/Products/DeleteProduct/DeleteProductHandler.cs b/src/Services/Catalog/Catalog.API/Products/DeleteProduct/DeleteProductHandler.cs index 74cd2657..302a62c3 100644 --- a/src/Services/Catalog/Catalog.API/Products/DeleteProduct/DeleteProductHandler.cs +++ b/src/Services/Catalog/Catalog.API/Products/DeleteProduct/DeleteProductHandler.cs @@ -6,10 +6,7 @@ public record DeleteProductResult(bool IsSuccess); public class DeleteProductCommandValidator : AbstractValidator { - public DeleteProductCommandValidator() - { - RuleFor(x => x.Id).NotEmpty().WithMessage("Product ID is required"); - } + public DeleteProductCommandValidator() => RuleFor(x => x.Id).NotEmpty().WithMessage("Product ID is required"); } internal class DeleteProductCommandHandler diff --git a/src/Services/Discount/Discount.Grpc/Data/DiscountContext.cs b/src/Services/Discount/Discount.Grpc/Data/DiscountContext.cs index e05e9b76..ac630c13 100644 --- a/src/Services/Discount/Discount.Grpc/Data/DiscountContext.cs +++ b/src/Services/Discount/Discount.Grpc/Data/DiscountContext.cs @@ -3,15 +3,10 @@ namespace Discount.Grpc.Data; -public class DiscountContext : DbContext +public class DiscountContext(DbContextOptions options) : DbContext(options) { public DbSet Coupons { get; set; } = default!; - public DiscountContext(DbContextOptions options) - : base(options) - { - } - protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasData( diff --git a/src/Services/Discount/Discount.Grpc/Services/DiscountService.cs b/src/Services/Discount/Discount.Grpc/Services/DiscountService.cs index 48126f30..0a4e589c 100644 --- a/src/Services/Discount/Discount.Grpc/Services/DiscountService.cs +++ b/src/Services/Discount/Discount.Grpc/Services/DiscountService.cs @@ -22,6 +22,7 @@ public override async Task GetDiscount(GetDiscountRequest request, logger.LogInformation("Discount is retrieved for ProductName : {productName}, Amount : {amount}", coupon.ProductName, coupon.Amount); var couponModel = coupon.Adapt(); + return couponModel; } @@ -37,6 +38,7 @@ public override async Task CreateDiscount(CreateDiscountRequest req logger.LogInformation("Discount is successfully created. ProductName : {ProductName}", coupon.ProductName); var couponModel = coupon.Adapt(); + return couponModel; } @@ -53,6 +55,7 @@ public override async Task UpdateDiscount(UpdateDiscountRequest req logger.LogInformation("Discount is successfully updated. ProductName : {ProductName}", coupon.ProductName); var couponModel = coupon.Adapt(); + return couponModel; } diff --git a/src/Services/Ordering/Ordering.Application/Extensions/OrderExtensions.cs b/src/Services/Ordering/Ordering.Application/Extensions/OrderExtensions.cs index 1ed8f54b..84b6855a 100644 --- a/src/Services/Ordering/Ordering.Application/Extensions/OrderExtensions.cs +++ b/src/Services/Ordering/Ordering.Application/Extensions/OrderExtensions.cs @@ -1,9 +1,8 @@ namespace Ordering.Application.Extensions; public static class OrderExtensions { - public static IEnumerable ToOrderDtoList(this IEnumerable orders) - { - return orders.Select(order => new OrderDto( + public static IEnumerable ToOrderDtoList(this IEnumerable orders)=> + orders.Select(order => new OrderDto( Id: order.Id.Value, CustomerId: order.CustomerId.Value, OrderName: order.OrderName.Value, @@ -13,16 +12,11 @@ public static IEnumerable ToOrderDtoList(this IEnumerable order Status: order.Status, OrderItems: order.OrderItems.Select(oi => new OrderItemDto(oi.OrderId.Value, oi.ProductId.Value, oi.Quantity, oi.Price)).ToList() )); - } - public static OrderDto ToOrderDto(this Order order) - { - return DtoFromOrder(order); - } + public static OrderDto ToOrderDto(this Order order) => DtoFromOrder(order); - private static OrderDto DtoFromOrder(Order order) - { - return new OrderDto( + private static OrderDto DtoFromOrder(Order order)=> + new OrderDto( Id: order.Id.Value, CustomerId: order.CustomerId.Value, OrderName: order.OrderName.Value, @@ -32,5 +26,5 @@ private static OrderDto DtoFromOrder(Order order) Status: order.Status, OrderItems: order.OrderItems.Select(oi => new OrderItemDto(oi.OrderId.Value, oi.ProductId.Value, oi.Quantity, oi.Price)).ToList() ); - } + } diff --git a/src/Services/Ordering/Ordering.Application/Orders/Commands/CreateOrder/CreateOrderHandler.cs b/src/Services/Ordering/Ordering.Application/Orders/Commands/CreateOrder/CreateOrderHandler.cs index caed6eb4..a2fa8708 100644 --- a/src/Services/Ordering/Ordering.Application/Orders/Commands/CreateOrder/CreateOrderHandler.cs +++ b/src/Services/Ordering/Ordering.Application/Orders/Commands/CreateOrder/CreateOrderHandler.cs @@ -34,6 +34,7 @@ private Order CreateNewOrder(OrderDto orderDto) { newOrder.Add(ProductId.Of(orderItemDto.ProductId), orderItemDto.Quantity, orderItemDto.Price); } + return newOrder; } } diff --git a/src/Services/Ordering/Ordering.Application/Orders/Commands/DeleteOrder/DeleteOrderCommand.cs b/src/Services/Ordering/Ordering.Application/Orders/Commands/DeleteOrder/DeleteOrderCommand.cs index 12bf91f4..88dc4272 100644 --- a/src/Services/Ordering/Ordering.Application/Orders/Commands/DeleteOrder/DeleteOrderCommand.cs +++ b/src/Services/Ordering/Ordering.Application/Orders/Commands/DeleteOrder/DeleteOrderCommand.cs @@ -9,8 +9,5 @@ public record DeleteOrderResult(bool IsSuccess); public class DeleteOrderCommandValidator : AbstractValidator { - public DeleteOrderCommandValidator() - { - RuleFor(x => x.OrderId).NotEmpty().WithMessage("OrderId is required"); - } + public DeleteOrderCommandValidator() => RuleFor(x => x.OrderId).NotEmpty().WithMessage("OrderId is required"); } diff --git a/src/Services/Ordering/Ordering.Domain/Abstractions/Aggregate.cs b/src/Services/Ordering/Ordering.Domain/Abstractions/Aggregate.cs index 58ba0bbb..4a843736 100644 --- a/src/Services/Ordering/Ordering.Domain/Abstractions/Aggregate.cs +++ b/src/Services/Ordering/Ordering.Domain/Abstractions/Aggregate.cs @@ -5,10 +5,7 @@ public abstract class Aggregate : Entity, IAggregate private readonly List _domainEvents = new(); public IReadOnlyList DomainEvents => _domainEvents.AsReadOnly(); - public void AddDomainEvent(IDomainEvent domainEvent) - { - _domainEvents.Add(domainEvent); - } + public void AddDomainEvent(IDomainEvent domainEvent) => _domainEvents.Add(domainEvent); public IDomainEvent[] ClearDomainEvents() {