35 lines
1.4 KiB
C#
35 lines
1.4 KiB
C#
using Netina.Domain.Entities.Orders;
|
|
|
|
namespace Netina.Repository.Handlers.Orders;
|
|
|
|
public class GetUserOrdersQueryHandler(ICurrentUserService currentUserService, IRepositoryWrapper repositoryWrapper)
|
|
: IRequestHandler<GetUserOrdersQuery, List<OrderSDto>>
|
|
{
|
|
public async Task<List<OrderSDto>> Handle(GetUserOrdersQuery request, CancellationToken cancellationToken)
|
|
{
|
|
Guid customerId = Guid.Empty;
|
|
if (request.CustomerId == default)
|
|
{
|
|
if (currentUserService.UserId == null)
|
|
throw new AppException("Token is wrong", ApiResultStatusCode.UnAuthorized);
|
|
if (!Guid.TryParse(currentUserService.UserId, out Guid userId))
|
|
throw new AppException("Token is wrong", ApiResultStatusCode.UnAuthorized);
|
|
var customer = await repositoryWrapper.SetRepository<Customer>()
|
|
.TableNoTracking
|
|
.FirstOrDefaultAsync(c => c.UserId == userId, cancellationToken);
|
|
if (customer == null)
|
|
throw new AppException("User is not a customer");
|
|
customerId = customer.Id;
|
|
}
|
|
else
|
|
customerId = request.CustomerId;
|
|
|
|
var orders = await repositoryWrapper.SetRepository<Order>()
|
|
.TableNoTracking
|
|
.Where(o => o.CustomerId == customerId)
|
|
.Select(OrderMapper.ProjectToSDto)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return orders;
|
|
}
|
|
} |