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; } } }
public void AddOrUpdateTaskItems(int noteId, IEnumerable<TaskItemDTO> tasks) { using (var db = new NoteeContext()) { foreach (var taskItem in tasks) { 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 == noteId); db.TaskItems.Add(model); } model.Text = taskItem.Text; model.IsDone = taskItem.IsDone; db.SaveChanges(); taskItem.TaskItemID = model.TaskItemID; new NotesManager().UpdateModificationTime(model.NoteID); } } }
public bool AddOrUpdateNote(NoteDTO note, string authorizedUserID) { using (var db = new NoteeContext()) { var user = db.SubUsers.SingleOrDefault(u => u.UserID == note.UserID); if (user.UserID != authorizedUserID) { return false; } Note model; if (db.SubUsers.Any(u => u.UserID == note.UserID)) { if (note.NoteID > 0) { model = db.Notes.Single(n => n.NoteID == note.NoteID); } else { model = db.Notes.Create(); model.User = db.SubUsers.Single(u => u.UserID == note.UserID); db.Notes.Add(model); } model.Title = note.Title; model.Text = note.Text; model.NoteType = note.Type; model.ModificationDate = DateTime.Now; model.Color = note.Color; model.ImageUrl = note.ImageUrl; model.Label = note.Label; model.isLocked = note.IsLocked; db.SaveChanges(); note.NoteID = model.NoteID; note.ModificationDate = model.ModificationDate; } else { return false; } new TaskItemsManager().AddOrUpdateTaskItems(model.NoteID, note.TaskItems); note.TaskItems.Clear(); model.TaskItems.ToList().ForEach(ti => note.TaskItems.Add(new TaskItemDTO(ti))); } return true; }
public void AddOrUpdateUser(UserDTO user) { using (var db = new NoteeContext()) { var model = db.SubUsers.SingleOrDefault(u => u.UserID == user.UserID); if(model == null) { model = db.SubUsers.Create(); model.UserID = user.UserID; db.SubUsers.Add(model); } db.SaveChanges(); user.UserID = model.UserID; new NotesManager().AddOrUpdateNotes(model.UserID, user.Notes); user.Notes.Clear(); model.Notes.ToList().ForEach(n => user.Notes.Add(new NoteDTO(n))); } }
public bool DeleteTaskItem(int id, string authorizedUserID) { using (var db = new NoteeContext()) { var taskItem = db.TaskItems.SingleOrDefault(ti => ti.TaskItemID == id); var note = db.Notes.SingleOrDefault(n => n.NoteID == taskItem.NoteID); var user = db.SubUsers.SingleOrDefault(u => u.UserID == note.UserID); if (authorizedUserID != user.UserID) { return false; } if (taskItem == null) { return false; } else { db.TaskItems.Remove(taskItem); db.SaveChanges(); new NotesManager().UpdateModificationTime(taskItem.NoteID); return true; } } }
public bool DeleteNote(int id, string authorizedUserID) { using (var db = new NoteeContext()) { var note = db.Notes.SingleOrDefault(n => n.NoteID == id); var user = db.SubUsers.SingleOrDefault(u => u.UserID == note.UserID); if (user.UserID != authorizedUserID) { return false; } if (note != null) { db.Notes.Remove(note); db.SaveChanges(); return true; } else { return false; } } }
public void AddOrUpdateNotes(string userId, IEnumerable<NoteDTO> notes) { foreach (var note in notes) { using (var db = new NoteeContext()) { Note model; if (note.NoteID > 0) { model = db.Notes.Single(n => n.NoteID == note.NoteID); } else { model = db.Notes.Create(); model.User = db.SubUsers.Single(u => u.UserID == userId); db.Notes.Add(model); } model.Title = note.Title; model.Text = note.Text; model.NoteType = note.Type; model.ModificationDate = DateTime.Now; model.Color = note.Color; model.ImageUrl = note.ImageUrl; model.Label = note.Label; model.isLocked = note.IsLocked; db.SaveChanges(); note.NoteID = model.NoteID; note.ModificationDate = model.ModificationDate; new TaskItemsManager().AddOrUpdateTaskItems(model.NoteID, note.TaskItems); note.TaskItems.Clear(); model.TaskItems.ToList().ForEach(ti => note.TaskItems.Add(new TaskItemDTO(ti))); } } }
public bool UpdateModificationTime(int id) { using (var db = new NoteeContext()) { var note = db.Notes.SingleOrDefault(n => n.NoteID == id); if (note != null) { note.ModificationDate = DateTime.Now; db.SaveChanges(); return true; } else { return false; } } }