public async Task <ActionResult <Note> > Post([FromRoute] string username, [FromBody] Note note) { if (string.IsNullOrWhiteSpace(username)) { return(BadRequest("Username is required")); } if (string.IsNullOrWhiteSpace(note.Id)) { note.CreatedAt = DateTime.UtcNow; _logger.LogInformation($"Creating new note for user '{username}'"); } else { // check the owner of the note Note originalNote = await _noteStorageService.GetNote(username, note.Id); if (originalNote == null) { _logger.LogWarning($"Couldn't find note with ID '{note.Id}' for user '{username}'"); return(NotFound()); } _logger.LogInformation($"Saving changes to existing note with ID '{note.Id}' for user '{username}'"); } // reset the user name as we were not displaying it on the page on purpose note.UserId = username; await _noteStorageService.SaveNote(note); return(note); }
public async Task <IActionResult> Edit(Note note) { EventType eventType = EventType.NoteEdited; if (string.IsNullOrWhiteSpace(note.Id)) { //note.Id = Guid.NewGuid(); note.CreatedAt = DateTime.UtcNow; eventType = EventType.NoteCreated; _logger.LogInformation($"Creating new note for user '{User.Identity.Name}'"); } else { // check the owner of the note Note originalNote = await _noteStorageService.GetNote(User.Identity.Name, note.Id); if (originalNote == null) { _logger.LogWarning($"Couldn't find note with ID '{note.Id}' for user '{User.Identity.Name}'"); return(NotFound()); } _logger.LogInformation($"Saving changes to existing note with ID '{note.Id}' for user '{User.Identity.Name}'"); } // reset the user name as we were not displaying it on the page on purpose note.UserId = User.Identity.Name; await _noteStorageService.SaveNote(note); _eventPublisher.PublishEvent(new Event { EventType = eventType, TimeStamp = DateTime.UtcNow, Username = User.Identity.Name, NoteId = note.Id }); return(RedirectToAction("List")); }