Example #1
0
        public bool AddOrUpdateTaskItem(TaskItemDTO taskItem, string authorizedUserID)
        {
            using (var db = new NoteeContext())
            {
                var note = db.Notes.SingleOrDefault(n => n.NoteID == taskItem.NoteID);
                var user = db.SubUsers.SingleOrDefault(u => u.UserID == note.UserID);
                if (db.Notes.Any(n => n.NoteID == taskItem.NoteID) && user.UserID == authorizedUserID)
                {
                    TaskItem model;
                    if (taskItem.TaskItemID > 0)
                    {
                        model = db.TaskItems.Single(ti => ti.TaskItemID == taskItem.TaskItemID);
                    }
                    else
                    {
                        model = db.TaskItems.Create();
                        model.Note = db.Notes.Single(n => n.NoteID == taskItem.NoteID);
                        db.TaskItems.Add(model);
                    }

                    model.Text = taskItem.Text;
                    model.IsDone = taskItem.IsDone;

                    db.SaveChanges();
                    taskItem.TaskItemID = model.TaskItemID;
                    new NotesManager().UpdateModificationTime(model.NoteID);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
Example #2
0
        // POST: api/TaskItems
        public JsonResult<ModificationResult<TaskItemDTO>> PostTaskItem(TaskItemDTO taskItem)
        {
            var result = ValidateModelState<TaskItemDTO>();
            if (result != null)
            {
                return result;
            }

            if (taskItemsManager.AddOrUpdateTaskItem(taskItem, User.Identity.Name))
            {
                return Json(new ModificationResult<TaskItemDTO>(true)
                {
                    Data = taskItem
                });
            }
            else
            {
                return Json(new ModificationResult<TaskItemDTO>("Error: Nie znaleziono notatki (nie można dołączyć tego zadania do notatki o id: " + taskItem.NoteID + ")"));
            }
        }