public IHttpActionResult PostSort(Task postedTask) { User user = CookieHelper.LoggedInUser(Request, db); if (user != null) { db.Entry(user).Collection(u => u.Tasks).Load(); Task movedTask = user.Tasks.Find(t => t.TaskID == postedTask.TaskID); if (movedTask.Priority != postedTask.Priority) { if (movedTask.Priority > postedTask.Priority) { //The task has become more of a priority //Move all tasks between the old position and the new position down one foreach (Task t in user.Tasks.Where(t => !t.Done && t.Priority < movedTask.Priority && t.Priority >= postedTask.Priority)) { t.Priority++; } } else { //The task's priority value has increased which means it became less of a priority foreach (Task t in user.Tasks.Where(t => !t.Done && t.Priority > movedTask.Priority && t.Priority<= postedTask.Priority)) { t.Priority--; } } movedTask.Priority = postedTask.Priority; } db.SaveChanges(); return StatusCode(HttpStatusCode.NoContent); } return StatusCode(HttpStatusCode.Forbidden); }
public IHttpActionResult PostTask(Task task) { User user = CookieHelper.LoggedInUser(Request, db); if (user != null) { db.Entry(user).Collection(u => u.Tasks).Load(); user.Tasks.Add(task); db.SaveChanges(); return StatusCode(HttpStatusCode.NoContent); } return StatusCode(HttpStatusCode.Forbidden); }