/// <summary> /// Permite almacenar la reserva /// </summary> /// <param name="reserve"></param> /// <returns></returns> public async Task SaveReserveAync(ReserveProductModel reserve) { using (ApplicactionDbContext Db = new ApplicactionDbContext()) { await Db.AddAsync(reserve); await Db.SaveChangesAsync(); } }
/// <summary> /// Reserva la produyctos de a unidad /// </summary> /// <returns></returns> public async Task ReserveProducts(ProductsViewModel model, string idUser) { try { var product = await _ProductsRepository.GetProductsById(model); if (product != null && product.avalaible > 0) { product.avalaible = product.avalaible - 1; _ProductsRepository.UpdateProduct(product); ReserveProductModel _ReserveProductModel = new ReserveProductModel { delete = false, idProducto = product.idProduct, idUser = idUser }; await _ProductsRepository.SaveReserveAync(_ReserveProductModel); } } catch (Exception e) { throw e; } }
public async Task ReserveProduct(ReserveProductModel model) { var product = await _productContext.Products .FirstOrDefaultAsync(x => x.Id == model.ProductId); if (product == null) { throw new System.Exception($"Product with id {model.ProductId} does not exist"); } product.Amount = product.Amount - 1; var reservation = new ReservedProduct() { ClientId = model.ClientId, ProductId = model.ProductId, CreatedAt = DateTime.UtcNow }; _productContext.ReservedProducts.Add(reservation); await _productContext.SaveChangesAsync(); }
public async Task <IActionResult> Set([FromBody] ReserveProductModel model) { await _productService.ReserveProduct(model); return(NoContent()); }