public IActionResult Deleted(BookStor book) { var books = _context.BookStors.FirstOrDefault(b => b.BookId == book.BookId); if (book.Delete == false) { books.Delete = book.Delete = true; } _context.SaveChanges(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Edit(BookCreateViewModel ViewModel) { if (!ModelState.IsValid) { try { DateTime?PublishDate; if (ViewModel.IsPublish == true && ViewModel.RecentIsPublish == false) { PublishDate = DateTime.Now; } else if (ViewModel.RecentIsPublish == true && ViewModel.IsPublish == false) { PublishDate = null; } else { PublishDate = ViewModel.PublishDate; } BookStor book = new BookStor() { BookId = ViewModel.BookId, Title = ViewModel.Title, ISBN = ViewModel.ISBN, NumOfPages = ViewModel.NumOfPages, Price = ViewModel.Price, Stock = ViewModel.Stock, IsPublish = ViewModel.IsPublish, LanguageID = ViewModel.LanguageID, PublisherID = ViewModel.PublisherID, PublishYear = ViewModel.PublishYear, Summary = ViewModel.Summary, Weight = ViewModel.Weight, PublishDate = PublishDate, Delete = false, }; _context.Update(book); var RecentAuthors = (from a in _context.Auther_Books where (a.BookId == ViewModel.BookId) select a.AutherId).ToArray(); var RecentTranslators = (from a in _context.Translator_Books where (a.BookId == ViewModel.BookId) select a.TranslaorId).ToArray(); var DeletedAuthors = RecentAuthors.Except(ViewModel.AuthorID); var DeletedTranslators = RecentTranslators.Except(ViewModel.TranslatorID); var AddedAuthors = ViewModel.AuthorID.Except(RecentAuthors); var AddedTranslators = ViewModel.TranslatorID.Except(RecentTranslators); if (DeletedAuthors.Count() != 0) { _context.RemoveRange(DeletedAuthors.Select(a => new Auther_book { AutherId = a, BookId = ViewModel.BookId }).ToList()); } if (DeletedTranslators.Count() != 0) { _context.RemoveRange(DeletedTranslators.Select(a => new Translator_Book { TranslaorId = a, BookId = ViewModel.BookId }).ToList()); } if (AddedAuthors.Count() != 0) { _context.AddRange(AddedAuthors.Select(a => new Auther_book { AutherId = a, BookId = ViewModel.BookId }).ToList()); } if (AddedTranslators.Count() != 0) { _context.AddRange(AddedTranslators.Select(a => new Translator_Book { TranslaorId = a, BookId = ViewModel.BookId }).ToList()); } await _context.SaveChangesAsync(); ViewBag.MsgSuccess = "ذخیره تغییرات با موفقیت انجام شد."; return(View(ViewModel)); } catch { ViewBag.MsgFailed = "در ذخیره تغییرات خطایی رخ داده است."; } } ViewBag.AuthorID = new SelectList(_context.Authers.Select(r => new ListAuther { AutherId = r.AutherId, NameFamily = r.Name + " " + r.LastName }), "AutherId", "NameFamily"); ViewBag.LanguageID = new SelectList(_context.Languges, "LanguegeName", "LanguegeName"); ViewBag.PublisherID = new SelectList(_context.Publishers, "PublisherId", "PublisherName"); ViewBag.TranslatorID = new SelectList(_context.Translators.Select(r => new ListTranslator { TranslatorId = r.TranslaorId, NameFamily = r.Name + " " + r.LastName }), "TranslatorId", "NameFamily"); return(View(ViewModel)); }
public async Task <IActionResult> Creat(BookCreateViewModel viewModel) { if (ModelState.IsValid) { if (viewModel.File != null) { string FileName = viewModel.File.FileName; string Extension = Path.GetExtension(FileName); var Type = FileExtentions.FileType.PDF; using (var memory = new MemoryStream()) { await viewModel.File.CopyToAsync(memory); //result = FileExtentions.IsValidFile(memory.ToArray(), Type, Extension.Replace('.', ' ')); string NewFileName = String.Concat(Guid.NewGuid().ToString(), Extension); var path = $"{_evn.WebRootPath}/BookFile/{NewFileName}"; using (var Stream = new FileStream(path, FileMode.Create)) { await viewModel.File.CopyToAsync(Stream); } viewModel.FileName = NewFileName; } } DateTime?PublishDate1 = null; if (viewModel.IsPublish == true) { PublishDate1 = DateTime.Now; } BookStor book = new BookStor { BookId = viewModel.BookId, Delete = false, ISBN = viewModel.ISBN, LanguageID = viewModel.LanguageID, IsPublish = viewModel.IsPublish, Price = viewModel.Price, NumOfPages = viewModel.NumOfPages, PublishYear = viewModel.PublishYear, Stock = viewModel.Stock, Summary = viewModel.Summary, Title = viewModel.Title, Weight = viewModel.Weight, PublishDate = PublishDate1, PublisherID = viewModel.PublisherID, File = viewModel.FileName, }; if (viewModel.Image != null) { using (var Memory = new MemoryStream()) { await viewModel.Image.CopyToAsync(Memory); book.Image = Memory.ToArray(); } } await _context.BookStors.AddAsync(book); List <Auther_book> authors = new List <Auther_book>(); if (viewModel.AuthorID != null) { for (int i = 0; i < viewModel.AuthorID.Length; i++) { Auther_book author = new Auther_book() { BookId = book.BookId, AutherId = viewModel.AuthorID[i], }; authors.Add(author); } await _context.Auther_Books.AddRangeAsync(authors); } List <Translator_Book> translators = new List <Translator_Book>(); if (viewModel.TranslatorID != null) { for (int i = 0; i < viewModel.TranslatorID.Length; i++) { Translator_Book translator = new Translator_Book { BookId = book.BookId, TranslaorId = viewModel.TranslatorID[i] }; translators.Add(translator); } await _context.Translator_Books.AddRangeAsync(translators); } List <Category_Book> categories = new List <Category_Book>(); if (viewModel.CategoryID != null) { for (int i = 0; i < viewModel.CategoryID.Length; i++) { Category_Book category = new Category_Book { BookId = book.BookId, CategoryID = viewModel.CategoryID[i] }; } } await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } else { ViewBag.AuthorID = new SelectList(_context.Authers.Select(r => new ListAuther { AutherId = r.AutherId, NameFamily = r.Name + " " + r.LastName }), "AutherId", "NameFamily"); ViewBag.LanguageID = new SelectList(_context.Languges, "LangugeId", "LanguegeName"); ViewBag.PublisherID = new SelectList(_context.Publishers, "PublisherId", "PublisherName"); ViewBag.TranslatorID = new SelectList(_context.Translators.Select(r => new ListTranslator { TranslatorId = r.TranslaorId, NameFamily = r.Name + " " + r.LastName }), "TranslatorId", "NameFamily"); viewModel.Category = _repasitory.GetAllCategory(); return(View(viewModel)); } }