37 lines
1.7 KiB
C#
37 lines
1.7 KiB
C#
using Netina.Domain.Entities.Orders;
|
|
using Netina.Repository.Abstracts;
|
|
|
|
namespace Netina.Repository.Handlers.Orders;
|
|
|
|
public class GetOrderLDtoQueryHandler(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService)
|
|
: IRequestHandler<GetOrderLDtoQuery, OrderLDto>
|
|
{
|
|
public async Task<OrderLDto> Handle(GetOrderLDtoQuery request, CancellationToken cancellationToken)
|
|
{
|
|
if (currentUserService.Permissions == null)
|
|
throw new BaseApiException(ApiResultStatusCode.UnAuthorized);
|
|
if (request.Id == default)
|
|
throw new AppException("Order id is null");
|
|
|
|
var order = await repositoryWrapper.SetRepository<Order>()
|
|
.TableNoTracking
|
|
.Where(o => o.Id == request.Id)
|
|
.Select(OrderMapper.ProjectToLDto)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
if (currentUserService.Permissions.Contains(ApplicationPermission.ViewMineOrders) && !currentUserService.Permissions.Contains(ApplicationPermission.ViewAllOrders))
|
|
{
|
|
if (currentUserService.UserId.IsNullOrEmpty() || !Guid.TryParse(currentUserService.UserId, out Guid userId))
|
|
throw new BaseApiException(ApiResultStatusCode.UnAuthorized);
|
|
|
|
var customer = await repositoryWrapper.SetRepository<Customer>()
|
|
.TableNoTracking
|
|
.FirstOrDefaultAsync(c => c.UserId == userId, cancellationToken);
|
|
|
|
if (customer == null || order.CustomerId != customer.Id)
|
|
throw new BaseApiException(ApiResultStatusCode.UnAuthorized);
|
|
}
|
|
if (order == null)
|
|
throw new AppException("Order not found", ApiResultStatusCode.NotFound);
|
|
return order;
|
|
}
|
|
} |