Esempio n. 1
0
        public async Task <T> PostAsync <T>(T postObject)
        {
            await _context.AddAsync(postObject);

            await _context.SaveChangesAsync();

            return(postObject);
        }
Esempio n. 2
0
        public async Task <ReservationResultDto> ReserveAsync(ReserveDto model)
        {
            var productIds = model.Items
                             .Select(i => i.Id)
                             .ToList();

            var strategy = _context.Database.CreateExecutionStrategy();

            return(await strategy.ExecuteAsync(async() =>
            {
                await using var transaction = _context.Database.BeginTransaction();
                try
                {
                    var warehouseItems = await GetWarehouseItems(productIds);

                    var reserve = new Reserve
                    {
                        CreationDate = DateTime.Now
                    };

                    foreach (var item in model.Items)
                    {
                        var warehouseItem = warehouseItems
                                            .FirstOrDefault(i => i.ProductId == item.Id);

                        if (warehouseItem == null)
                        {
                            throw new ProductNotFoundException(item.Id);
                        }

                        if (warehouseItem.Balance < item.Quantity)
                        {
                            throw new InsufficientProductQuantityException(item.Id, warehouseItem.Balance);
                        }

                        reserve.ReserveItems.Add(new ReserveItem
                        {
                            ProductId = item.Id,
                            Quantity = item.Quantity
                        });
                    }

                    await _context.AddAsync(reserve);

                    IncreaseProductReservedQuantity(warehouseItems, reserve.ReserveItems);

                    await _context.SaveChangesAsync();
                    await transaction.CommitAsync();

                    return reserve.MapToReservationResultDto();
                }
                catch
                {
                    await transaction.RollbackAsync();
                    throw;
                }
            }));
        }
Esempio n. 3
0
        private async Task AddModelAsync(IModel modelToPut)
        {
            await _context.AddAsync(modelToPut);

            await _context.SaveChangesAsync();
        }