public async Task <ActionResult <HousePlantItemDTO> > PostHousePlantItem(HousePlantItemDTO housePlantItemDTO) { // Create new housePlantItem with values from DTO var housePlantItem = new HousePlantItem { CommonName = housePlantItemDTO.CommonName, IsWatered = housePlantItemDTO.IsWatered }; _context.HousePlantItems.Add(housePlantItem); await _context.SaveChangesAsync(); return(CreatedAtAction("GetHousePlantItem", new { id = housePlantItem.ID }, ItemToDTO(housePlantItem))); }
public async Task <IActionResult> PutHousePlantItem(long id, HousePlantItemDTO housePlantItemDTO) { if (id != housePlantItemDTO.ID) { return(BadRequest()); } // The following was created on initial scaffold before modifying to use DTO //_context.Entry(housePlantItemDTO).State = EntityState.Modified; // Create a new housePlantItem with values from DTO var housePlantItem = await _context.HousePlantItems.FindAsync(id); if (housePlantItem == null) { return(NotFound()); } housePlantItem.CommonName = housePlantItemDTO.CommonName; housePlantItem.IsWatered = housePlantItemDTO.IsWatered; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!HousePlantItemExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }