public CabinetModel Post(CabinetModel model) { if (!dbContext.Users.Any(u => u.Id == model.UserId)) { throw new HttpResponseException(HttpStatusCode.NotFound); } var cabinet = model.ToDbCabinet(); dbContext.Add(cabinet); dbContext.Commit(); return Get(cabinet.Id); }
public CabinetModel Put(CabinetModel model) { var cabinet = dbContext.Cabinets .Include(c => c.Medications) .FirstOrDefault(m => m.Id == model.Id); if (cabinet == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } cabinet.Name = model.Name ?? cabinet.Name; cabinet.LastUpdated = DateTime.Now; if (model.Medications != null) { // Remove all medications from the cabinet foreach (var dbMedication in cabinet.Medications) { dbContext.Remove(dbMedication); } // Re-populate the cabinet cabinet.Medications = model.Medications.Select(m => m.ToDbMedication()).ToList(); } dbContext.Commit(); return Get(model.Id); }