Exemple #1
0
        /*[HttpPost]
         * [ValidateAntiForgeryToken]
         * public async Task<IActionResult> Wypozycz(int id, [Bind("UserID,Imie,Nazwisko,Zapisany_od,Books")] User user, int[] selectedBooks,string searchString,string currentFilter)
         * {
         *  if (id != user.UserID)
         *  {
         *      return NotFound();
         *  }
         *  ViewData["CurrentFilter"] = searchString;
         *  if (searchString==null)
         *  {
         *      searchString = currentFilter;
         *  }
         *  user = await _context.Users
         *      .Include(u => u.Books)
         *          .ThenInclude(b=>b.Autor)
         *      .AsNoTracking()
         *      .SingleOrDefaultAsync(m => m.UserID == id);
         *
         *  if (ModelState.IsValid)
         *  {
         *      UpdateUserBooks(user, selectedBooks);
         *      try
         *      {
         *          _context.Update(user);
         *          await _context.SaveChangesAsync();
         *      }
         *      catch (DbUpdateConcurrencyException)
         *      {
         *          if (!UserExists(user.UserID))
         *          {
         *              return NotFound();
         *          }
         *          else
         *          {
         *              throw;
         *          }
         *      }
         *
         *      return RedirectToAction(nameof(Wypozycz));
         *  }
         *  PopulateBookDropdownList(searchString);
         *  return View(user);
         * }*/
        private void UpdateUserBooks(User userToUpdate, int[] selectedBooks)
        {
            if (selectedBooks == null)
            {
                userToUpdate.Books = new List <Book>();
                return;
            }
            var selectedBooksHS = new HashSet <int>(selectedBooks);
            var currentBooks    = new HashSet <int>
                                      (userToUpdate.Books.Select(b => b.BookID));

            foreach (var book in _context.Books.Include(a => a.Autor).AsNoTracking())
            {
                if (selectedBooksHS.Contains(book.BookID))
                {
                    if (currentBooks.Contains(book.BookID))
                    {
                        Book removedBook = userToUpdate.Books.SingleOrDefault(b => b.BookID == book.BookID);
                        _context.Remove(removedBook);
                        _context.Books.Add(new Book {
                            Tytul = book.Tytul, AuthorID = book.AuthorID, Autor = book.Autor, Gatunek = book.Gatunek
                        });
                        //userToUpdate.Books.Remove(removedBook);
                        //_context.Update(removedBook);
                    }
                    else
                    {
                        userToUpdate.Books.Add(new Book {
                            BookID = book.BookID, Tytul = book.Tytul, AuthorID = book.AuthorID, Autor = book.Autor, Gatunek = book.Gatunek
                        });
                    }
                }
            }
        }