public async Task <ActionResult <PharmacyInventoryDto> > Update(PharmacyInventoryDto pharmacyInventory) { try { bool exists = await _context.PharmacyInventory.AnyAsync(x => x.PharmacyInventoryId == pharmacyInventory.PharmacyInventoryId); if (!exists) { return(new NotFoundObjectResult("Id doesn't exist")); } bool itemExists = await _context.Item.AnyAsync(x => x.ItemId == pharmacyInventory.ItemId); if (!itemExists) { return(new NotFoundObjectResult("ItemId doesn't exist")); } bool pharmacyExists = await _context.Pharmacy.AnyAsync(x => x.PharmacyId == pharmacyInventory.PharmacyId); if (!pharmacyExists) { return(new NotFoundObjectResult("PharmacyId doesn't exist")); } if (!pharmacyInventory.QuantityOnHand.HasValue) { return(new NotFoundObjectResult("QuantityOnHand needs a value")); } else { if (pharmacyInventory.QuantityOnHand.Value <= 0) { return(new NotFoundObjectResult("QuantityOnHand must be non-zero value")); } } PharmacyInventory updatePharmacyInventory = _mapper.Map <PharmacyInventory>(pharmacyInventory); updatePharmacyInventory.ItemNumber = (await _context.Item.FirstOrDefaultAsync(x => x.ItemId == pharmacyInventory.ItemId)).ItemNumber; _context.Entry(updatePharmacyInventory).State = EntityState.Modified; _context.PharmacyInventory.Update(updatePharmacyInventory).Property(x => x.ItemNumber).IsModified = false; await _context.SaveChangesAsync(); return(_mapper.Map <PharmacyInventoryDto>(updatePharmacyInventory)); } catch (Exception ex) { throw ex; } }
public async Task <ActionResult <ItemDto> > Update(ItemDto item) { try { bool exists = await _context.Item.AnyAsync(x => x.ItemId == item.ItemId); if (!exists) { return(new NotFoundObjectResult("Id doesn't exist")); } if (string.IsNullOrEmpty(item.Upc)) { return(new NotFoundObjectResult("UPC must have a value")); } else { if (item.Upc.Length != 12) { return(new NotFoundObjectResult("UPC must have 12 digits")); } else { long isNumeric; if (!long.TryParse(item.Upc, out isNumeric)) { return(new NotFoundObjectResult("UPC must contain only numbers")); } } } bool itemVendorExists = await _context.ItemVendor.AnyAsync(x => x.ItemVendorId == item.ItemVendorId); if (!itemVendorExists) { return(new NotFoundObjectResult("ItemVendor doesn't exist")); } Item updateItem = _mapper.Map <Item>(item); _context.Entry(updateItem).State = EntityState.Modified; _context.Item.Update(updateItem).Property(x => x.ItemNumber).IsModified = false; await _context.SaveChangesAsync(); return(_mapper.Map <ItemDto>(updateItem)); } catch (Exception ex) { throw ex; } }