public async Task <ActionResult> Create(NewBookViewModel model)
        {
            if (ModelState.IsValid && model.File != null)
            {
                Book book = new Book();
                book.Title          = model.Title;
                book.Age            = model.Age;
                book.Descriptions   = model.Descriptions;
                book.CategoryId     = model.CategoryId;
                book.Authors        = db.Authours.Where(a => model.AuthorsId.Contains(a.Id)).ToList();
                book.Categories     = db.Categories.Where(c => model.CategoriesId.Contains(c.Id)).ToList();
                book.PublisherId    = model.PublisherId;
                book.Print          = model.Print;
                book.PrintDate      = model.PrintDate;
                book.FileName       = model.File.FileName;
                book.CoverImageName = model.CoverImage?.FileName ?? "";
                book.CreateDate     = DateTime.Now;
                book.LastUpdate     = DateTime.Now;

                db.Books.Add(book);
                await db.SaveChangesAsync();

                if (model.File.ContentLength > 0)
                {
                    model.File.SaveAs(Path.Combine(Server.MapPath("~/Resources/BooksFiles"), Path.GetFileName(model.File.FileName)));
                }

                if (model.CoverImage?.ContentLength > 0)
                {
                    string path = Path.Combine(Server.MapPath("~/Resources/BooksCoverImage"), Path.GetFileName(model.CoverImage.FileName));
                    OptimizeImages.SetCompressionLevel(new Bitmap(model.CoverImage.InputStream), path);
                }

                return(RedirectToAction("Index"));
            }

            var categories = db.Categories.Select(c => new { Id = c.Id, Name = c.Name }).ToList();
            var publishers = db.Publishers.Select(c => new { Id = c.Id, Name = c.Name }).ToList();
            var authours   = db.Authours.Select(c => new { Id = c.Id, Name = c.FirstName }).ToList();

            model.Category   = new SelectList(categories, "Id", "Name", model.CategoryId);
            model.Categories = new MultiSelectList(categories, "Id", "Name", model.CategoriesId);
            model.Authors    = new MultiSelectList(authours, "Id", "Name", model.AuthorsId);
            model.Publisher  = new SelectList(publishers, "Id", "Name", model.PublisherId);

            return(View(model));
        }
        public async Task <ActionResult> Edit(EditBookViewModel model)
        {
            if (ModelState.IsValid)
            {
                Book book = await db.Books.Include(b => b.Authors).Include(b => b.Categories).FirstAsync(b => b.Id == model.Id);

                book.Title        = model.Title;
                book.Age          = model.Age;
                book.Descriptions = model.Descriptions;
                book.CategoryId   = model.CategoryId;
                book.Authors      = db.Authours.Where(a => model.AuthorsId.Contains(a.Id)).ToList();
                book.Categories   = db.Categories.Where(c => model.CategoriesId.Contains(c.Id)).ToList();
                book.PublisherId  = model.PublisherId;
                book.Print        = model.Print;
                book.PrintDate    = model.PrintDate;

                if (model.File?.ContentLength > 0)
                {
                    book.FileName = model.File.FileName;
                }

                if (model.CoverImage?.ContentLength > 0)
                {
                    book.CoverImageName = model.CoverImage?.FileName;
                }

                book.LastUpdate = DateTime.Now;

                db.Entry(book).State = EntityState.Modified;
                await db.SaveChangesAsync();

                if (model.File?.ContentLength > 0)
                {
                    model.File.SaveAs(Path.Combine(Server.MapPath("~/Resources/BooksFiles"), Path.GetFileName(model.File.FileName)));
                }

                if (model.CoverImage?.ContentLength > 0)
                {
                    string path = Path.Combine(Server.MapPath("~/Resources/BooksCoverImage"), Path.GetFileName(model.CoverImage.FileName));
                    OptimizeImages.SetCompressionLevel(new Bitmap(model.CoverImage.InputStream), path);
                }

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }