public async Task <IActionResult> UpdateNote(int id, NoteRequestDto noteDto) { if (ModelState.IsValid) { var existingNote = await _context.Notes.FirstOrDefaultAsync(x => x.NoteId == id); if (existingNote is null) { return(NotFound()); } var userId = this.User.Claims.First(i => i.Type == "Id").Value; if (existingNote.UserId != userId) { return(Unauthorized()); } else { existingNote.Title = noteDto.Title; existingNote.Description = noteDto.Description; await _context.SaveChangesAsync(); return(NoContent()); } } return(new JsonResult("Something went wrong") { StatusCode = 500 }); }
public async Task <NoteResponseDto> UpdateNote(int userId, int noteId, NoteRequestDto noteToUpdate) { Note note = await _repository.GetNoteByNoteIdAndUserId(noteId, userId); if (note == null) { return(null); } return(_mapper.Map <NoteResponseDto>(await _repository.UpdateNote(_mapper.Map <Note>(noteToUpdate), note))); }
public async Task <NoteResponseDto> AddNote(NoteRequestDto note, int userid, string email) { Note noteModel = _mapper.Map <Note>(note); noteModel.AccountId = userid; if (noteModel.Image != null && noteModel.Image.Length > 0) { noteModel.Image = await _cloudService.UpdloadToCloud(note.Image, email); } return(_mapper.Map <NoteResponseDto>(await _repository.AddNote(noteModel))); }
public async Task <IActionResult> AddNoteASync([FromForm] NoteRequestDto note) { int userId = Convert.ToInt32(HttpContext.Items["userId"]); string email = (string)HttpContext.Items["email"]; var addednote = await _service.AddNote(note, userId, email); return(Ok(new { Data = addednote, StatusCode = (int)HttpStatusCode.Created, Message = ResponseMessages.CREATED })); }
public async Task <IActionResult> UpdateNoteAsync(int noteId, [FromBody] NoteRequestDto note) { int userId = Convert.ToInt32(HttpContext.Items["userId"]); var updatedNote = await _service.UpdateNote(userId, noteId, note); if (updatedNote == null) { return(NotFound(new { Data = (string)null, StatusCode = (int)HttpStatusCode.NotFound, Message = ResponseMessages.NO_SUCH_NOTES })); } return(Ok(new { Data = updatedNote, StatusCode = (int)HttpStatusCode.OK, Message = ResponseMessages.UPDATED })); }
public async Task <IActionResult> CreateNote(NoteRequestDto noteDto) { if (ModelState.IsValid) { var newNote = new Note() { Title = noteDto.Title, Description = noteDto.Description, UserId = this.User.Claims.First(i => i.Type == "Id").Value }; var createdNote = await _context.AddAsync(newNote); await _context.SaveChangesAsync(); // return CreatedAtAction("GetNote", new { createdNote.Entity.NoteId }, createdNote); return(CreatedAtAction("GetNote", new { id = createdNote.Entity.NoteId }, createdNote.Entity)); } return(new JsonResult("Something went wrong") { StatusCode = 500 }); }