public ActionResult EditRole(RolesEditRoleVM model, string[] assignedAuthenticatingActions)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();

            Role role = null;
            if (!this.ModelState.IsValid)
            {
                AuthenticatingActionsRepository authenticatingActionsRepository = new AuthenticatingActionsRepository(context);

                var authenticatingActions = authenticatingActionsRepository.GetAll();
                List<AuthenticatingActionsVM> authenticatingActionsViewModel = new List<AuthenticatingActionsVM>();

                foreach (var action in authenticatingActions)
                {
                    authenticatingActionsViewModel.Add(new AuthenticatingActionsVM
                    {
                        ID = action.ID,
                        Name = action.Name,
                        IsAssigned = false
                    });
                }

                ViewBag.AuthenticatingActions = authenticatingActionsViewModel;
                return View(model);
            }

            using (UnitOfWork unitOfWork = new UnitOfWork(context))
            {
                try
                {
                    var authenticatingActionsRepository = new AuthenticatingActionsRepository(unitOfWork);

                    var rolesRepository = new RolesRepository(unitOfWork);
                    if (model.ID > 0)
                    {
                        role = rolesRepository.GetAll(filter: r => r.ID == model.ID, includeProperties: "AuthenticatingActions").FirstOrDefault();
                    }
                    else
                    {
                        role = new Role();
                        role.AuthenticatingActions = new List<AuthenticatingAction>();
                    }

                    role.Name = model.Name;

                    UpdateAuthenticatingActions(assignedAuthenticatingActions, role, authenticatingActionsRepository);
                    rolesRepository.Save(role);
                    PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository);

                    unitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    unitOfWork.RollBack();
                    throw ex;
                }
            }

            return RedirectToAction("Index", "Roles");
        }
 public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     LibraryManagementSystemContext context = new LibraryManagementSystemContext();
     RolesRepository rolesRepository = new RolesRepository(context);
     if (AuthenticationManager.LoggedUser == null)
     {
         filterContext.HttpContext.Response.Redirect("~/?RedirectUrl=" + filterContext.HttpContext.Request.Url);
         filterContext.Result = new EmptyResult();
         return;
     }
     else
     {
         foreach (var role in AuthenticationManager.LoggedUser.Roles)
         {
             if (!rolesRepository.Exists(role.ID, filterContext.RouteData.Values["Controller"].ToString(), filterContext.RouteData.Values["Action"].ToString()))
             {
                 filterContext.HttpContext.Response.Redirect("~/");
             }
             else
             {
                 base.OnActionExecuting(filterContext);
             }
         }
     }
 }
        public ActionResult EditBarcode(BarcodesEditBarcodeVM model)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            BarcodesRepository barcodesRepository = new BarcodesRepository(context);

            Barcode barcode = null;
            if (barcodesRepository.GetAll().Any(b => b.BarcodeNumber == model.BarcodeNumber) &&
                model.ID != barcodesRepository.GetAll(filter: b => b.BarcodeNumber == model.BarcodeNumber).FirstOrDefault().ID)
            {
                ModelState.AddModelError("BarcodeNumber", "* barcode already exists");
            }
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            else
            {
                if (model.ID > 0)
                {
                    barcode = barcodesRepository.GetByID(model.ID);
                }
                else
                {
                    barcode = new Barcode();
                }

                barcode.ID = model.ID;
                barcode.BookID = model.BookID;
                barcode.BarcodeNumber = model.BarcodeNumber;

                barcodesRepository.Save(barcode);
            }

            return RedirectToAction("ListBookBarcodes/" + barcode.BookID, "Books");
        }
        public ActionResult EditCustomer(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            CustomersRepository customersRepository = new CustomersRepository(context);
            CustomersEditCustomerVM model = new CustomersEditCustomerVM();

            Customer customer = customersRepository.GetByID(id);
            if (id > 0)
            {
                model.ID = customer.ID;
                model.PersonalNumber = customer.PersonalNumber;
                model.FirstName = customer.FirstName;
                model.LastName = customer.LastName;
                model.Email = customer.Email;
                model.Address = customer.Address;
                model.PicturePath = customer.PicturePath;
                model.Birthday = customer.Birthday;
                model.DateIn = customer.DateIn;
                if (customer.DateOut != null)
                {
                    model.DateOut = customer.DateOut.Value;
                }
            }
            else
            {
                customer = new Customer();
                model.DateOut = null;
            }

            return View(model);
        }
        public bool AuthenticateUser(string email, string password)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            UsersRepository usersRepository = new UsersRepository(context);

            LoggedUser = usersRepository.GetAll(filter: u => u.Email == email && u.Password == password).FirstOrDefault();
            return LoggedUser != null;
        }
        public ActionResult Details(int id, string sortOrder)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            PublishersRepository publishersRepository = new PublishersRepository(context);
            BooksRepository booksRepository = new BooksRepository(context);
            PublishersDetailsVM model = new PublishersDetailsVM();
            this.TryUpdateModel(model);

            var publisher = publishersRepository.GetByID(id);
            if (publisher != null)
            {
                model.ID = publisher.ID;
                model.PublisherName = publisher.Name;
                model.Address = publisher.Address;
                model.BooksPager = model.BooksPager ?? new GenericPagerVM();
                model.BooksPager.CurrentPage = model.BooksPager.CurrentPage == 0 ? 1 : model.BooksPager.CurrentPage;
                model.BooksPager.Action = "Details";
                model.BooksPager.Controller = "Publishers";
                model.BooksPager.Prefix = "BooksPager";
                model.BooksPager.CurrentParameters = new Dictionary<string, object>()
                {
                    { "BookTitle", model.BookTitle },
                    { "BooksPager.CurrentPage", model.BooksPager.CurrentPage }
                };

                #region Sorting and Filtering

                Expression<Func<Book, bool>> filter = b =>
                            (string.IsNullOrEmpty(model.BookTitle) || b.Title.Contains(model.BookTitle));
                model.BooksPager.PagesCount = GetPagesCount(filter);

                ViewBag.BookSortParam = string.IsNullOrEmpty(sortOrder) ? "book_desc" : "";
                switch (sortOrder)
                {
                    case "book_desc":
                        model.Books = booksRepository
                            .GetAll(model.BooksPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, x => x.OrderByDescending(b => b.Title))
                            .Where(b => b.PublisherID == id)
                            .ToList();
                        break;
                    default:
                        model.Books = booksRepository
                           .GetAll(model.BooksPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, x => x.OrderBy(b => b.Title))
                           .Where(b => b.PublisherID == id)
                           .ToList();
                        break;
                }
                #endregion

                return View(model);
            }
            else
            {
                return RedirectToAction("Index", "Publishers");
            }
        }
        public ActionResult DeleteBarcode(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            BarcodesRepository barcodesRepository = new BarcodesRepository(context);
            BarcodesDeleteBarcodeVM model = new BarcodesDeleteBarcodeVM();

            Barcode barcode = barcodesRepository.GetByID(id);
            model.ID = barcode.ID;
            model.BarcodeNumber = barcode.BarcodeNumber;

            return View(model);
        }
        public ActionResult DeleteRole(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository = new RolesRepository(context);

            RolesDeleteRoleVM model = new RolesDeleteRoleVM();
            Role role = rolesRepository.GetAll(filter: r => r.ID == id, includeProperties: "AuthenticatingActions").FirstOrDefault();
            model.ID = role.ID;
            model.Name = role.Name;

            return View(model);
        }
        public ActionResult DeleteBook(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            BooksRepository booksRepository = new BooksRepository(context);
            BooksDeleteBookVM model = new BooksDeleteBookVM();

            Book book = booksRepository.GetByID(id);

            model.ID = book.ID;
            model.Title = book.Title;

            return View(model);
        }
        public ActionResult DeleteUser(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            UsersRepository usersRepository = new UsersRepository(context);
            UsersDeleteUserVM model = new UsersDeleteUserVM();

            User user = usersRepository.GetByID(id);

            model.ID = user.ID;
            model.FullName = user.ToString();

            return View(model);
        }
        public ActionResult Index()
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            UsersRepository usersRepo = new UsersRepository(context);
            HomeIndexVM model = new HomeIndexVM();
            TryUpdateModel(model);

            if (AuthenticationManager.LoggedUser != null)
            {
                model.ID = AuthenticationManager.LoggedUser.ID;
            }

            return View(model);
        }
        public ActionResult EditRent()
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            BooksRepository booksRepository = new BooksRepository(context);
            CustomersRepository customersRepository = new CustomersRepository(context);
            RentsEditRentVM model = new RentsEditRentVM();

            model.Customers = customersRepository.GetAll();
            model.Books = booksRepository.GetAll();
            model.RentDate = DateTime.Now.Date;
            model.UserID = AuthenticationManager.LoggedUser.ID;

            return View(model);
        }
        public ActionResult DeleteUser(UsersDeleteUserVM model)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            UsersRepository usersRepository = new UsersRepository(context);

            User user = usersRepository.GetAll(filter: u => u.ID == model.ID, includeProperties: "Roles").FirstOrDefault();
            if (user == null)
            {
                return HttpNotFound();
            }

            user.Roles = null;
            usersRepository.Delete(user);

            return RedirectToAction("Index", "Users");
        }
        public ActionResult AddBookAuthor(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            BooksRepository booksRepository = new BooksRepository(context);
            AuthorsRepository authorsRepository = new AuthorsRepository(context);
            BooksAddBookAuthorVM model = new BooksAddBookAuthorVM();

            Book book = booksRepository.GetByID(id);
            model.ID = book.ID;
            model.Title = book.Title;
            model.Authors = model.Authors ?? new List<SelectListItem>();
            model.Authors = SelectListHandler.Create<Author>(
                authorsRepository.GetAll(), a => (a.FirstName + " " + a.LastName), a => a.ID.ToString(), model.AuthorID.ToString());

            return View(model);
        }
        public ActionResult DeleteRole(RolesDeleteRoleVM model)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository = new RolesRepository(context);

            Role role = rolesRepository.GetAll(filter: r => r.ID == model.ID, includeProperties: "AuthenticatingActions").FirstOrDefault();
            if (role == null)
            {
                return HttpNotFound();
            }

            role.AuthenticatingActions = null;
            rolesRepository.Delete(role);

            return RedirectToAction("Index", "Roles");
        }
        public ActionResult DeleteBarcode(BarcodesDeleteBarcodeVM model)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            BarcodesRepository barcodesRepository = new BarcodesRepository(context);

            Barcode barcode = barcodesRepository.GetByID(model.ID);
            if (barcode == null)
            {
                return HttpNotFound();
            }
            else
            {
                barcodesRepository.Delete(barcode);
            }

            return RedirectToAction("ListBookBarcodes/" + barcode.BookID, "Books");
        }
        public ActionResult DeleteBook(BooksDeleteBookVM model)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            BooksRepository booksRepository = new BooksRepository(context);

            Book book = booksRepository.GetByID(model.ID);
            if (book == null)
            {
                return HttpNotFound();
            }
            else
            {
                booksRepository.Delete(book);
            }

            return RedirectToAction("Index");
        }
        public ActionResult EditCustomer(CustomersEditCustomerVM model)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            CustomersRepository customersRepository = new CustomersRepository(context);

            ModelState.Remove("DateOut");

            if (model.Email != null && customersRepository.GetAll().Any(c => c.Email == model.Email) &&
                model.ID != customersRepository.GetAll(filter: c => c.Email == model.Email).FirstOrDefault().ID)
            {
                ModelState.AddModelError("Email", "* email already exists");
            }
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            else
            {
                Customer customer = null;
                if (model.ID > 0)
                {
                    customer = customersRepository.GetByID(model.ID);
                    customer.PersonalNumber = model.PersonalNumber;
                }
                else
                {
                    customer = new Customer();
                    customer.PersonalNumber = customersRepository.GetAll().LastOrDefault().PersonalNumber + 1;
                }

                customer.ID = model.ID;
                customer.FirstName = model.FirstName;
                customer.LastName = model.LastName;
                customer.Email = model.Email;
                customer.Address = model.Address;
                customer.Birthday = model.Birthday;
                customer.DateIn = model.DateIn;
                customer.DateOut = model.DateOut != null ? model.DateOut : null;

                customersRepository.Save(customer);
            }

            return RedirectToAction("Index", "Customers");
        }
        public ActionResult EditPublisher(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            PublishersRepository publishersRepository = new PublishersRepository(context);
            PublishersEditPublisherVM model = new PublishersEditPublisherVM();

            Publisher publisher = publishersRepository.GetByID(id);
            if (id > 0)
            {
                if (publisher == null)
                {
                    return RedirectToAction("Index", "Publishers");
                }

                model.ID = publisher.ID;
                model.Name = publisher.Name;
                model.Address = publisher.Address;
            }

            return View(model);
        }
        public ActionResult EditBarcode(int id, int bookID)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            BarcodesRepository barcodesRepository = new BarcodesRepository(context);
            BooksRepository booksRepository = new BooksRepository(context);
            BarcodesEditBarcodeVM model = new BarcodesEditBarcodeVM();

            Barcode barcode = barcodesRepository.GetByID(id);
            if (id > 0)
            {
                if (barcode == null)
                {
                    barcode.BookID = bookID;
                }

                model.ID = barcode.ID;
                model.BookID = barcode.BookID;
                model.BarcodeNumber = barcode.BarcodeNumber;
            }

            return View(model);
        }
        public ActionResult EditUser(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            UsersRepository usersRepository = new UsersRepository(context);
            RolesRepository rolesRepository = new RolesRepository(context);
            UsersEditUserVM model = new UsersEditUserVM();

            if (id > 0)
            {
                User user = usersRepository.GetAll(filter: u => u.ID == id,
                    includeProperties: "Roles").FirstOrDefault();

                PopulateAssignedRoles(user, rolesRepository);

                model.ID = user.ID;
                model.PersonalNumber = user.PersonalNumber;
                model.Password = user.Password;
                model.FirstName = user.FirstName;
                model.Email = user.Email;
                model.Address = user.Address;
                model.LastName = user.LastName;
                model.Birthday = user.Birthday;
                model.DateIn = user.DateIn;
                if (user.DateOut != null)
                {
                    model.DateOut = user.DateOut.Value;
                }
            }
            else
            {
                model.Email = " ";

                User user = new User();
                user.Roles = new List<Role>();
                PopulateAssignedRoles(user, rolesRepository);
            }

            return View(model);
        }
        public ActionResult AddBookAuthor(BooksAddBookAuthorVM model)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            BooksRepository booksRepository = new BooksRepository(context);
            AuthorsRepository authorsRepository = new AuthorsRepository(context);

            Book book = null;
            Author author = null;
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            else
            {
                book = booksRepository.GetByID(model.ID);
                author = authorsRepository.GetByID(model.AuthorID);

                book.Authors.Add(author);
                booksRepository.Save(book);
            }

            return RedirectToAction("Details/" + model.ID, "Books");
        }
        public ActionResult EditRole(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository = new RolesRepository(context);
            AuthenticatingActionsRepository authenticatingActionsRepository = new AuthenticatingActionsRepository(context);

            RolesEditRoleVM model = new RolesEditRoleVM();
            if (id > 0)
            {
                Role role = rolesRepository.GetAll(filter: r => r.ID == id, includeProperties: "AuthenticatingActions").FirstOrDefault();
                PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository);

                model.ID = role.ID;
                model.Name = role.Name;
            }
            else
            {
                Role role = new Role();
                role.AuthenticatingActions = new List<AuthenticatingAction>();
                PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository);
            }

            return View(model);
        }
        public void PopulateAssignedAuthenticatingActions(Role role, AuthenticatingActionsRepository authenticatingActionsRepository)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository = new RolesRepository(context);

            var authenticatingActions = authenticatingActionsRepository.GetAll();
            var roleAuthenticatingActions = new HashSet<int>(role.AuthenticatingActions.Select(r => r.ID));
            var model = new List<AuthenticatingActionsVM>();

            foreach (var action in authenticatingActions)
            {
                model.Add(new AuthenticatingActionsVM
                {
                    ID = action.ID,
                    Name = action.Name,
                    IsAssigned = roleAuthenticatingActions.Contains(action.ID)
                });
            }

            ViewBag.AuthenticatingActions = model;
        }
        public ActionResult Index()
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository = new RolesRepository(context);
            RolesIndexVM model = new RolesIndexVM();
            this.TryUpdateModel(model);

            model.RolesPager = model.RolesPager ?? new GenericPagerVM();
            model.RolesPager.CurrentPage = model.RolesPager.CurrentPage == 0 ? 1 : model.RolesPager.CurrentPage;

            model.RolesPager.PagesCount = this.GetPagesCount();
            model.RolesList = rolesRepository.GetAll(model.RolesPager.CurrentPage, ApplicationConfiguration.ItemsPerPage);

            model.RolesPager.Prefix = "RolesPager";
            model.RolesPager.Action = "Index";
            model.RolesPager.Controller = "Roles";
            model.RolesPager.CurrentParameters = new Dictionary<string, object>()
            {
                {"RolesPager.CurrentPage",model.RolesPager.CurrentPage}
            };

            return View(model);
        }
        public int GetPagesCount()
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            RolesRepository rolesRepository = new RolesRepository(context);

            int pagesCount = 0;
            int rolesCount = rolesRepository.Count();
            int itemsPerPage = ApplicationConfiguration.ItemsPerPage;
            pagesCount = rolesCount / itemsPerPage;
            if ((rolesCount % itemsPerPage) > 0)
            {
                pagesCount++;
            }
            return pagesCount;
        }
        public ActionResult EditUser(UsersEditUserVM model, string[] assignedRoles)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();

            ModelState.Remove("DateOut");

            UsersRepository usersRepositoryContext = new UsersRepository(context);
            if (model.Email != null && usersRepositoryContext.GetAll().Any(u => u.Email == model.Email) &&
                model.ID != usersRepositoryContext.GetAll(filter: u => u.Email == model.Email).FirstOrDefault().ID)
            {
                ModelState.AddModelError("Email", "* email already exists");
            }
            if (model.ID <= 0 && string.IsNullOrEmpty(model.Password))
            {
                this.ModelState.AddModelError("Password", "* password required");
            }
            if (!this.ModelState.IsValid)
            {
                RolesRepository rolesRepository = new RolesRepository(context);
                var allRoles = rolesRepository.GetAll();

                List<AssignedRolesVM> assignedRolesViewModel = new List<AssignedRolesVM>();
                foreach (var role in allRoles)
                {
                    assignedRolesViewModel.Add(new AssignedRolesVM
                        {
                            ID = role.ID,
                            Name = role.Name,
                            IsAssigned = false
                        });
                }

                ViewBag.Roles = assignedRolesViewModel;
                return View(model);
            }

            TryUpdateModel(model);

            User user = null;
            using (UnitOfWork unitOfWork = new UnitOfWork(context))
            {
                try
                {
                    UsersRepository usersRepositoryUnitOfWork = new UsersRepository(unitOfWork);
                    RolesRepository rolesRepository = new RolesRepository(unitOfWork);

                    if (model.ID > 0)
                    {
                        user = usersRepositoryUnitOfWork.GetAll(filter: u => u.ID == model.ID, includeProperties: "Roles").FirstOrDefault();
                        user.PersonalNumber = model.PersonalNumber;
                    }
                    else
                    {
                        user = new User();
                        user.Roles = new List<Role>();
                        user.PersonalNumber = usersRepositoryUnitOfWork.GetAll().LastOrDefault().PersonalNumber + 1;
                    }

                    user.Password =
                        (model.Password != null) && (model.Password.Trim() != String.Empty) ? model.Password.Trim() : user.Password;
                    user.FirstName = model.FirstName;
                    user.Email = model.Email;
                    user.Address = model.Address;
                    user.LastName = model.LastName;
                    user.Birthday = model.Birthday;
                    user.DateIn = model.DateIn;
                    user.DateOut = model.DateOut != null ? model.DateOut : null;

                    UpdateUserRoles(assignedRoles, user, rolesRepository);
                    usersRepositoryUnitOfWork.Save(user);

                    PopulateAssignedRoles(user, rolesRepository);
                    unitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    unitOfWork.RollBack();
                    throw ex;
                }
            }

            return RedirectToAction("Index", "Users");
        }
        public int GetPagesCount(Expression<Func<User, bool>> filter = null)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            UsersRepository usersRepository = new UsersRepository(context);

            int pagesCount = 0;
            int pageSize = ApplicationConfiguration.ItemsPerPage;
            int usersCount = 0;
            usersCount = usersRepository.Count(filter);
            pagesCount = usersCount / pageSize;
            if ((usersCount % pageSize) > 0)
            {
                pagesCount++;
            }

            return pagesCount;
        }
        public ActionResult Index(string sortOrder)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            UsersRepository usersRepository = new UsersRepository(context);
            UsersIndexVM model = new UsersIndexVM();
            this.TryUpdateModel(model);

            model.UsersPager = model.UsersPager ?? new GenericPagerVM();
            model.UsersPager.CurrentPage = model.UsersPager.CurrentPage == 0 ? 1 : model.UsersPager.CurrentPage;
            model.UsersPager.Action = "Index";
            model.UsersPager.Controller = "Users";
            model.UsersPager.Prefix = "UsersPager";
            model.UsersPager.CurrentParameters = new Dictionary<string, object>()
            {
                { "PersonaNumber", model.PersonalNumber },
                { "CustomerName", model.UserName },
                { "Email", model.Email },
                { "Address", model.Address },
                { "RoleName", model.RoleName },
                { "BirthdayStartDate", model.BirthdayStartDate },
                { "BirthdayEndDate", model.BirthdayEndDate },
                { "DateInStartDate", model.DateInStartDate },
                { "DateInEndDate", model.DateInEndDate },
                { "DateOutStartDate", model.DateOutStartDate },
                { "DateOutEndDate", model.DateOutEndDate },
                { "UsersPager.CurrentPage", model.UsersPager.CurrentPage }
            };

            #region Soring and Filtering
            Expression<Func<User, bool>> filter = u =>
                   (model.PersonalNumber == default(int) || u.PersonalNumber == model.PersonalNumber) &&
                   (string.IsNullOrEmpty(model.UserName) || (u.FirstName.Contains(model.UserName) || u.LastName.Contains(model.UserName))) &&
                   (string.IsNullOrEmpty(model.Email) || u.Email.Contains(model.Email)) &&
                   (string.IsNullOrEmpty(model.Address) || u.Address.Contains(model.Address)) &&
                   (string.IsNullOrEmpty(model.RoleName) || u.Roles.Any(r => r.Name.Contains(model.RoleName))) &&
                   (model.BirthdayStartDate == default(DateTime) || (u.Birthday == model.BirthdayStartDate || (u.Birthday >= model.BirthdayStartDate && u.Birthday <= model.BirthdayEndDate))) &&
                   (model.DateInStartDate == default(DateTime) || (u.DateIn == model.DateInStartDate || (u.DateIn >= model.DateInStartDate && u.Birthday <= model.DateInEndDate))) &&
                   (model.DateOutStartDate == default(DateTime) || (u.DateOut == model.DateOutStartDate || (u.DateOut >= model.DateOutStartDate && u.Birthday <= model.DateOutEndDate)));
            model.UsersPager.PagesCount = GetPagesCount(filter);

            ViewBag.NameSortParam = string.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.RolesSortParam = sortOrder == "Roles" ? "roles_desc" : "Roles";
            ViewBag.PersonalNumberSortParam = sortOrder == "PersonalNumber" ? "personalNumber_desc" : "PersonalNumber";
            ViewBag.EmailSortParam = sortOrder == "Email" ? "email_desc" : "Email";
            ViewBag.AddressSortParam = sortOrder == "Address" ? "address_desc" : "Address";
            ViewBag.BirthdaySortParam = sortOrder == "Birthday" ? "birthday_desc" : "Birthday";
            ViewBag.DateInSortParam = sortOrder == "DateIn" ? "dateIn_desc" : "DateIn";
            ViewBag.DateOutSortParam = sortOrder == "DateOut" ? "dateOut_desc" : "DateOut";
            switch (sortOrder)
            {
                case "name_desc":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.FirstName));
                    break;
                case "Roles":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.Roles.FirstOrDefault().Name));
                    break;
                case "roles_desc":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.Roles.FirstOrDefault().Name));
                    break;
                case "PersonalNumber":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.PersonalNumber));
                    break;
                case "personalNumber_desc":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.PersonalNumber));
                    break;
                case "Email":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.Email));
                    break;
                case "email_desc":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.Email));
                    break;
                case "Address":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.Address));
                    break;
                case "address_desc":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.Address));
                    break;
                case "Birthday":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.Birthday));
                    break;
                case "birthday_desc":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.Birthday));
                    break;
                case "DateIn":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.DateIn));
                    break;
                case "dateIn_desc":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.DateIn));
                    break;
                case "DateOut":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.DateOut));
                    break;
                case "dateOut_desc":
                    model.UsersList = usersRepository
                        .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.DateOut));
                    break;
                default:
                    model.UsersList = usersRepository
                       .GetAll(model.UsersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.FirstName));
                    break;
            }
            #endregion

            return View(model);
        }
        public ActionResult Details(int id)
        {
            LibraryManagementSystemContext context = new LibraryManagementSystemContext();
            BooksRepository booksRepository = new BooksRepository(context);
            AuthorsRepository authorsRepository = new AuthorsRepository(context);
            BooksDetailsVM model = new BooksDetailsVM();
            this.TryUpdateModel(model);

            Book book = booksRepository.GetByID(id);
            Author author = new Author();
            if (book != null)
            {
                model.ID = book.ID;
                model.Title = book.Title;
                model.Publisher = book.Publisher;
                model.StockCount = book.StockCount;
                model.DeliveryPrice = book.DeliveryPrice;
                model.DateReceived = book.DateReceived;
                model.DatePublished = book.DatePublished;
                model.AuthorsPager = model.AuthorsPager ?? new GenericPagerVM();
                model.AuthorsPager.PagesCount = GetPagesCount();
                model.AuthorsPager.CurrentPage =
                    model.AuthorsPager.CurrentPage == 0 ? 1 : model.AuthorsPager.CurrentPage;
                model.Authors = authorsRepository
                    .GetAll(model.AuthorsPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, a => a.Books.Any(b => b.ID == id), order: x => x.OrderBy(a => a.FirstName))
                    .ToList();
                model.AuthorsPager.Action = "Details";
                model.AuthorsPager.Controller = "Books";
                model.AuthorsPager.Prefix = "AuthorsPager";
                model.AuthorsPager.CurrentParameters = new Dictionary<string, object>()
                {
                    { "AuthorsPager.CurrentPage", model.AuthorsPager.CurrentPage }
                };

                return View(model);
            }
            else
            {
                return RedirectToAction("Index", "Authors");
            }
        }