public async Task <IActionResult> CreateOrder([FromBody] CreateOrderDTO dto) { try { _orderDtoValidator.ValidateCreateOrderDto(dto); var user = await GetCurrentUser(); if (user.PharmacyId == null) { return(ApiUnauthorized()); } int pharmacyId = (int)user.PharmacyId; await _ordersManager.TryCreateOrder(dto, pharmacyId); return(Created()); } catch (DtoValidationException ex) { return(ApiBadRequest(ex.Message, ex.Parameter)); } catch (ResourceNotFoundException ex) { return(ApiNotFound(ex.Message, ex.Parameter)); } catch (DuplicateObjectException ex) { return(ApiBadRequest(ApiErrorSlug.ObjectAlreadyExists, ex.Message)); } }
private async Task Restock(Pharmacy pharmacy, RequiredMedicamentAmount requiredAmount, ProductBalance productInPharmacy) { var orderDto = await CreateOrderDTO(requiredAmount, productInPharmacy); try { await _ordersManager.TryCreateOrder(orderDto, pharmacy.Id); } catch (DuplicateObjectException ex) { var order = await Context.Orders .Where(o => o.Id == ex.Id) .Include(o => o.OrderProductBalances) .ThenInclude(opb => opb.ProductBalance) .FirstOrDefaultAsync(); order.UpdateFromDTO(await _ordersManager.CreateProductBalanceList(orderDto.Products)); await Context.SaveChangesAsync(); } }