public IHttpActionResult PutNotification(int id, Notification notification) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != notification.NotificationId) { return BadRequest(); } db.Entry(notification).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!NotificationExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostNotification(NotificationDTO notificationDTO) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var idUser = ((ClaimsIdentity)User.Identity).GetUserId(); Notification notification = new Notification() { CreatedBy = ((ClaimsIdentity)User.Identity).GetUserId(), CreatedDate = DateTime.Now, ModifiedBy = ((ClaimsIdentity)User.Identity).GetUserId(), ModifiedDate = DateTime.Now, DueDate = notificationDTO.DueDate, IsActive = true, RestaurantId = ((ClaimsIdentity)User.Identity).GetUserId() }; foreach (IngNotificationDTO ingNoty in notificationDTO.IngNotificationsDTO) { IngNotification ingNotification = new IngNotification() { TotalRequired = ingNoty.needed, CreatedBy = ((ClaimsIdentity)User.Identity).GetUserId(), CreatedDate = DateTime.Now, ModifiedBy = ((ClaimsIdentity)User.Identity).GetUserId(), ModifiedDate = DateTime.Now, IsActive = true, RestaurantId = ((ClaimsIdentity)User.Identity).GetUserId(), IngDish = db.IngDishes.Where(x => x.IngDishId == ingNoty.ingrDishId).FirstOrDefault() }; notification.IngNotifications.Add(ingNotification); } try { //turnOff All the old Active Notifications for the same Restaurant List<Notification> notifications = db.Notifications.Where(x=> x.IsActive ==true && x.RestaurantId == idUser).ToList(); foreach (Notification noti in notifications) { noti.IsActive = false; db.Entry(noti).State = EntityState.Modified; } db.Notifications.Add(notification); db.SaveChanges(); } catch (Exception e) { ErrorSignal.FromCurrentContext().Raise(e); throw new Exception("There was a problem saving this record: " + e.Message); } return CreatedAtRoute("DefaultApi", new { id = notification.NotificationId }, notification); }