public async Task <IActionResult> ModifyEntry(long id, [FromBody] DiaryEntryModifyModel modifyModel) { var result = await _diaryBusinessService.ModifyEntry(id, modifyModel); if (result.hasErrors) { return(BadRequest(result.errorMessage)); } return(await GetById(id)); }
/// <summary> /// Used to modify an existing diary entry /// </summary> public async Task <(bool hasErrors, string errorMessage, DiaryEntry result)> ModifyEntry(long id, DiaryEntryModifyModel modifyModel) { var entry = await _databaseContext .Set <DiaryEntry>() .Include(x => x.DiaryImages) .FirstOrDefaultAsync(x => x.Id == id && x.UserId == _userBusinessService.CurrentUserId); if (entry == null) { return(true, "ERROR_DIARY_ENTRY_NOT_FOUND", null); } entry.Description = modifyModel.Description; entry.EventAt = modifyModel.EventAt; entry.UpdateModified <DiaryEntry>(_userBusinessService.CurrentUserName); if (modifyModel.ShouldUpdateImages) { await UpdateDiaryImages(entry.DiaryImages, modifyModel.Images, entry.Id, _userBusinessService.CurrentUserName); } await _databaseContext.SaveChangesAsync(); return(false, null, entry); }
public async Task Should_Modify_Diary_Entry(bool shouldUpdateImages) { // Arrange await DatabaseContext.Database.EnsureDeletedAsync(); var diaryEntry = await _controller.CreateNewEntry(new DiaryEntryCreateModel { Description = "Testdescription", EventAt = new DateTime(2021, 1, 16, 10, 0, 0), Images = new List <DiaryImageCreateModel> { new DiaryImageCreateModel { ImageFileName = "Testimages One", ImageFile = new byte[] { 1, 2, 3 } }, new DiaryImageCreateModel { ImageFileName = "Testimages Two", ImageFile = new byte[] { 4, 5, 6, 7 } } } }); var modifyModel = new DiaryEntryModifyModel { Description = "New Description", EventAt = new DateTime(2021, 1, 16, 15, 15, 15), ShouldUpdateImages = shouldUpdateImages, Images = new List <DiaryImageModifyModel> { new DiaryImageModifyModel { ImageFileName = "New image", ImageFile = new byte[] { 1, 2, 3 } }, new DiaryImageModifyModel { ImageFileName = "Modified image", ImageFile = new byte[] { 4, 5, 6, 7 } }, new DiaryImageModifyModel { ImageFileName = "Removed image", ImageFile = new byte[] { 8, 9 } } } }; // Act var result = (OkObjectResult)await _controller.ModifyEntry(diaryEntry.Id, modifyModel); var savedDiaryEntry = (DiaryEntryViewModel)result.Value; // Assert Assert.AreEqual(modifyModel.Description, modifyModel.Description); Assert.AreEqual(modifyModel.EventAt, modifyModel.EventAt); var expectedmImageCount = shouldUpdateImages ? modifyModel.Images.Count() : savedDiaryEntry.Images.Count(); Assert.AreEqual(expectedmImageCount, modifyModel.Images.Count(), savedDiaryEntry.Images.Count()); if (shouldUpdateImages) { foreach (var savedImage in modifyModel.Images) { var foundImage = savedDiaryEntry.Images.First(x => x.ImageFileName == savedImage.ImageFileName); Assert.IsTrue(savedImage.ImageFile.SequenceEqual(foundImage.ImageFile)); } } else { foreach (var savedImage in diaryEntry.Images) { var foundImage = savedDiaryEntry.Images.First(x => x.ImageFileName == savedImage.ImageFileName); Assert.IsTrue(savedImage.ImageFile.SequenceEqual(foundImage.ImageFile)); } } var dbEntry = DatabaseContext .Set <DiaryEntry>() .FirstOrDefault(x => x.Id == savedDiaryEntry.Id); Assert.AreEqual(DateTime.Now.Year, dbEntry.CreatedAt.Year); Assert.AreEqual(DateTime.Now.Month, dbEntry.CreatedAt.Month); Assert.AreEqual(DateTime.Now.Day, dbEntry.CreatedAt.Day); Assert.AreEqual(_userBusinessService.CurrentUserName, dbEntry.CreatedBy); Assert.AreEqual(DateTime.Now.Year, dbEntry.ModifiedAt.Year); Assert.AreEqual(DateTime.Now.Month, dbEntry.ModifiedAt.Month); Assert.AreEqual(DateTime.Now.Day, dbEntry.ModifiedAt.Day); Assert.AreEqual(_userBusinessService.CurrentUserName, dbEntry.ModifiedBy); }
public async Task Should_Not_Get_Or_Modify_Diary_Entry_If_Different_User() { // Arrange await DatabaseContext.Database.EnsureDeletedAsync(); var currentUser = await DatabaseContext .Set <User>() .AddAsync(new User { UserName = "******" }); _userBusinessService.SetCurrentUser(currentUser.Entity); var diaryEntry = await _controller.CreateNewEntry(new DiaryEntryCreateModel { Description = "Testdescription", EventAt = new DateTime(2021, 1, 16, 10, 0, 0), Images = new List <DiaryImageCreateModel> { new DiaryImageCreateModel { ImageFileName = "Testimages One", ImageFile = new byte[] { 1, 2, 3 } }, new DiaryImageCreateModel { ImageFileName = "Testimages Two", ImageFile = new byte[] { 4, 5, 6, 7 } } } }); // Imitate that the diary entry belongs to someone else var otherUser = await DatabaseContext .Set <User>() .AddAsync(new User { UserName = "******" }); var dbEntry = await DatabaseContext.Set <DiaryEntry>().FirstOrDefaultAsync(x => x.Id == diaryEntry.Id); dbEntry.UserId = otherUser.Entity.Id; await DatabaseContext.SaveChangesAsync(); var modifyModel = new DiaryEntryModifyModel { Description = "New Description", EventAt = new DateTime(2021, 1, 16, 15, 15, 15) }; // Act var resultGet = (BadRequestObjectResult)await _controller.GetById(diaryEntry.Id); var resultModify = (BadRequestObjectResult)await _controller.ModifyEntry(diaryEntry.Id, modifyModel); var resultArchive = (BadRequestObjectResult)await _controller.ArchiveEntry(diaryEntry.Id); var resultUnarchive = (BadRequestObjectResult)await _controller.UnarchiveEntry(diaryEntry.Id); var resultDelete = (BadRequestObjectResult)await _controller.DeleteEntry(diaryEntry.Id); // Assert Assert.IsNotNull(resultGet); Assert.AreEqual("ERROR_DIARY_ENTRY_NOT_FOUND", resultGet.Value); Assert.IsNotNull(resultModify); Assert.AreEqual("ERROR_DIARY_ENTRY_NOT_FOUND", resultModify.Value); Assert.IsNotNull(resultArchive); Assert.AreEqual("ERROR_DIARY_ENTRY_NOT_FOUND", resultArchive.Value); Assert.IsNotNull(resultUnarchive); Assert.AreEqual("ERROR_DIARY_ENTRY_NOT_FOUND", resultUnarchive.Value); Assert.IsNotNull(resultDelete); Assert.AreEqual("ERROR_DIARY_ENTRY_NOT_FOUND", resultDelete.Value); }