Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
typeof(TRequest).Name, timeTaken.Seconds);

logger.LogInformation("[END] Handled {Request} with {Response}", typeof(TRequest).Name, typeof(TResponse).Name);

return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ public record DeleteBasketResult(bool IsSuccess);

public class DeleteBasketCommandValidator : AbstractValidator<DeleteBasketCommand>
{
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)
Expand Down
2 changes: 2 additions & 0 deletions src/Services/Basket/Basket.API/Data/BasketRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public async Task<ShoppingCart> StoreBasket(ShoppingCart basket, CancellationTok
{
session.Store(basket);
await session.SaveChangesAsync(cancellationToken);

return basket;
}

public async Task<bool> DeleteBasket(string userName, CancellationToken cancellationToken = default)
{
session.Delete<ShoppingCart>(userName);
await session.SaveChangesAsync(cancellationToken);

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public async Task<ShoppingCart> GetBasket(string userName, CancellationToken can

var basket = await repository.GetBasket(userName, cancellationToken);
await cache.SetStringAsync(userName, JsonSerializer.Serialize(basket), cancellationToken);

return basket;
}

Expand Down
5 changes: 1 addition & 4 deletions src/Services/Basket/Basket.API/Models/ShoppingCart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ public class ShoppingCart
public List<ShoppingCartItem> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ public record DeleteProductResult(bool IsSuccess);

public class DeleteProductCommandValidator : AbstractValidator<DeleteProductCommand>
{
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
Expand Down
7 changes: 1 addition & 6 deletions src/Services/Discount/Discount.Grpc/Data/DiscountContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@

namespace Discount.Grpc.Data;

public class DiscountContext : DbContext
public class DiscountContext(DbContextOptions<DiscountContext> options) : DbContext(options)
{
public DbSet<Coupon> Coupons { get; set; } = default!;

public DiscountContext(DbContextOptions<DiscountContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Coupon>().HasData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public override async Task<CouponModel> GetDiscount(GetDiscountRequest request,
logger.LogInformation("Discount is retrieved for ProductName : {productName}, Amount : {amount}", coupon.ProductName, coupon.Amount);

var couponModel = coupon.Adapt<CouponModel>();

return couponModel;
}

Expand All @@ -37,6 +38,7 @@ public override async Task<CouponModel> CreateDiscount(CreateDiscountRequest req
logger.LogInformation("Discount is successfully created. ProductName : {ProductName}", coupon.ProductName);

var couponModel = coupon.Adapt<CouponModel>();

return couponModel;
}

Expand All @@ -53,6 +55,7 @@ public override async Task<CouponModel> UpdateDiscount(UpdateDiscountRequest req
logger.LogInformation("Discount is successfully updated. ProductName : {ProductName}", coupon.ProductName);

var couponModel = coupon.Adapt<CouponModel>();

return couponModel;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace Ordering.Application.Extensions;
public static class OrderExtensions
{
public static IEnumerable<OrderDto> ToOrderDtoList(this IEnumerable<Order> orders)
{
return orders.Select(order => new OrderDto(
public static IEnumerable<OrderDto> ToOrderDtoList(this IEnumerable<Order> orders)=>
orders.Select(order => new OrderDto(
Id: order.Id.Value,
CustomerId: order.CustomerId.Value,
OrderName: order.OrderName.Value,
Expand All @@ -13,16 +12,11 @@ public static IEnumerable<OrderDto> ToOrderDtoList(this IEnumerable<Order> 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,
Expand All @@ -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()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private Order CreateNewOrder(OrderDto orderDto)
{
newOrder.Add(ProductId.Of(orderItemDto.ProductId), orderItemDto.Quantity, orderItemDto.Price);
}

return newOrder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ public record DeleteOrderResult(bool IsSuccess);

public class DeleteOrderCommandValidator : AbstractValidator<DeleteOrderCommand>
{
public DeleteOrderCommandValidator()
{
RuleFor(x => x.OrderId).NotEmpty().WithMessage("OrderId is required");
}
public DeleteOrderCommandValidator() => RuleFor(x => x.OrderId).NotEmpty().WithMessage("OrderId is required");
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ public abstract class Aggregate<TId> : Entity<TId>, IAggregate<TId>
private readonly List<IDomainEvent> _domainEvents = new();
public IReadOnlyList<IDomainEvent> DomainEvents => _domainEvents.AsReadOnly();

public void AddDomainEvent(IDomainEvent domainEvent)
{
_domainEvents.Add(domainEvent);
}
public void AddDomainEvent(IDomainEvent domainEvent) => _domainEvents.Add(domainEvent);

public IDomainEvent[] ClearDomainEvents()
{
Expand Down