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