public PartialViewResult SetRating(Rating rating) { Rating existed = context.Ratings.FirstOrDefault(r => r.BookId == rating.BookId && r.UserId == rating.UserId); if (existed == null) { context.Ratings.Add(rating); } else { existed.Value = rating.Value; } context.SaveChanges(); HomeIndexViewModel viewModel = new HomeIndexViewModel { Books = context.Books.ToList(), Recommendations = CollaborativeFilter.GetRecomendations(context, User.Identity.GetUserId()), Genres = context.Genres.ToList(), Countries = context.Countries.ToList(), FilterModel = FilterModel ?? new FilterModel() }; return(PartialView("BooksRecomendationsPartial", viewModel)); }
public ActionResult CreateBook(CreateBookModel createBookModel) { try { List <Genre> genres = createBookModel.CheckedGenreObjects.Where(genre => genre.Checked).Select(genre => context.Genres.First(g => g.Id == genre.Id)).ToList(); Book book = new Book { Author = createBookModel.Author, PageCount = createBookModel.PageCount, Year = createBookModel.Year, CountryId = createBookModel.CountryId, Description = createBookModel.Description, Name = createBookModel.Name, PurchaseUrl = createBookModel.PurchaseUrl, Genres = genres }; string fileName = Guid.NewGuid().ToString() + Path.GetExtension(createBookModel.File.FileName); string path = Path.Combine(Server.MapPath("~/Content/BookImages"), fileName); createBookModel.File.SaveAs(path); book.ImageUrl = "../Content/BookImages/" + fileName; context.Books.Add(book); context.SaveChanges(); } catch { } return(RedirectToAction("Books")); }
public static void SetupContext( DbContextOptions <ELibraryContext> options, Fixture fixture) { using var context = new ELibraryContext(options); int userId1 = fixture.Create <int>(); int userId2 = fixture.Create <int>(); User user1 = fixture.Build <User>() .With(x => x.Id, userId1) .Without(x => x.FavoriteBooks) .Without(x => x.BooksOnLoan) .Create(); User user2 = fixture.Build <User>() .With(x => x.Id, userId2) .Without(x => x.BooksOnLoan) .Without(x => x.FavoriteBooks) .Create(); string isbn1 = fixture.Create <string>(); string isbn2 = fixture.Create <string>(); string isbn3 = fixture.Create <string>(); string isbn4 = fixture.Create <string>(); Book book1 = fixture.Build <Book>() .With(x => x.ISBN, isbn1) .Without(x => x.BooksOnLoan) .Without(x => x.UseFavoriteBooks) .Create(); Book book2 = fixture.Build <Book>() .With(x => x.ISBN, isbn2) .Without(x => x.BooksOnLoan) .Without(x => x.UseFavoriteBooks) .Create(); Book book3 = fixture.Build <Book>() .With(x => x.ISBN, isbn3) .Without(x => x.BooksOnLoan) .Without(x => x.UseFavoriteBooks) .Create(); Book book4 = fixture.Build <Book>() .With(x => x.ISBN, isbn4) .Without(x => x.BooksOnLoan) .Without(x => x.UseFavoriteBooks) .Create(); context.Users.AddRange(user1, user2); context.SaveChanges(); }