public async Task <IActionResult> Create([Bind("Id,Name,Info,Fb2,Pdf,PagesQuantity,Picture")] Book book, int[] genres, int[] authors) { var AreRepeats = _context.Books.Where(obj => obj.Name == book.Name && obj.PagesQuantity == book.PagesQuantity); foreach (var a in AreRepeats) { bool flag = false; var aBooks = _context.AuthorsBooks.Where(obj => obj.BookId == a.Id); foreach (var author in authors) { if (aBooks.Where(obj => obj.AuthorId == author).Count() > 0) { flag = true; } break; } if (flag) { ModelState.AddModelError("", "Така книга вже існує"); break; } } if (ModelState.IsValid) { _context.Add(book); foreach (int GId in genres) { GenresBook gb = new GenresBook { GenreId = GId, BookId = book.Id }; book.GenresBooks.Add(gb); } foreach (int AId in authors) { AuthorsBook ab = new AuthorsBook { AuthorId = AId, BookId = book.Id }; book.AuthorsBooks.Add(ab); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewBag.Genres = _context.Genres.ToList(); ViewBag.Authors = _context.Authors.ToList(); return(View(book)); }
public async Task <IActionResult> Import(IFormFile fileExcel) { if (ModelState.IsValid) { if (fileExcel != null) { using (var stream = new FileStream(fileExcel.FileName, FileMode.Create)) { await fileExcel.CopyToAsync(stream); using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled)) { List <string> missingAuthors = new List <string>(); List <string> unvalidPath = new List <string>(); List <string> quantity = new List <string>(); List <string> doubles = new List <string>(); string NameProblem = ""; foreach (IXLWorksheet worksheet in workBook.Worksheets) { Author newAuthor; var author = _context.Authors.Where(obj => (obj.FirstName + " " + obj.LastName).ToLower() == worksheet.Name.ToLower()).ToList(); if (author.Count > 0) { newAuthor = author[0]; foreach (IXLRow row in worksheet.RowsUsed().Skip(1)) { try { if (row.Cell(1).Value.ToString() == null || row.Cell(1).Value.ToString().Length < 3) { NameProblem = "Деякі з книжок не були додані, бо назва була відсутня або закоротка"; continue; } bool flag = false; Book book = new Book(); Regex fb2 = new Regex(@"^(/css/fb2Books/)\S+.fb2$"); Regex pdf = new Regex(@"^(/css/pdfBooks/)\S+.pdf$"); Regex pic = new Regex(@"^(/css/covers/)\S+.(png|jpeg|jpg)$"); if (fb2.IsMatch(row.Cell(5).Value.ToString()) && pdf.IsMatch(row.Cell(6).Value.ToString()) && pic.IsMatch(row.Cell(7).Value.ToString())) { flag = true; book.Fb2 = row.Cell(5).Value.ToString(); book.Pdf = row.Cell(6).Value.ToString(); book.Picture = row.Cell(7).Value.ToString(); } else { unvalidPath.Add(row.Cell(1).Value.ToString()); } if (flag == true) { var areDoubles = _context.AuthorsBooks.Where(obj => obj.Id == newAuthor.Id).Join(_context.Books, b => b.BookId, c => c.Id, (b, c) => new { Name = c.Name }); if (areDoubles.Where(obj => obj.Name == row.Cell(1).Value.ToString()).ToList().Count > 0) { doubles.Add(row.Cell(1).Value.ToString()); } else if (_context.Books.Where(obj => obj.Fb2 == row.Cell(5).Value.ToString() || obj.Pdf == row.Cell(6).Value.ToString()).ToList().Count > 0) { doubles.Add(row.Cell(1).Value.ToString()); } else { book.Name = row.Cell(1).Value.ToString(); int qnt; bool success = Int32.TryParse(row.Cell(8).Value.ToString(), out qnt); if (success && qnt > 0) { book.PagesQuantity = qnt; } else { book.PagesQuantity = 2; quantity.Add(book.Name); } if (row.Cell(9).Value.ToString() != null) { book.Info = row.Cell(9).Value.ToString(); } else { book.Info = "Інформація за замовчуванням"; } _context.Books.Add(book); AuthorsBook ab = new AuthorsBook(); ab.Book = book; ab.Author = newAuthor; _context.AuthorsBooks.Add(ab); for (int i = 2; i <= 4; i++) { if (row.Cell(i).Value.ToString().Length > 0) { Genre genre; var g = _context.Genres.Where(obj => obj.Name.ToLower().Contains(row.Cell(i).Value.ToString().ToLower())).ToList(); if (g.Count > 0) { genre = g[0]; } else { genre = new Genre(); genre.Name = row.Cell(i).Value.ToString(); genre.Info = "Інформація поки відсутня"; //додати в контекст _context.Add(genre); } GenresBook gb = new GenresBook(); gb.Book = book; gb.Genre = genre; _context.GenresBooks.Add(gb); } } } } } catch (Exception e) { //logging самостійно :) } } } else { missingAuthors.Add(worksheet.Name); } } ViewBag.UnvalidPath = unvalidPath; ViewBag.MissingAuthors = missingAuthors; ViewBag.PagesChanged = quantity; ViewBag.Doubles = doubles; ViewBag.NameProblem = NameProblem; } } } await _context.SaveChangesAsync(); } return(View()); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Info,Fb2,Pdf,PagesQuantity,Picture")] Book book, int[] genres, int[] authors) { if (id != book.Id) { return(NotFound()); } var AreRepeats = _context.Books.Where(obj => obj.Id != book.Id && obj.Name == book.Name && obj.PagesQuantity == book.PagesQuantity); foreach (var a in AreRepeats) { bool flag = false; var aBooks = _context.AuthorsBooks.Where(obj => obj.BookId == a.Id); foreach (var author in authors) { if (aBooks.Where(obj => obj.AuthorId == author).Count() > 0) { flag = true; } break; } if (flag) { ModelState.AddModelError("", "Така книга вже існує"); break; } } if (ModelState.IsValid) { try { _context.Update(book); foreach (var item in _context.GenresBooks) { if (item.BookId == book.Id) { _context.GenresBooks.Remove(item); } } foreach (var item in _context.AuthorsBooks) { if (item.BookId == book.Id) { _context.AuthorsBooks.Remove(item); } } foreach (int GId in genres) { GenresBook gb = new GenresBook { GenreId = GId, BookId = book.Id }; book.GenresBooks.Add(gb); } foreach (int AId in authors) { AuthorsBook ab = new AuthorsBook { AuthorId = AId, BookId = book.Id }; book.AuthorsBooks.Add(ab); } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(book.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } var thisGenres = _context.GenresBooks.Where(obj => obj.BookId == id).Join(_context.Genres, b => b.GenreId, g => g.Id, (b, g) => new { Id = b.GenreId, Name = g.Name }); List <string> thisNames = new List <string>(); foreach (var g in thisGenres) { thisNames.Add(g.Name); } ViewBag.ThisGenres = thisGenres; ViewBag.OtherGenres = _context.Genres.Where(obj => !thisNames.Contains(obj.Name)).ToList(); var thisAuthors = _context.AuthorsBooks.Where(obj => obj.BookId == id).Join(_context.Authors, b => b.AuthorId, a => a.Id, (b, a) => new { Id = b.AuthorId, FullName = a.FirstName + " " + a.LastName }); List <string> thisFullNames = new List <string>(); foreach (var a in thisAuthors) { thisFullNames.Add(a.FullName); } ViewBag.ThisAuthors = thisAuthors; ViewBag.OtherAuthors = _context.Authors.Where(obj => !thisFullNames.Contains(obj.FirstName + " " + obj.LastName)).ToList(); return(View(book)); }