public async Task Update_ReturnsBadRequestAndProperContentTypeDoesntAddAndUpdate_ForDeletedNote() { // Arrange var client = _factory.CreateClient(); var note = new NoteForUpdatingDto() { Title = "title X", Content = "content X" }; var noteJson = JsonSerializer.Serialize(note); var httpContent = new StringContent(noteJson, Encoding.UTF8); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // Act var response = await client.PutAsync(Consts.deletedNoteUrl, httpContent); // Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.Equal("text/plain; charset=utf-8", response.Content.Headers.ContentType.ToString()); var deletedNote = await _dbContext.FindAsync <Note>(Consts.deletedNoteId); Assert.True(deletedNote.Title != "title X"); var newNote = await _dbContext.FindAsync <Note>(Consts.newNoteId); Assert.True(newNote == null); }
public async Task <IActionResult> Update(int id, NoteForUpdatingDto noteDto) { var noteToUpdate = await _repo.GetById <Note>(id); if (noteToUpdate == null || noteToUpdate.Deleted == true) { return(BadRequest(nonexistentNoteMessage)); } // If this is the first time updating a note, we fill its OriginalNoteId with its own Id. // Doing this when adding would require to make another call to db to retrieve it and another to save it again. if (noteToUpdate.OriginalNoteId == 0) { noteToUpdate.OriginalNoteId = noteToUpdate.Id; } noteToUpdate.Modified = DateTime.Now; _repo.Update(noteToUpdate); var noteToAdd = _mapper.Map <Note>(noteDto); var latestVersion = await _repo.GetLatestById(id); noteToAdd.Version = latestVersion.Version + 1; noteToAdd.OriginalNoteId = noteToUpdate.OriginalNoteId; _repo.Add(noteToAdd); return(Ok()); }
public async Task Update_ReturnsOkUpdatesAndAddsToDb_ForProperNote() { // Arrange var client = _factory.CreateClient(); var note = new NoteForUpdatingDto() { Title = "title Y", Content = "content Y" }; var noteJson = JsonSerializer.Serialize(note); var httpContent = new StringContent(noteJson, Encoding.UTF8); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // Act var response = await client.PutAsync(Consts.properIdUrl, httpContent); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); var newNote = await _dbContext.FindAsync <Note>(Consts.newNoteId); Assert.True(newNote != null); Assert.Equal(3, newNote.Version); // The highest version of this note in test db is 2 var updatedNote = await _dbContext.FindAsync <Note>(1); Assert.Equal(updatedNote.Modified.GetDateTimeFormats('G').FirstOrDefault(), newNote.Created.GetDateTimeFormats('G').FirstOrDefault()); // G format = 15/06/2009 13:45:30 }