public bool Edit(CreateNoteDTO note, int userId) { if (note.Id > 0) { string title = note.Title.Trim().ToLowerInvariant(); var dbNote = context.Notes.Where(wh => wh.Title == title && wh.Id != note.Id).FirstOrDefault(); if (dbNote == null) { var noteTobeEdit = (from notes in context.Notes join noteSharing in context.NoteSharing on notes.Id equals noteSharing.NoteId where noteSharing.UserId == userId && noteSharing.Edit select notes).FirstOrDefault(); if (noteTobeEdit != null) { noteTobeEdit.Title = note.Title; noteTobeEdit.Description = note.Description; noteTobeEdit.Content = note.Content; context.SaveChangesAsync(); return(true); } } } return(false); }
public bool TryCreateNote(ref CreateNoteDTO newNote, int userId) { try { string title = newNote.Title.Trim().ToLowerInvariant(); if (context.Notes.FirstOrDefault(x => x.Title.ToLowerInvariant() == title) != null) { return(false); } var dbNote = new Notes() { Title = title, Description = newNote.Description.Trim(), Content = newNote.Content, CreatedBy = userId }; context.Notes.Add(dbNote); context.NoteSharing.Add(new NoteSharing() { NoteId = dbNote.Id, UserId = userId, Delete = true, Edit = true, Read = true, Share = true }); context.SaveChangesAsync(); } catch (Exception ex) { throw; } return(true); }
[Route("EditNote")]// api/notes/EditNote public IActionResult EditNote([FromBody] CreateNoteDTO editNote) { string message = string.Empty; var isUpdated = _noteService.Edit(editNote, GetUserId()); if (!isUpdated) { return(BadRequest(new { message = "Cannot update a Note!" })); } return(Ok(true)); }
public IActionResult CreateNote([FromBody] CreateNoteDTO createNoteDTO) { if (createNoteDTO == null) { return(BadRequest(ModelState)); } var noteObj = _mapper.Map <Note>(createNoteDTO); if (!_noteRepo.CreateNote(noteObj)) { ModelState.AddModelError("", $"Something went wrong when saving the record {noteObj.Title}"); return(StatusCode(500, ModelState)); } return(CreatedAtRoute("GetNote", new { noteId = noteObj.Id }, noteObj)); }
[Route("CreateNote")]// api/notes/CreateNote public IActionResult CreateNote([FromBody] CreateNoteDTO newNote) { string message = string.Empty, operationMessage = string.Empty; bool isSucceeded = false; if (newNote.Id > 0) { isSucceeded = _noteService.Edit(newNote, GetUserId()); operationMessage = "Can not update note!"; } else { isSucceeded = _noteService.TryCreateNote(ref newNote, GetUserId()); operationMessage = "Can not create a note!"; } if (!isSucceeded) { return(BadRequest(new { message = operationMessage })); } return(Ok(true)); }