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; }
// POST: api/Notes - ADD or UPDATE public JsonResult<ModificationResult<NoteDTO>> PostNote(NoteDTO note) { var result = ValidateModelState<NoteDTO>(); if (result != null) { return result; } if (noteManager.AddOrUpdateNote(note, User.Identity.Name)) { return Json(new ModificationResult<NoteDTO>(true) { Data = note }); } else { return Json(new ModificationResult<NoteDTO>("error: nie znaleziono uzytkownika (nie mozna dolaczyc tej notatki do uzytkownika o id: " + note.UserID + ")")); } }