From 3cdfbf895429f07d4e51be196a0c1e84f32f7a79 Mon Sep 17 00:00:00 2001 From: lumijiez <59575049+lumijiez@users.noreply.github.com> Date: Thu, 26 Jun 2025 00:51:24 +0300 Subject: [PATCH] Cleanup in handlers --- .../Addresses/CreateAddressHandler.cs | 15 +++++---- .../Addresses/GetAddressByIdHandler.cs | 31 ------------------ .../Addresses/GetAddressesByUserIdHandler.cs | 17 +++++++--- .../Addresses/GetMyAddressesHandler.cs | 26 +++++++++++++++ .../Categories/CreateCategoryHandler.cs | 12 +++++-- .../Categories/DeleteCategoryHandler.cs | 12 +++++-- .../Categories/GetCategoriesHandler.cs | 7 ++-- .../Categories/UpdateCategoryHandler.cs | 15 ++++++--- .../Commands/Orders/CreateOrderHandler.cs | 32 +++++++++++++------ .../Commands/Orders/GetOrderByIdHandler.cs | 9 ++++-- .../Orders/GetOrdersByMerchantIdHandler.cs | 15 ++++++--- .../Orders/GetOrdersByUserIdHandler.cs | 15 ++++++--- .../CreateProductVariantHandler.cs | 11 +++++-- .../DeleteProductVariantHandler.cs | 11 +++++-- .../GetProductVariantsHandler.cs | 18 +++++++---- .../UpdateProductVariantHandler.cs | 14 +++++--- .../Commands/Products/CreateProductHandler.cs | 9 ++++-- .../Commands/Products/DeleteProductHandler.cs | 11 +++++-- .../Commands/Products/GetProductsHandler.cs | 11 +++++-- .../Commands/Products/UpdateProductCommand.cs | 14 +++++--- .../Commands/Users/DeleteUserRoleHandler.cs | 15 ++++++--- .../Commands/Users/GetAllRolesHandler.cs | 11 +++++-- .../Commands/Users/GetUserRolesHandler.cs | 12 +++++-- .../Commands/Users/SetUserFullNameHandler.cs | 13 ++++++-- .../Commands/Users/SetUserPhoneHandler.cs | 13 ++++++-- .../Commands/Users/SetUserRoleHandler.cs | 12 +++++-- .../Commands/Users/SyncUserHandler.cs | 12 +++++-- .../Services/ICurrentUserService.cs | 2 +- .../Controllers/AddressesController.cs | 21 +++--------- 29 files changed, 274 insertions(+), 142 deletions(-) delete mode 100644 src/Imprink.Application/Commands/Addresses/GetAddressByIdHandler.cs create mode 100644 src/Imprink.Application/Commands/Addresses/GetMyAddressesHandler.cs diff --git a/src/Imprink.Application/Commands/Addresses/CreateAddressHandler.cs b/src/Imprink.Application/Commands/Addresses/CreateAddressHandler.cs index 330559e..9e9a94c 100644 --- a/src/Imprink.Application/Commands/Addresses/CreateAddressHandler.cs +++ b/src/Imprink.Application/Commands/Addresses/CreateAddressHandler.cs @@ -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 public class CreateAddressHandler( IUnitOfWork uw, IMapper mapper, - ICurrentUserService userService, - ILogger logger) + ICurrentUserService userService) : IRequestHandler { - public async Task Handle(CreateAddressCommand request, CancellationToken cancellationToken) + public async Task Handle( + CreateAddressCommand request, + CancellationToken cancellationToken) { return await uw.TransactAsync(async () => { var address = mapper.Map
(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; diff --git a/src/Imprink.Application/Commands/Addresses/GetAddressByIdHandler.cs b/src/Imprink.Application/Commands/Addresses/GetAddressByIdHandler.cs deleted file mode 100644 index 3d14552..0000000 --- a/src/Imprink.Application/Commands/Addresses/GetAddressByIdHandler.cs +++ /dev/null @@ -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 -{ - public Guid Id { get; set; } - public string? UserId { get; set; } -} - -public class GetAddressByIdHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler -{ - public async Task 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(address) : null; - } -} \ No newline at end of file diff --git a/src/Imprink.Application/Commands/Addresses/GetAddressesByUserIdHandler.cs b/src/Imprink.Application/Commands/Addresses/GetAddressesByUserIdHandler.cs index 9d151cc..5816518 100644 --- a/src/Imprink.Application/Commands/Addresses/GetAddressesByUserIdHandler.cs +++ b/src/Imprink.Application/Commands/Addresses/GetAddressesByUserIdHandler.cs @@ -8,23 +8,30 @@ namespace Imprink.Application.Commands.Addresses; public class GetAddressesByUserIdQuery : IRequest> { 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> +public class GetAddressesByUserIdHandler( + IUnitOfWork uw, + IMapper mapper) + : IRequestHandler> { - public async Task> Handle(GetAddressesByUserIdQuery request, CancellationToken cancellationToken) + public async Task> Handle( + GetAddressesByUserIdQuery request, + CancellationToken cancellationToken) { IEnumerable
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 { diff --git a/src/Imprink.Application/Commands/Addresses/GetMyAddressesHandler.cs b/src/Imprink.Application/Commands/Addresses/GetMyAddressesHandler.cs new file mode 100644 index 0000000..3f7a565 --- /dev/null +++ b/src/Imprink.Application/Commands/Addresses/GetMyAddressesHandler.cs @@ -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>; + +public class GetMyAddressesHandler( + IUnitOfWork uw, + IMapper mapper, + ICurrentUserService userService) + : IRequestHandler> +{ + public async Task> Handle( + GetMyAddressesQuery request, + CancellationToken cancellationToken) + { + IEnumerable addresses = await uw.AddressRepository + .GetByUserIdAsync(userService.GetCurrentUserId(), cancellationToken); + + return mapper.Map>(addresses); + } +} \ No newline at end of file diff --git a/src/Imprink.Application/Commands/Categories/CreateCategoryHandler.cs b/src/Imprink.Application/Commands/Categories/CreateCategoryHandler.cs index 310578a..d690cc4 100644 --- a/src/Imprink.Application/Commands/Categories/CreateCategoryHandler.cs +++ b/src/Imprink.Application/Commands/Categories/CreateCategoryHandler.cs @@ -14,9 +14,13 @@ public class CreateCategoryCommand : IRequest public Guid? ParentCategoryId { get; set; } } -public class CreateCategoryHandler(IUnitOfWork unitOfWork) : IRequestHandler +public class CreateCategoryHandler( + IUnitOfWork unitOfWork) + : IRequestHandler { - public async Task Handle(CreateCategoryCommand request, CancellationToken cancellationToken) + public async Task Handle( + CreateCategoryCommand request, + CancellationToken cancellationToken) { await unitOfWork.BeginTransactionAsync(cancellationToken); @@ -32,7 +36,9 @@ public class CreateCategoryHandler(IUnitOfWork unitOfWork) : IRequestHandler public Guid Id { get; init; } } -public class DeleteCategoryHandler(IUnitOfWork unitOfWork) : IRequestHandler +public class DeleteCategoryHandler( + IUnitOfWork unitOfWork) + : IRequestHandler { - public async Task Handle(DeleteCategoryCommand request, CancellationToken cancellationToken) + public async Task 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); diff --git a/src/Imprink.Application/Commands/Categories/GetCategoriesHandler.cs b/src/Imprink.Application/Commands/Categories/GetCategoriesHandler.cs index b606dbe..1e5fcbd 100644 --- a/src/Imprink.Application/Commands/Categories/GetCategoriesHandler.cs +++ b/src/Imprink.Application/Commands/Categories/GetCategoriesHandler.cs @@ -10,10 +10,13 @@ public class GetCategoriesQuery : IRequest> public bool RootCategoriesOnly { get; set; } = false; } -public class GetCategoriesHandler(IUnitOfWork unitOfWork) +public class GetCategoriesHandler( + IUnitOfWork unitOfWork) : IRequestHandler> { - public async Task> Handle(GetCategoriesQuery request, CancellationToken cancellationToken) + public async Task> Handle( + GetCategoriesQuery request, + CancellationToken cancellationToken) { IEnumerable categories; diff --git a/src/Imprink.Application/Commands/Categories/UpdateCategoryHandler.cs b/src/Imprink.Application/Commands/Categories/UpdateCategoryHandler.cs index f9f5f23..975d3c5 100644 --- a/src/Imprink.Application/Commands/Categories/UpdateCategoryHandler.cs +++ b/src/Imprink.Application/Commands/Categories/UpdateCategoryHandler.cs @@ -15,15 +15,20 @@ public class UpdateCategoryCommand : IRequest public Guid? ParentCategoryId { get; set; } } -public class UpdateCategoryHandler(IUnitOfWork unitOfWork) : IRequestHandler +public class UpdateCategoryHandler( + IUnitOfWork unitOfWork) + : IRequestHandler { - public async Task Handle(UpdateCategoryCommand request, CancellationToken cancellationToken) + public async Task 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 public Guid AddressId { get; set; } } -public class CreateOrderHandler(IUnitOfWork uw, IMapper mapper, ICurrentUserService userService) : IRequestHandler +public class CreateOrderHandler( + IUnitOfWork uw, + IMapper mapper, + ICurrentUserService userService) + : IRequestHandler { - public async Task Handle(CreateOrderCommand request, CancellationToken cancellationToken) + public async Task 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(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); diff --git a/src/Imprink.Application/Commands/Orders/GetOrderByIdHandler.cs b/src/Imprink.Application/Commands/Orders/GetOrderByIdHandler.cs index 8e4724f..f9d0770 100644 --- a/src/Imprink.Application/Commands/Orders/GetOrderByIdHandler.cs +++ b/src/Imprink.Application/Commands/Orders/GetOrderByIdHandler.cs @@ -11,9 +11,14 @@ public class GetOrderByIdQuery : IRequest public bool IncludeDetails { get; set; } } -public class GetOrderByIdHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler +public class GetOrderByIdHandler( + IUnitOfWork uw, + IMapper mapper) + : IRequestHandler { - public async Task Handle(GetOrderByIdQuery request, CancellationToken cancellationToken) + public async Task Handle( + GetOrderByIdQuery request, + CancellationToken cancellationToken) { Order? order; diff --git a/src/Imprink.Application/Commands/Orders/GetOrdersByMerchantIdHandler.cs b/src/Imprink.Application/Commands/Orders/GetOrdersByMerchantIdHandler.cs index bb62a68..93b30ac 100644 --- a/src/Imprink.Application/Commands/Orders/GetOrdersByMerchantIdHandler.cs +++ b/src/Imprink.Application/Commands/Orders/GetOrdersByMerchantIdHandler.cs @@ -11,19 +11,26 @@ public class GetOrdersByMerchantIdQuery : IRequest> public bool IncludeDetails { get; set; } } -public class GetOrdersByMerchantIdHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler> +public class GetOrdersByMerchantIdHandler( + IUnitOfWork uw, + IMapper mapper) + : IRequestHandler> { - public async Task> Handle(GetOrdersByMerchantIdQuery request, CancellationToken cancellationToken) + public async Task> Handle( + GetOrdersByMerchantIdQuery request, + CancellationToken cancellationToken) { IEnumerable 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>(orders); diff --git a/src/Imprink.Application/Commands/Orders/GetOrdersByUserIdHandler.cs b/src/Imprink.Application/Commands/Orders/GetOrdersByUserIdHandler.cs index a73a700..41dd354 100644 --- a/src/Imprink.Application/Commands/Orders/GetOrdersByUserIdHandler.cs +++ b/src/Imprink.Application/Commands/Orders/GetOrdersByUserIdHandler.cs @@ -11,19 +11,26 @@ public class GetOrdersByUserIdQuery : IRequest> public bool IncludeDetails { get; set; } } -public class GetOrdersByUserIdHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler> +public class GetOrdersByUserIdHandler( + IUnitOfWork uw, + IMapper mapper) + : IRequestHandler> { - public async Task> Handle(GetOrdersByUserIdQuery request, CancellationToken cancellationToken) + public async Task> Handle( + GetOrdersByUserIdQuery request, + CancellationToken cancellationToken) { IEnumerable 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>(orders); diff --git a/src/Imprink.Application/Commands/ProductVariants/CreateProductVariantHandler.cs b/src/Imprink.Application/Commands/ProductVariants/CreateProductVariantHandler.cs index 0ab7aa7..1444882 100644 --- a/src/Imprink.Application/Commands/ProductVariants/CreateProductVariantHandler.cs +++ b/src/Imprink.Application/Commands/ProductVariants/CreateProductVariantHandler.cs @@ -17,10 +17,14 @@ public class CreateProductVariantCommand : IRequest public bool IsActive { get; set; } = true; } -public class CreateProductVariantHandler(IUnitOfWork unitOfWork, IMapper mapper) +public class CreateProductVariantHandler( + IUnitOfWork unitOfWork, + IMapper mapper) : IRequestHandler { - public async Task Handle(CreateProductVariantCommand request, CancellationToken cancellationToken) + public async Task 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); diff --git a/src/Imprink.Application/Commands/ProductVariants/DeleteProductVariantHandler.cs b/src/Imprink.Application/Commands/ProductVariants/DeleteProductVariantHandler.cs index 8e6a2ea..d461975 100644 --- a/src/Imprink.Application/Commands/ProductVariants/DeleteProductVariantHandler.cs +++ b/src/Imprink.Application/Commands/ProductVariants/DeleteProductVariantHandler.cs @@ -7,15 +7,20 @@ public class DeleteProductVariantCommand : IRequest public Guid Id { get; set; } } -public class DeleteProductVariantHandler(IUnitOfWork unitOfWork) : IRequestHandler +public class DeleteProductVariantHandler( + IUnitOfWork unitOfWork) + : IRequestHandler { - public async Task Handle(DeleteProductVariantCommand request, CancellationToken cancellationToken) + public async Task 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) { diff --git a/src/Imprink.Application/Commands/ProductVariants/GetProductVariantsHandler.cs b/src/Imprink.Application/Commands/ProductVariants/GetProductVariantsHandler.cs index 5e76ae2..1e56414 100644 --- a/src/Imprink.Application/Commands/ProductVariants/GetProductVariantsHandler.cs +++ b/src/Imprink.Application/Commands/ProductVariants/GetProductVariantsHandler.cs @@ -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> public bool InStockOnly { get; set; } = false; } -public class GetProductVariantsHandler(IUnitOfWork unitOfWork, IMapper mapper, ILogger logger) +public class GetProductVariantsHandler( + IUnitOfWork unitOfWork, + IMapper mapper) : IRequestHandler> { - public async Task> Handle(GetProductVariantsQuery request, CancellationToken cancellationToken) + public async Task> Handle( + GetProductVariantsQuery request, + CancellationToken cancellationToken) { IEnumerable 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 diff --git a/src/Imprink.Application/Commands/ProductVariants/UpdateProductVariantHandler.cs b/src/Imprink.Application/Commands/ProductVariants/UpdateProductVariantHandler.cs index 0c88be3..e45232c 100644 --- a/src/Imprink.Application/Commands/ProductVariants/UpdateProductVariantHandler.cs +++ b/src/Imprink.Application/Commands/ProductVariants/UpdateProductVariantHandler.cs @@ -18,23 +18,29 @@ public class UpdateProductVariantCommand : IRequest public bool IsActive { get; set; } } -public class UpdateProductVariantHandler(IUnitOfWork unitOfWork, IMapper mapper) +public class UpdateProductVariantHandler( + IUnitOfWork unitOfWork, + IMapper mapper) : IRequestHandler { - public async Task Handle(UpdateProductVariantCommand request, CancellationToken cancellationToken) + public async Task 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); diff --git a/src/Imprink.Application/Commands/Products/CreateProductHandler.cs b/src/Imprink.Application/Commands/Products/CreateProductHandler.cs index ac12e05..7b93c3c 100644 --- a/src/Imprink.Application/Commands/Products/CreateProductHandler.cs +++ b/src/Imprink.Application/Commands/Products/CreateProductHandler.cs @@ -16,9 +16,14 @@ public class CreateProductCommand : IRequest public Guid? CategoryId { get; set; } } -public class CreateProductHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler +public class CreateProductHandler( + IUnitOfWork uw, + IMapper mapper) + : IRequestHandler { - public async Task Handle(CreateProductCommand request, CancellationToken cancellationToken) + public async Task Handle( + CreateProductCommand request, + CancellationToken cancellationToken) { return await uw.TransactAsync(async () => { diff --git a/src/Imprink.Application/Commands/Products/DeleteProductHandler.cs b/src/Imprink.Application/Commands/Products/DeleteProductHandler.cs index 2e6c326..c839fad 100644 --- a/src/Imprink.Application/Commands/Products/DeleteProductHandler.cs +++ b/src/Imprink.Application/Commands/Products/DeleteProductHandler.cs @@ -8,13 +8,18 @@ public class DeleteProductCommand : IRequest public Guid Id { get; set; } } -public class DeleteProductHandler(IUnitOfWork uw) : IRequestHandler +public class DeleteProductHandler( + IUnitOfWork uw) + : IRequestHandler { - 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) { diff --git a/src/Imprink.Application/Commands/Products/GetProductsHandler.cs b/src/Imprink.Application/Commands/Products/GetProductsHandler.cs index 4e65018..0ef998d 100644 --- a/src/Imprink.Application/Commands/Products/GetProductsHandler.cs +++ b/src/Imprink.Application/Commands/Products/GetProductsHandler.cs @@ -9,11 +9,16 @@ public class GetProductsQuery : IRequest> public ProductFilterParameters FilterParameters { get; set; } = new(); } -public class GetProductsHandler(IUnitOfWork unitOfWork) : IRequestHandler> +public class GetProductsHandler( + IUnitOfWork unitOfWork) + : IRequestHandler> { - public async Task> Handle(GetProductsQuery request, CancellationToken cancellationToken) + public async Task> 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 { diff --git a/src/Imprink.Application/Commands/Products/UpdateProductCommand.cs b/src/Imprink.Application/Commands/Products/UpdateProductCommand.cs index 06f1313..c60cbb6 100644 --- a/src/Imprink.Application/Commands/Products/UpdateProductCommand.cs +++ b/src/Imprink.Application/Commands/Products/UpdateProductCommand.cs @@ -16,15 +16,20 @@ public class UpdateProductCommand : IRequest public Guid? CategoryId { get; set; } } -public class UpdateProductHandler(IUnitOfWork unitOfWork) : IRequestHandler +public class UpdateProductHandler( + IUnitOfWork unitOfWork) + : IRequestHandler { - public async Task Handle(UpdateProductCommand request, CancellationToken cancellationToken) + public async Task 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; -public class DeleteUserRoleHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler +public class DeleteUserRoleHandler( + IUnitOfWork uw, + IMapper mapper) + : IRequestHandler { - public async Task Handle(DeleteUserRoleCommand request, CancellationToken cancellationToken) + public async Task 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); diff --git a/src/Imprink.Application/Commands/Users/GetAllRolesHandler.cs b/src/Imprink.Application/Commands/Users/GetAllRolesHandler.cs index ec1661b..ec4d093 100644 --- a/src/Imprink.Application/Commands/Users/GetAllRolesHandler.cs +++ b/src/Imprink.Application/Commands/Users/GetAllRolesHandler.cs @@ -6,11 +6,16 @@ namespace Imprink.Application.Commands.Users; public record GetAllRolesCommand : IRequest>; -public class GetAllRolesHandler(IUnitOfWork uw, IMapper mapper): IRequestHandler> +public class GetAllRolesHandler( + IUnitOfWork uw, + IMapper mapper): IRequestHandler> { - public async Task> Handle(GetAllRolesCommand request, CancellationToken cancellationToken) + public async Task> Handle( + GetAllRolesCommand request, + CancellationToken cancellationToken) { - var roles = await uw.RoleRepository.GetAllRolesAsync(cancellationToken); + var roles = await uw.RoleRepository + .GetAllRolesAsync(cancellationToken); return mapper.Map>(roles); } diff --git a/src/Imprink.Application/Commands/Users/GetUserRolesHandler.cs b/src/Imprink.Application/Commands/Users/GetUserRolesHandler.cs index ea3f8d8..5517ee0 100644 --- a/src/Imprink.Application/Commands/Users/GetUserRolesHandler.cs +++ b/src/Imprink.Application/Commands/Users/GetUserRolesHandler.cs @@ -7,14 +7,20 @@ namespace Imprink.Application.Commands.Users; public record GetUserRolesCommand(string Sub) : IRequest>; -public class GetUserRolesHandler(IUnitOfWork uw, IMapper mapper): IRequestHandler> +public class GetUserRolesHandler( + IUnitOfWork uw, + IMapper mapper) + : IRequestHandler> { - public async Task> Handle(GetUserRolesCommand request, CancellationToken cancellationToken) + public async Task> 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>(roles); } diff --git a/src/Imprink.Application/Commands/Users/SetUserFullNameHandler.cs b/src/Imprink.Application/Commands/Users/SetUserFullNameHandler.cs index 11c9597..0f4a1ca 100644 --- a/src/Imprink.Application/Commands/Users/SetUserFullNameHandler.cs +++ b/src/Imprink.Application/Commands/Users/SetUserFullNameHandler.cs @@ -8,9 +8,15 @@ namespace Imprink.Application.Commands.Users; public record SetUserFullNameCommand(string FirstName, string LastName) : IRequest; -public class SetUserFullNameHandler(IUnitOfWork uw, IMapper mapper, ICurrentUserService userService) : IRequestHandler +public class SetUserFullNameHandler( + IUnitOfWork uw, + IMapper mapper, + ICurrentUserService userService) + : IRequestHandler { - public async Task Handle(SetUserFullNameCommand request, CancellationToken cancellationToken) + public async Task 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."); diff --git a/src/Imprink.Application/Commands/Users/SetUserPhoneHandler.cs b/src/Imprink.Application/Commands/Users/SetUserPhoneHandler.cs index ac7bc68..f3876e7 100644 --- a/src/Imprink.Application/Commands/Users/SetUserPhoneHandler.cs +++ b/src/Imprink.Application/Commands/Users/SetUserPhoneHandler.cs @@ -8,9 +8,15 @@ namespace Imprink.Application.Commands.Users; public record SetUserPhoneCommand(string PhoneNumber) : IRequest; -public class SetUserPhoneHandler(IUnitOfWork uw, IMapper mapper, ICurrentUserService userService) : IRequestHandler +public class SetUserPhoneHandler( + IUnitOfWork uw, + IMapper mapper, + ICurrentUserService userService) + : IRequestHandler { - public async Task Handle(SetUserPhoneCommand request, CancellationToken cancellationToken) + public async Task 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."); diff --git a/src/Imprink.Application/Commands/Users/SetUserRoleHandler.cs b/src/Imprink.Application/Commands/Users/SetUserRoleHandler.cs index 7c265f0..e826982 100644 --- a/src/Imprink.Application/Commands/Users/SetUserRoleHandler.cs +++ b/src/Imprink.Application/Commands/Users/SetUserRoleHandler.cs @@ -8,9 +8,14 @@ namespace Imprink.Application.Commands.Users; public record SetUserRoleCommand(string Sub, Guid RoleId) : IRequest; -public class SetUserRoleHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler +public class SetUserRoleHandler( + IUnitOfWork uw, + IMapper mapper) + : IRequestHandler { - public async Task Handle(SetUserRoleCommand request, CancellationToken cancellationToken) + public async Task 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); diff --git a/src/Imprink.Application/Commands/Users/SyncUserHandler.cs b/src/Imprink.Application/Commands/Users/SyncUserHandler.cs index a304b27..e30da35 100644 --- a/src/Imprink.Application/Commands/Users/SyncUserHandler.cs +++ b/src/Imprink.Application/Commands/Users/SyncUserHandler.cs @@ -7,15 +7,21 @@ namespace Imprink.Application.Commands.Users; public record SyncUserCommand(Auth0User User) : IRequest; -public class SyncUserHandler(IUnitOfWork uw, IMapper mapper): IRequestHandler +public class SyncUserHandler( + IUnitOfWork uw, + IMapper mapper) + : IRequestHandler { - public async Task Handle(SyncUserCommand request, CancellationToken cancellationToken) + public async Task 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"); diff --git a/src/Imprink.Application/Services/ICurrentUserService.cs b/src/Imprink.Application/Services/ICurrentUserService.cs index 83bbe41..b4b9c4a 100644 --- a/src/Imprink.Application/Services/ICurrentUserService.cs +++ b/src/Imprink.Application/Services/ICurrentUserService.cs @@ -2,5 +2,5 @@ namespace Imprink.Application.Services; public interface ICurrentUserService { - string? GetCurrentUserId(); + string GetCurrentUserId(); } \ No newline at end of file diff --git a/src/Imprink.WebApi/Controllers/AddressesController.cs b/src/Imprink.WebApi/Controllers/AddressesController.cs index bac0cfb..79e3a39 100644 --- a/src/Imprink.WebApi/Controllers/AddressesController.cs +++ b/src/Imprink.WebApi/Controllers/AddressesController.cs @@ -11,27 +11,16 @@ namespace Imprink.WebApi.Controllers; public class AddressesController(IMediator mediator) : ControllerBase { - [HttpGet("{id:guid}")] + [HttpGet("me")] [Authorize] - public async Task> GetAddressById( - Guid id, - [FromQuery] string? userId = null, - CancellationToken cancellationToken = default) + public async Task>> 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>> 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); } } \ No newline at end of file