public async Task <IActionResult> Update(CreatingViewModel model) { if (ModelState.IsValid) { await _repository.UpdateReadingStatus(model, HttpContext.User); } return(Redirect("/Home/UserPage")); }
public async void UpsertHomeControllerTest() { //Arrange var context = _serviceProvider.GetService <Context>(); Book fakeBook = new() { Author = "Kyryl Halmiz", Genre = "Fiction", Pages = 500, Title = "Robots war" }; await context.AddAsync(fakeBook); await context.SaveChangesAsync(); //Act HomeController controller = new HomeController(new Repository(context, _userManager)); controller.ControllerContext = new ControllerContext(); var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, "example name"), new Claim(ClaimTypes.PrimarySid, "1"), new Claim("custom-claim", "example claim value"), }, "mock")); controller.ControllerContext.HttpContext = new DefaultHttpContext() { User = user }; ViewResult result = await controller.Upsert(fakeBook.Isbn) as ViewResult; CreatingViewModel model = (CreatingViewModel)result.Model; context.Remove(fakeBook); context.SaveChanges(); //Assert Assert.NotNull(result); Assert.Equal(model.Book.Author, fakeBook.Author); Assert.Equal(model.Book.Title, fakeBook.Title); Assert.Equal(model.Book.Genre, fakeBook.Genre); Assert.Null(model.Status); Assert.IsType <CreatingViewModel>(model); } }
public async Task UpdateReadingStatus(CreatingViewModel model, ClaimsPrincipal claims) { var currentUser = await GetCurrentUserAsync(claims); var userId = currentUser?.Id; Book bookfromDB = await GetBookById(model.Book.Isbn); ReadingStatus status = (await GetBookReadingStatus(model.Book.Isbn, claims)); if (bookfromDB is null) { await _context.AddAsync(model.Book); } else { status.PagesRead = model.Status.PagesRead; status.Status = model.Status.Status; } await _context.SaveChangesAsync(); }