Cleanup in handlers
This commit is contained in:
@@ -3,7 +3,6 @@ using Imprink.Application.Dtos;
|
||||
using Imprink.Application.Services;
|
||||
using Imprink.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Imprink.Application.Commands.Addresses;
|
||||
|
||||
@@ -31,21 +30,23 @@ public class CreateAddressCommand : IRequest<AddressDto>
|
||||
public class CreateAddressHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper,
|
||||
ICurrentUserService userService,
|
||||
ILogger<CreateAddressHandler> logger)
|
||||
ICurrentUserService userService)
|
||||
: IRequestHandler<CreateAddressCommand, AddressDto>
|
||||
{
|
||||
public async Task<AddressDto> Handle(CreateAddressCommand request, CancellationToken cancellationToken)
|
||||
public async Task<AddressDto> Handle(
|
||||
CreateAddressCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return await uw.TransactAsync(async () =>
|
||||
{
|
||||
var address = mapper.Map<Address>(request);
|
||||
|
||||
address.UserId = userService.GetCurrentUserId()!;
|
||||
address.UserId = userService.GetCurrentUserId();
|
||||
|
||||
if (address.IsDefault)
|
||||
{
|
||||
var currentDefault = await uw.AddressRepository.GetDefaultByUserIdAsync(address.UserId, cancellationToken);
|
||||
var currentDefault = await uw.AddressRepository
|
||||
.GetDefaultByUserIdAsync(address.UserId, cancellationToken);
|
||||
|
||||
if (currentDefault != null)
|
||||
{
|
||||
currentDefault.IsDefault = false;
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
using AutoMapper;
|
||||
using Imprink.Application.Dtos;
|
||||
using Imprink.Domain.Entities;
|
||||
using MediatR;
|
||||
|
||||
namespace Imprink.Application.Commands.Addresses;
|
||||
|
||||
public class GetAddressByIdQuery : IRequest<AddressDto?>
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string? UserId { get; set; }
|
||||
}
|
||||
|
||||
public class GetAddressByIdHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler<GetAddressByIdQuery, AddressDto?>
|
||||
{
|
||||
public async Task<AddressDto?> Handle(GetAddressByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
Address? address;
|
||||
|
||||
if (!string.IsNullOrEmpty(request.UserId))
|
||||
{
|
||||
address = await uw.AddressRepository.GetByIdAndUserIdAsync(request.Id, request.UserId, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
address = await uw.AddressRepository.GetByIdAsync(request.Id, cancellationToken);
|
||||
}
|
||||
|
||||
return address != null ? mapper.Map<AddressDto>(address) : null;
|
||||
}
|
||||
}
|
||||
@@ -8,23 +8,30 @@ namespace Imprink.Application.Commands.Addresses;
|
||||
public class GetAddressesByUserIdQuery : IRequest<IEnumerable<AddressDto>>
|
||||
{
|
||||
public string UserId { get; set; } = null!;
|
||||
public bool ActiveOnly { get; set; } = false;
|
||||
public bool ActiveOnly { get; set; }
|
||||
public string? AddressType { get; set; }
|
||||
}
|
||||
|
||||
public class GetAddressesByUserIdHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler<GetAddressesByUserIdQuery, IEnumerable<AddressDto>>
|
||||
public class GetAddressesByUserIdHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<GetAddressesByUserIdQuery, IEnumerable<AddressDto>>
|
||||
{
|
||||
public async Task<IEnumerable<AddressDto>> Handle(GetAddressesByUserIdQuery request, CancellationToken cancellationToken)
|
||||
public async Task<IEnumerable<AddressDto>> Handle(
|
||||
GetAddressesByUserIdQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
IEnumerable<Address> addresses;
|
||||
|
||||
if (!string.IsNullOrEmpty(request.AddressType))
|
||||
{
|
||||
addresses = await uw.AddressRepository.GetByUserIdAndTypeAsync(request.UserId, request.AddressType, cancellationToken);
|
||||
addresses = await uw.AddressRepository
|
||||
.GetByUserIdAndTypeAsync(request.UserId, request.AddressType, cancellationToken);
|
||||
}
|
||||
else if (request.ActiveOnly)
|
||||
{
|
||||
addresses = await uw.AddressRepository.GetActiveByUserIdAsync(request.UserId, cancellationToken);
|
||||
addresses = await uw.AddressRepository
|
||||
.GetActiveByUserIdAsync(request.UserId, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
using AutoMapper;
|
||||
using Imprink.Application.Dtos;
|
||||
using Imprink.Application.Services;
|
||||
using Imprink.Domain.Entities;
|
||||
using MediatR;
|
||||
|
||||
namespace Imprink.Application.Commands.Addresses;
|
||||
|
||||
public class GetMyAddressesQuery : IRequest<IEnumerable<AddressDto?>>;
|
||||
|
||||
public class GetMyAddressesHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper,
|
||||
ICurrentUserService userService)
|
||||
: IRequestHandler<GetMyAddressesQuery, IEnumerable<AddressDto?>>
|
||||
{
|
||||
public async Task<IEnumerable<AddressDto?>> Handle(
|
||||
GetMyAddressesQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
IEnumerable<Address?> addresses = await uw.AddressRepository
|
||||
.GetByUserIdAsync(userService.GetCurrentUserId(), cancellationToken);
|
||||
|
||||
return mapper.Map<IEnumerable<AddressDto>>(addresses);
|
||||
}
|
||||
}
|
||||
@@ -14,9 +14,13 @@ public class CreateCategoryCommand : IRequest<CategoryDto>
|
||||
public Guid? ParentCategoryId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateCategoryHandler(IUnitOfWork unitOfWork) : IRequestHandler<CreateCategoryCommand, CategoryDto>
|
||||
public class CreateCategoryHandler(
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<CreateCategoryCommand, CategoryDto>
|
||||
{
|
||||
public async Task<CategoryDto> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
|
||||
public async Task<CategoryDto> Handle(
|
||||
CreateCategoryCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await unitOfWork.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
@@ -32,7 +36,9 @@ public class CreateCategoryHandler(IUnitOfWork unitOfWork) : IRequestHandler<Cre
|
||||
ParentCategoryId = request.ParentCategoryId
|
||||
};
|
||||
|
||||
var createdCategory = await unitOfWork.CategoryRepository.AddAsync(category, cancellationToken);
|
||||
var createdCategory = await unitOfWork
|
||||
.CategoryRepository.AddAsync(category, cancellationToken);
|
||||
|
||||
await unitOfWork.SaveAsync(cancellationToken);
|
||||
await unitOfWork.CommitTransactionAsync(cancellationToken);
|
||||
|
||||
|
||||
@@ -7,15 +7,21 @@ public class DeleteCategoryCommand : IRequest<bool>
|
||||
public Guid Id { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteCategoryHandler(IUnitOfWork unitOfWork) : IRequestHandler<DeleteCategoryCommand, bool>
|
||||
public class DeleteCategoryHandler(
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<DeleteCategoryCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(DeleteCategoryCommand request, CancellationToken cancellationToken)
|
||||
public async Task<bool> Handle(
|
||||
DeleteCategoryCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await unitOfWork.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var exists = await unitOfWork.CategoryRepository.ExistsAsync(request.Id, cancellationToken);
|
||||
var exists = await unitOfWork.CategoryRepository
|
||||
.ExistsAsync(request.Id, cancellationToken);
|
||||
|
||||
if (!exists)
|
||||
{
|
||||
await unitOfWork.RollbackTransactionAsync(cancellationToken);
|
||||
|
||||
@@ -10,10 +10,13 @@ public class GetCategoriesQuery : IRequest<IEnumerable<CategoryDto>>
|
||||
public bool RootCategoriesOnly { get; set; } = false;
|
||||
}
|
||||
|
||||
public class GetCategoriesHandler(IUnitOfWork unitOfWork)
|
||||
public class GetCategoriesHandler(
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<GetCategoriesQuery, IEnumerable<CategoryDto>>
|
||||
{
|
||||
public async Task<IEnumerable<CategoryDto>> Handle(GetCategoriesQuery request, CancellationToken cancellationToken)
|
||||
public async Task<IEnumerable<CategoryDto>> Handle(
|
||||
GetCategoriesQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
IEnumerable<Category> categories;
|
||||
|
||||
|
||||
@@ -15,15 +15,20 @@ public class UpdateCategoryCommand : IRequest<CategoryDto>
|
||||
public Guid? ParentCategoryId { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateCategoryHandler(IUnitOfWork unitOfWork) : IRequestHandler<UpdateCategoryCommand, CategoryDto>
|
||||
public class UpdateCategoryHandler(
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<UpdateCategoryCommand, CategoryDto>
|
||||
{
|
||||
public async Task<CategoryDto> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken)
|
||||
public async Task<CategoryDto> Handle(
|
||||
UpdateCategoryCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await unitOfWork.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var existingCategory = await unitOfWork.CategoryRepository.GetByIdAsync(request.Id, cancellationToken);
|
||||
var existingCategory = await unitOfWork.CategoryRepository
|
||||
.GetByIdAsync(request.Id, cancellationToken);
|
||||
|
||||
if (existingCategory == null)
|
||||
{
|
||||
@@ -37,7 +42,9 @@ public class UpdateCategoryHandler(IUnitOfWork unitOfWork) : IRequestHandler<Upd
|
||||
existingCategory.IsActive = request.IsActive;
|
||||
existingCategory.ParentCategoryId = request.ParentCategoryId;
|
||||
|
||||
var updatedCategory = await unitOfWork.CategoryRepository.UpdateAsync(existingCategory, cancellationToken);
|
||||
var updatedCategory = await unitOfWork.CategoryRepository
|
||||
.UpdateAsync(existingCategory, cancellationToken);
|
||||
|
||||
await unitOfWork.CommitTransactionAsync(cancellationToken);
|
||||
|
||||
return new CategoryDto
|
||||
|
||||
@@ -22,25 +22,35 @@ public class CreateOrderCommand : IRequest<OrderDto>
|
||||
public Guid AddressId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateOrderHandler(IUnitOfWork uw, IMapper mapper, ICurrentUserService userService) : IRequestHandler<CreateOrderCommand, OrderDto>
|
||||
public class CreateOrderHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper,
|
||||
ICurrentUserService userService)
|
||||
: IRequestHandler<CreateOrderCommand, OrderDto>
|
||||
{
|
||||
public async Task<OrderDto> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
|
||||
public async Task<OrderDto> Handle(
|
||||
CreateOrderCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return await uw.TransactAsync(async () =>
|
||||
{
|
||||
var sourceAddress = await uw.AddressRepository.GetByIdAndUserIdAsync(request.AddressId, userService.GetCurrentUserId()!, cancellationToken);
|
||||
var userId = userService.GetCurrentUserId()!;
|
||||
|
||||
var sourceAddress = await uw.AddressRepository
|
||||
.GetByIdAndUserIdAsync(request.AddressId, userId, cancellationToken);
|
||||
|
||||
if (sourceAddress == null)
|
||||
{
|
||||
throw new NotFoundException($"Address with ID {request.AddressId} not found for user {userService.GetCurrentUserId()!}");
|
||||
}
|
||||
throw new NotFoundException($"Address {request.AddressId} not found for {userId}");
|
||||
|
||||
var order = mapper.Map<Order>(request);
|
||||
order.UserId = userService.GetCurrentUserId()!;
|
||||
order.UserId = userService.GetCurrentUserId();
|
||||
order.OrderDate = DateTime.UtcNow;
|
||||
order.OrderStatusId = 0;
|
||||
order.ShippingStatusId = 0;
|
||||
|
||||
var variant = uw.ProductVariantRepository.GetByIdAsync(request.ProductVariantId, cancellationToken).Result;
|
||||
var variant = uw.ProductVariantRepository
|
||||
.GetByIdAsync(request.ProductVariantId, cancellationToken).Result;
|
||||
|
||||
if (variant == null)
|
||||
throw new NotFoundException("Product variant not found");
|
||||
|
||||
@@ -72,12 +82,14 @@ public class CreateOrderHandler(IUnitOfWork uw, IMapper mapper, ICurrentUserServ
|
||||
|
||||
await uw.OrderAddressRepository.AddAsync(orderAddress, cancellationToken);
|
||||
|
||||
createdOrder.Product = (await uw.ProductRepository.GetByIdAsync(createdOrder.ProductId, cancellationToken))!;
|
||||
createdOrder.Product = (await uw.ProductRepository
|
||||
.GetByIdAsync(createdOrder.ProductId, cancellationToken))!;
|
||||
|
||||
if (!createdOrder.ProductVariantId.HasValue)
|
||||
throw new NotFoundException("Product variant not found");
|
||||
|
||||
createdOrder.ProductVariant = await uw.ProductVariantRepository.GetByIdAsync(createdOrder.ProductVariantId.Value, cancellationToken);
|
||||
createdOrder.ProductVariant = await uw.ProductVariantRepository
|
||||
.GetByIdAsync(createdOrder.ProductVariantId.Value, cancellationToken);
|
||||
|
||||
await uw.SaveAsync(cancellationToken);
|
||||
|
||||
|
||||
@@ -11,9 +11,14 @@ public class GetOrderByIdQuery : IRequest<OrderDto?>
|
||||
public bool IncludeDetails { get; set; }
|
||||
}
|
||||
|
||||
public class GetOrderByIdHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler<GetOrderByIdQuery, OrderDto?>
|
||||
public class GetOrderByIdHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<GetOrderByIdQuery, OrderDto?>
|
||||
{
|
||||
public async Task<OrderDto?> Handle(GetOrderByIdQuery request, CancellationToken cancellationToken)
|
||||
public async Task<OrderDto?> Handle(
|
||||
GetOrderByIdQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
Order? order;
|
||||
|
||||
|
||||
@@ -11,19 +11,26 @@ public class GetOrdersByMerchantIdQuery : IRequest<IEnumerable<OrderDto>>
|
||||
public bool IncludeDetails { get; set; }
|
||||
}
|
||||
|
||||
public class GetOrdersByMerchantIdHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler<GetOrdersByMerchantIdQuery, IEnumerable<OrderDto>>
|
||||
public class GetOrdersByMerchantIdHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<GetOrdersByMerchantIdQuery, IEnumerable<OrderDto>>
|
||||
{
|
||||
public async Task<IEnumerable<OrderDto>> Handle(GetOrdersByMerchantIdQuery request, CancellationToken cancellationToken)
|
||||
public async Task<IEnumerable<OrderDto>> Handle(
|
||||
GetOrdersByMerchantIdQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
IEnumerable<Order> orders;
|
||||
|
||||
if (request.IncludeDetails)
|
||||
{
|
||||
orders = await uw.OrderRepository.GetByMerchantIdWithDetailsAsync(request.MerchantId, cancellationToken);
|
||||
orders = await uw.OrderRepository
|
||||
.GetByMerchantIdWithDetailsAsync(request.MerchantId, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
orders = await uw.OrderRepository.GetByMerchantIdAsync(request.MerchantId, cancellationToken);
|
||||
orders = await uw.OrderRepository
|
||||
.GetByMerchantIdAsync(request.MerchantId, cancellationToken);
|
||||
}
|
||||
|
||||
return mapper.Map<IEnumerable<OrderDto>>(orders);
|
||||
|
||||
@@ -11,19 +11,26 @@ public class GetOrdersByUserIdQuery : IRequest<IEnumerable<OrderDto>>
|
||||
public bool IncludeDetails { get; set; }
|
||||
}
|
||||
|
||||
public class GetOrdersByUserIdHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler<GetOrdersByUserIdQuery, IEnumerable<OrderDto>>
|
||||
public class GetOrdersByUserIdHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<GetOrdersByUserIdQuery, IEnumerable<OrderDto>>
|
||||
{
|
||||
public async Task<IEnumerable<OrderDto>> Handle(GetOrdersByUserIdQuery request, CancellationToken cancellationToken)
|
||||
public async Task<IEnumerable<OrderDto>> Handle(
|
||||
GetOrdersByUserIdQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
IEnumerable<Order> orders;
|
||||
|
||||
if (request.IncludeDetails)
|
||||
{
|
||||
orders = await uw.OrderRepository.GetByUserIdWithDetailsAsync(request.UserId, cancellationToken);
|
||||
orders = await uw.OrderRepository
|
||||
.GetByUserIdWithDetailsAsync(request.UserId, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
orders = await uw.OrderRepository.GetByUserIdAsync(request.UserId, cancellationToken);
|
||||
orders = await uw.OrderRepository
|
||||
.GetByUserIdAsync(request.UserId, cancellationToken);
|
||||
}
|
||||
|
||||
return mapper.Map<IEnumerable<OrderDto>>(orders);
|
||||
|
||||
@@ -17,10 +17,14 @@ public class CreateProductVariantCommand : IRequest<ProductVariantDto>
|
||||
public bool IsActive { get; set; } = true;
|
||||
}
|
||||
|
||||
public class CreateProductVariantHandler(IUnitOfWork unitOfWork, IMapper mapper)
|
||||
public class CreateProductVariantHandler(
|
||||
IUnitOfWork unitOfWork,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<CreateProductVariantCommand, ProductVariantDto>
|
||||
{
|
||||
public async Task<ProductVariantDto> Handle(CreateProductVariantCommand request, CancellationToken cancellationToken)
|
||||
public async Task<ProductVariantDto> Handle(
|
||||
CreateProductVariantCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await unitOfWork.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
@@ -30,7 +34,8 @@ public class CreateProductVariantHandler(IUnitOfWork unitOfWork, IMapper mapper)
|
||||
|
||||
productVariant.Product = null!;
|
||||
|
||||
var createdVariant = await unitOfWork.ProductVariantRepository.AddAsync(productVariant, cancellationToken);
|
||||
var createdVariant = await unitOfWork.ProductVariantRepository
|
||||
.AddAsync(productVariant, cancellationToken);
|
||||
|
||||
await unitOfWork.SaveAsync(cancellationToken);
|
||||
await unitOfWork.CommitTransactionAsync(cancellationToken);
|
||||
|
||||
@@ -7,15 +7,20 @@ public class DeleteProductVariantCommand : IRequest<bool>
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteProductVariantHandler(IUnitOfWork unitOfWork) : IRequestHandler<DeleteProductVariantCommand, bool>
|
||||
public class DeleteProductVariantHandler(
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<DeleteProductVariantCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(DeleteProductVariantCommand request, CancellationToken cancellationToken)
|
||||
public async Task<bool> Handle(
|
||||
DeleteProductVariantCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await unitOfWork.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var exists = await unitOfWork.ProductVariantRepository.ExistsAsync(request.Id, cancellationToken);
|
||||
var exists = await unitOfWork.ProductVariantRepository
|
||||
.ExistsAsync(request.Id, cancellationToken);
|
||||
|
||||
if (!exists)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,6 @@ using AutoMapper;
|
||||
using Imprink.Application.Dtos;
|
||||
using Imprink.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Imprink.Application.Commands.ProductVariants;
|
||||
|
||||
@@ -13,10 +12,14 @@ public class GetProductVariantsQuery : IRequest<IEnumerable<ProductVariantDto>>
|
||||
public bool InStockOnly { get; set; } = false;
|
||||
}
|
||||
|
||||
public class GetProductVariantsHandler(IUnitOfWork unitOfWork, IMapper mapper, ILogger<GetProductVariantsHandler> logger)
|
||||
public class GetProductVariantsHandler(
|
||||
IUnitOfWork unitOfWork,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<GetProductVariantsQuery, IEnumerable<ProductVariantDto>>
|
||||
{
|
||||
public async Task<IEnumerable<ProductVariantDto>> Handle(GetProductVariantsQuery request, CancellationToken cancellationToken)
|
||||
public async Task<IEnumerable<ProductVariantDto>> Handle(
|
||||
GetProductVariantsQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
IEnumerable<ProductVariant> variants;
|
||||
|
||||
@@ -24,15 +27,18 @@ public class GetProductVariantsHandler(IUnitOfWork unitOfWork, IMapper mapper, I
|
||||
{
|
||||
if (request.InStockOnly)
|
||||
{
|
||||
variants = await unitOfWork.ProductVariantRepository.GetInStockByProductIdAsync(request.ProductId.Value, cancellationToken);
|
||||
variants = await unitOfWork.ProductVariantRepository
|
||||
.GetInStockByProductIdAsync(request.ProductId.Value, cancellationToken);
|
||||
}
|
||||
else if (request.IsActive.HasValue && request.IsActive.Value)
|
||||
{
|
||||
variants = await unitOfWork.ProductVariantRepository.GetActiveByProductIdAsync(request.ProductId.Value, cancellationToken);
|
||||
variants = await unitOfWork.ProductVariantRepository
|
||||
.GetActiveByProductIdAsync(request.ProductId.Value, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
variants = await unitOfWork.ProductVariantRepository.GetByProductIdAsync(request.ProductId.Value, cancellationToken);
|
||||
variants = await unitOfWork.ProductVariantRepository
|
||||
.GetByProductIdAsync(request.ProductId.Value, cancellationToken);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
@@ -18,23 +18,29 @@ public class UpdateProductVariantCommand : IRequest<ProductVariantDto>
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateProductVariantHandler(IUnitOfWork unitOfWork, IMapper mapper)
|
||||
public class UpdateProductVariantHandler(
|
||||
IUnitOfWork unitOfWork,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<UpdateProductVariantCommand, ProductVariantDto>
|
||||
{
|
||||
public async Task<ProductVariantDto> Handle(UpdateProductVariantCommand request, CancellationToken cancellationToken)
|
||||
public async Task<ProductVariantDto> Handle(
|
||||
UpdateProductVariantCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await unitOfWork.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var existingVariant = await unitOfWork.ProductVariantRepository.GetByIdAsync(request.Id, cancellationToken);
|
||||
var existingVariant = await unitOfWork.ProductVariantRepository
|
||||
.GetByIdAsync(request.Id, cancellationToken);
|
||||
|
||||
if (existingVariant == null)
|
||||
throw new NotFoundException($"Product variant with ID {request.Id} not found.");
|
||||
|
||||
mapper.Map(request, existingVariant);
|
||||
|
||||
var updatedVariant = await unitOfWork.ProductVariantRepository.UpdateAsync(existingVariant, cancellationToken);
|
||||
var updatedVariant = await unitOfWork.ProductVariantRepository
|
||||
.UpdateAsync(existingVariant, cancellationToken);
|
||||
|
||||
await unitOfWork.SaveAsync(cancellationToken);
|
||||
await unitOfWork.CommitTransactionAsync(cancellationToken);
|
||||
|
||||
@@ -16,9 +16,14 @@ public class CreateProductCommand : IRequest<ProductDto>
|
||||
public Guid? CategoryId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateProductHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler<CreateProductCommand, ProductDto>
|
||||
public class CreateProductHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<CreateProductCommand, ProductDto>
|
||||
{
|
||||
public async Task<ProductDto> Handle(CreateProductCommand request, CancellationToken cancellationToken)
|
||||
public async Task<ProductDto> Handle(
|
||||
CreateProductCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return await uw.TransactAsync(async () =>
|
||||
{
|
||||
|
||||
@@ -8,13 +8,18 @@ public class DeleteProductCommand : IRequest
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteProductHandler(IUnitOfWork uw) : IRequestHandler<DeleteProductCommand>
|
||||
public class DeleteProductHandler(
|
||||
IUnitOfWork uw)
|
||||
: IRequestHandler<DeleteProductCommand>
|
||||
{
|
||||
public async Task Handle(DeleteProductCommand request, CancellationToken cancellationToken)
|
||||
public async Task Handle(
|
||||
DeleteProductCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await uw.TransactAsync(async () =>
|
||||
{
|
||||
var exists = await uw.ProductRepository.ExistsAsync(request.Id, cancellationToken);
|
||||
var exists = await uw.ProductRepository
|
||||
.ExistsAsync(request.Id, cancellationToken);
|
||||
|
||||
if (!exists)
|
||||
{
|
||||
|
||||
@@ -9,11 +9,16 @@ public class GetProductsQuery : IRequest<PagedResultDto<ProductDto>>
|
||||
public ProductFilterParameters FilterParameters { get; set; } = new();
|
||||
}
|
||||
|
||||
public class GetProductsHandler(IUnitOfWork unitOfWork) : IRequestHandler<GetProductsQuery, PagedResultDto<ProductDto>>
|
||||
public class GetProductsHandler(
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<GetProductsQuery, PagedResultDto<ProductDto>>
|
||||
{
|
||||
public async Task<PagedResultDto<ProductDto>> Handle(GetProductsQuery request, CancellationToken cancellationToken)
|
||||
public async Task<PagedResultDto<ProductDto>> Handle(
|
||||
GetProductsQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var pagedResult = await unitOfWork.ProductRepository.GetPagedAsync(request.FilterParameters, cancellationToken);
|
||||
var pagedResult = await unitOfWork.ProductRepository
|
||||
.GetPagedAsync(request.FilterParameters, cancellationToken);
|
||||
|
||||
var productDtos = pagedResult.Items.Select(p => new ProductDto
|
||||
{
|
||||
|
||||
@@ -16,15 +16,20 @@ public class UpdateProductCommand : IRequest<ProductDto>
|
||||
public Guid? CategoryId { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateProductHandler(IUnitOfWork unitOfWork) : IRequestHandler<UpdateProductCommand, ProductDto>
|
||||
public class UpdateProductHandler(
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<UpdateProductCommand, ProductDto>
|
||||
{
|
||||
public async Task<ProductDto> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
|
||||
public async Task<ProductDto> Handle(
|
||||
UpdateProductCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await unitOfWork.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var existingProduct = await unitOfWork.ProductRepository.GetByIdAsync(request.Id, cancellationToken);
|
||||
var existingProduct = await unitOfWork.ProductRepository
|
||||
.GetByIdAsync(request.Id, cancellationToken);
|
||||
|
||||
if (existingProduct == null)
|
||||
throw new NotFoundException($"Product with ID {request.Id} not found.");
|
||||
@@ -37,7 +42,8 @@ public class UpdateProductHandler(IUnitOfWork unitOfWork) : IRequestHandler<Upda
|
||||
existingProduct.ImageUrl = request.ImageUrl;
|
||||
existingProduct.CategoryId = request.CategoryId;
|
||||
|
||||
var updatedProduct = await unitOfWork.ProductRepository.UpdateAsync(existingProduct, cancellationToken);
|
||||
var updatedProduct = await unitOfWork.ProductRepository
|
||||
.UpdateAsync(existingProduct, cancellationToken);
|
||||
|
||||
var categoryDto = new CategoryDto
|
||||
{
|
||||
|
||||
@@ -7,9 +7,14 @@ namespace Imprink.Application.Commands.Users;
|
||||
|
||||
public record DeleteUserRoleCommand(string Sub, Guid RoleId) : IRequest<UserRoleDto?>;
|
||||
|
||||
public class DeleteUserRoleHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler<DeleteUserRoleCommand, UserRoleDto?>
|
||||
public class DeleteUserRoleHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<DeleteUserRoleCommand, UserRoleDto?>
|
||||
{
|
||||
public async Task<UserRoleDto?> Handle(DeleteUserRoleCommand request, CancellationToken cancellationToken)
|
||||
public async Task<UserRoleDto?> Handle(
|
||||
DeleteUserRoleCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await uw.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
@@ -18,12 +23,14 @@ public class DeleteUserRoleHandler(IUnitOfWork uw, IMapper mapper) : IRequestHan
|
||||
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
|
||||
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
|
||||
|
||||
var existingUserRole = await uw.UserRoleRepository.GetUserRoleAsync(request.Sub, request.RoleId, cancellationToken);
|
||||
var existingUserRole = await uw.UserRoleRepository
|
||||
.GetUserRoleAsync(request.Sub, request.RoleId, cancellationToken);
|
||||
|
||||
if (existingUserRole == null)
|
||||
throw new NotFoundException($"User role not found for user {request.Sub} and role {request.RoleId}");
|
||||
|
||||
var removedRole = await uw.UserRoleRepository.RemoveUserRoleAsync(existingUserRole, cancellationToken);
|
||||
var removedRole = await uw.UserRoleRepository
|
||||
.RemoveUserRoleAsync(existingUserRole, cancellationToken);
|
||||
|
||||
await uw.SaveAsync(cancellationToken);
|
||||
await uw.CommitTransactionAsync(cancellationToken);
|
||||
|
||||
@@ -6,11 +6,16 @@ namespace Imprink.Application.Commands.Users;
|
||||
|
||||
public record GetAllRolesCommand : IRequest<IEnumerable<RoleDto>>;
|
||||
|
||||
public class GetAllRolesHandler(IUnitOfWork uw, IMapper mapper): IRequestHandler<GetAllRolesCommand, IEnumerable<RoleDto>>
|
||||
public class GetAllRolesHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper): IRequestHandler<GetAllRolesCommand, IEnumerable<RoleDto>>
|
||||
{
|
||||
public async Task<IEnumerable<RoleDto>> Handle(GetAllRolesCommand request, CancellationToken cancellationToken)
|
||||
public async Task<IEnumerable<RoleDto>> Handle(
|
||||
GetAllRolesCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var roles = await uw.RoleRepository.GetAllRolesAsync(cancellationToken);
|
||||
var roles = await uw.RoleRepository
|
||||
.GetAllRolesAsync(cancellationToken);
|
||||
|
||||
return mapper.Map<IEnumerable<RoleDto>>(roles);
|
||||
}
|
||||
|
||||
@@ -7,14 +7,20 @@ namespace Imprink.Application.Commands.Users;
|
||||
|
||||
public record GetUserRolesCommand(string Sub) : IRequest<IEnumerable<RoleDto>>;
|
||||
|
||||
public class GetUserRolesHandler(IUnitOfWork uw, IMapper mapper): IRequestHandler<GetUserRolesCommand, IEnumerable<RoleDto>>
|
||||
public class GetUserRolesHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<GetUserRolesCommand, IEnumerable<RoleDto>>
|
||||
{
|
||||
public async Task<IEnumerable<RoleDto>> Handle(GetUserRolesCommand request, CancellationToken cancellationToken)
|
||||
public async Task<IEnumerable<RoleDto>> Handle(
|
||||
GetUserRolesCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
|
||||
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
|
||||
|
||||
var roles = await uw.UserRoleRepository.GetUserRolesAsync(request.Sub, cancellationToken);
|
||||
var roles = await uw.UserRoleRepository
|
||||
.GetUserRolesAsync(request.Sub, cancellationToken);
|
||||
|
||||
return mapper.Map<IEnumerable<RoleDto>>(roles);
|
||||
}
|
||||
|
||||
@@ -8,9 +8,15 @@ namespace Imprink.Application.Commands.Users;
|
||||
|
||||
public record SetUserFullNameCommand(string FirstName, string LastName) : IRequest<UserDto?>;
|
||||
|
||||
public class SetUserFullNameHandler(IUnitOfWork uw, IMapper mapper, ICurrentUserService userService) : IRequestHandler<SetUserFullNameCommand, UserDto?>
|
||||
public class SetUserFullNameHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper,
|
||||
ICurrentUserService userService)
|
||||
: IRequestHandler<SetUserFullNameCommand, UserDto?>
|
||||
{
|
||||
public async Task<UserDto?> Handle(SetUserFullNameCommand request, CancellationToken cancellationToken)
|
||||
public async Task<UserDto?> Handle(
|
||||
SetUserFullNameCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await uw.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
@@ -21,7 +27,8 @@ public class SetUserFullNameHandler(IUnitOfWork uw, IMapper mapper, ICurrentUser
|
||||
if (currentUser == null)
|
||||
throw new NotFoundException("User token could not be accessed.");
|
||||
|
||||
var user = await uw.UserRepository.SetUserFullNameAsync(currentUser, request.FirstName, request.LastName, cancellationToken);
|
||||
var user = await uw.UserRepository
|
||||
.SetUserFullNameAsync(currentUser, request.FirstName, request.LastName, cancellationToken);
|
||||
|
||||
if (user == null)
|
||||
throw new DataUpdateException("User name could not be updated.");
|
||||
|
||||
@@ -8,9 +8,15 @@ namespace Imprink.Application.Commands.Users;
|
||||
|
||||
public record SetUserPhoneCommand(string PhoneNumber) : IRequest<UserDto?>;
|
||||
|
||||
public class SetUserPhoneHandler(IUnitOfWork uw, IMapper mapper, ICurrentUserService userService) : IRequestHandler<SetUserPhoneCommand, UserDto?>
|
||||
public class SetUserPhoneHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper,
|
||||
ICurrentUserService userService)
|
||||
: IRequestHandler<SetUserPhoneCommand, UserDto?>
|
||||
{
|
||||
public async Task<UserDto?> Handle(SetUserPhoneCommand request, CancellationToken cancellationToken)
|
||||
public async Task<UserDto?> Handle(
|
||||
SetUserPhoneCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await uw.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
@@ -21,7 +27,8 @@ public class SetUserPhoneHandler(IUnitOfWork uw, IMapper mapper, ICurrentUserSer
|
||||
if (currentUser == null)
|
||||
throw new NotFoundException("User token could not be accessed.");
|
||||
|
||||
var user = await uw.UserRepository.SetUserPhoneAsync(currentUser, request.PhoneNumber, cancellationToken);
|
||||
var user = await uw.UserRepository
|
||||
.SetUserPhoneAsync(currentUser, request.PhoneNumber, cancellationToken);
|
||||
|
||||
if (user == null)
|
||||
throw new DataUpdateException("User phone could not be updated.");
|
||||
|
||||
@@ -8,9 +8,14 @@ namespace Imprink.Application.Commands.Users;
|
||||
|
||||
public record SetUserRoleCommand(string Sub, Guid RoleId) : IRequest<UserRoleDto?>;
|
||||
|
||||
public class SetUserRoleHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler<SetUserRoleCommand, UserRoleDto?>
|
||||
public class SetUserRoleHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<SetUserRoleCommand, UserRoleDto?>
|
||||
{
|
||||
public async Task<UserRoleDto?> Handle(SetUserRoleCommand request, CancellationToken cancellationToken)
|
||||
public async Task<UserRoleDto?> Handle(
|
||||
SetUserRoleCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await uw.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
@@ -25,7 +30,8 @@ public class SetUserRoleHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandle
|
||||
RoleId = request.RoleId
|
||||
};
|
||||
|
||||
var addedRole = await uw.UserRoleRepository.AddUserRoleAsync(userRole, cancellationToken);
|
||||
var addedRole = await uw.UserRoleRepository
|
||||
.AddUserRoleAsync(userRole, cancellationToken);
|
||||
|
||||
await uw.SaveAsync(cancellationToken);
|
||||
await uw.CommitTransactionAsync(cancellationToken);
|
||||
|
||||
@@ -7,15 +7,21 @@ namespace Imprink.Application.Commands.Users;
|
||||
|
||||
public record SyncUserCommand(Auth0User User) : IRequest<UserDto?>;
|
||||
|
||||
public class SyncUserHandler(IUnitOfWork uw, IMapper mapper): IRequestHandler<SyncUserCommand, UserDto?>
|
||||
public class SyncUserHandler(
|
||||
IUnitOfWork uw,
|
||||
IMapper mapper)
|
||||
: IRequestHandler<SyncUserCommand, UserDto?>
|
||||
{
|
||||
public async Task<UserDto?> Handle(SyncUserCommand request, CancellationToken cancellationToken)
|
||||
public async Task<UserDto?> Handle(
|
||||
SyncUserCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await uw.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var user = await uw.UserRepository.UpdateOrCreateUserAsync(request.User, cancellationToken);
|
||||
var user = await uw.UserRepository
|
||||
.UpdateOrCreateUserAsync(request.User, cancellationToken);
|
||||
|
||||
if (user == null)
|
||||
throw new Exception("User exists but could not be updated");
|
||||
|
||||
@@ -2,5 +2,5 @@ namespace Imprink.Application.Services;
|
||||
|
||||
public interface ICurrentUserService
|
||||
{
|
||||
string? GetCurrentUserId();
|
||||
string GetCurrentUserId();
|
||||
}
|
||||
@@ -11,27 +11,16 @@ namespace Imprink.WebApi.Controllers;
|
||||
public class AddressesController(IMediator mediator) : ControllerBase
|
||||
{
|
||||
|
||||
[HttpGet("{id:guid}")]
|
||||
[HttpGet("me")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<AddressDto>> GetAddressById(
|
||||
Guid id,
|
||||
[FromQuery] string? userId = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
public async Task<ActionResult<IEnumerable<AddressDto?>>> GetMyAddresses(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var result = await mediator.Send(new GetAddressByIdQuery
|
||||
{
|
||||
Id = id,
|
||||
UserId = userId
|
||||
}, cancellationToken);
|
||||
|
||||
if (result == null)
|
||||
return NotFound();
|
||||
|
||||
var result = await mediator.Send(new GetMyAddressesQuery(), cancellationToken);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpGet("user/{userId}")]
|
||||
[Authorize]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<ActionResult<IEnumerable<AddressDto>>> GetAddressesByUserId(
|
||||
string userId,
|
||||
[FromQuery] bool activeOnly = false,
|
||||
@@ -55,6 +44,6 @@ public class AddressesController(IMediator mediator) : ControllerBase
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var result = await mediator.Send(command, cancellationToken);
|
||||
return CreatedAtAction(nameof(GetAddressById), new { id = result.Id }, result);
|
||||
return CreatedAtAction(nameof(CreateAddress), new { id = result.Id }, result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user