コード例 #1
0
 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);
 }
コード例 #2
0
 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);
 }