public async Task <IActionResult> AddBook([Bind("AuthorId,BookId")] Authorship AddObj)
        {
            var bookData   = _context.Books.Where(o => o.Id == AddObj.BookId).FirstOrDefault();
            var authorData = _context.Authors.Where(o => o.Id == AddObj.AuthorId).FirstOrDefault();
            var search     = await _context.Authorship.Where(o => o.BookId == AddObj.BookId && o.AuthorId == AddObj.AuthorId).FirstOrDefaultAsync();

            if (search != null)
            {
                ModelState.AddModelError("BookId", "В автора вже є ця книга");
            }
            if (authorData.DateOfBirth != null && authorData.DateOfBirth.Value.Year > bookData.YearOfPublication)
            {
                ModelState.AddModelError("BookId", "Книжка не може бути написана раніше за дату народження автора");
            }
            if (ModelState.IsValid)
            {
                _context.Add(AddObj);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Details", new { id = AddObj.AuthorId }));
            }
            ViewBag.AuthorData = new SelectList(_context.Authors, "Id", "FullName", AddObj.AuthorId);
            ViewBag.BookData   = new SelectList(_context.Books, "Id", "Name", AddObj.BookId);
            return(View(AddObj));
        }
示例#2
0
        private bool EditBook(Book editedBook)
        {
            if (textBoxTitle.Text.Count() == 0)
            {
                MessageBox.Show("Nazwa nie może być pusta");
                return(false);
            }
            if (lstViewAddedAuthors.Items.Count == 0)
            {
                MessageBox.Show("Lista autorów nie może być pusta");
                return(false);
            }
            editedBook.Title   = textBoxTitle.Text;
            editedBook.GenreId = dbContext.Genres.Where(g => g.Name == genreSpinner.Text).FirstOrDefault().Id;
            editedBook.Genre   = dbContext.Genres.Where(g => g.Name == genreSpinner.Text).FirstOrDefault();
            List <Authorship> authorships = new List <Authorship>();

            dbContext.Authorships.RemoveRange(editedBook.Authorship);

            foreach (Author author in addedAuthors)
            {
                Authorship authorship = new Authorship();
                authorship.Book   = editedBook;
                authorship.Author = author;
                dbContext.Authorships.Add(authorship);
            }
            return(true);
        }
示例#3
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,BookID,AuthorID")] Authorship authorship)
        {
            if (id != authorship.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(authorship);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AuthorshipExists(authorship.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AuthorID"] = new SelectList(_context.Authors, "ID", "FullName", authorship.AuthorID);
            ViewData["BookID"]   = new SelectList(_context.Books, "BookID", "Title", authorship.BookID);
            return(View(authorship));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            Authorship authorship = db.Authorships.Find(id);

            db.Authorships.Remove(authorship);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
 public ActionResult Edit([Bind(Include = "AuthorshipID,AuthorID,BookID")] Authorship authorship)
 {
     if (ModelState.IsValid)
     {
         db.Entry(authorship).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.AuthorID = new SelectList(db.Authors, "AuthorID", "AuthorName", authorship.AuthorID);
     ViewBag.BookID   = new SelectList(db.Books, "BookID", "Title", authorship.BookID);
     return(View(authorship));
 }
        // GET: Authorships/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Authorship authorship = db.Authorships.Find(id);

            if (authorship == null)
            {
                return(HttpNotFound());
            }
            return(View(authorship));
        }
        // GET: Authorships/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Authorship authorship = db.Authorships.Find(id);

            if (authorship == null)
            {
                return(HttpNotFound());
            }
            ViewBag.AuthorID = new SelectList(db.Authors, "AuthorID", "AuthorName", authorship.AuthorID);
            ViewBag.BookID   = new SelectList(db.Books, "BookID", "Title", authorship.BookID);
            return(View(authorship));
        }
示例#8
0
        public async Task <IActionResult> Create([Bind("ID,BookID,AuthorID")] Authorship authorship)
        {
            ViewData["AuthorID"] = new SelectList(_context.Authors, "ID", "FullName", authorship.AuthorID);
            ViewData["BookID"]   = new SelectList(_context.Books, "BookID", "Title", authorship.BookID);

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Add(authorship);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException)
                {
                    ModelState.AddModelError(string.Empty, "Duplicate Authorship");
                    return(View(authorship));
                }
            }
            return(RedirectToAction(nameof(Index)));
        }
示例#9
0
        public async Task <IActionResult> Import(IFormFile fileExcel)
        {
            if (ModelState.IsValid)
            {
                if (fileExcel != null)
                {
                    using (var stream = new FileStream(fileExcel.FileName, FileMode.Create))
                        if (Path.GetExtension(stream.Name) == ".xlsx" || Path.GetExtension(stream.Name) == ".xls")
                        {
                            await fileExcel.CopyToAsync(stream);

                            using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled))
                            {
                                foreach (IXLWorksheet worksheet in workBook.Worksheets)
                                {
                                    //worksheet.Name - назва категорії. Пробуємо знайти в БД, якщо відсутня, то створюємо нову
                                    Categories newcat;
                                    var        c = (from cat in _context.Categories
                                                    where cat.CategoryName.Contains(worksheet.Name)
                                                    select cat).ToList();
                                    if (c.Count > 0)
                                    {
                                        newcat = c[0];
                                    }
                                    else
                                    {
                                        newcat = new Categories();
                                        newcat.CategoryName = worksheet.Name;
                                        _context.Categories.Add(newcat);
                                    }
                                    //перегляд усіх рядків
                                    foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                                    {
                                        try
                                        {
                                            Books book = new Books();
                                            book.Name = row.Cell("1").Value.ToString();
                                            if (Int16.Parse(row.Cell("2").Value.ToString()) < 1 || Int16.Parse(row.Cell("2").Value.ToString()) > 25000)
                                            {
                                                throw new Exception();
                                            }
                                            book.NumberOfPages = Int16.Parse(row.Cell("2").Value.ToString());

                                            if (Int16.Parse(row.Cell("3").Value.ToString()) < 1300 || Int16.Parse(row.Cell("3").Value.ToString()) > 2020)
                                            {
                                                throw new Exception();
                                            }

                                            book.YearOfPublication = Int16.Parse(row.Cell("3").Value.ToString());
                                            BookCategory bookCategory = new BookCategory();
                                            bookCategory.Category = newcat;
                                            bookCategory.Book     = book;
                                            _context.Books.Add(book);
                                            _context.BookCategory.Add(bookCategory);
                                            for (int i = 4; i <= 6; i++)
                                            {
                                                /* if(!Regex.IsMatch(row.Cell(i).Value.ToString(), @"^([A-Z][a-z]+)\ ([A-Z][a-z]+)(\ ?([A-Z][a-z]+)?)|([А-ЯІЇЄЩ][а-яіїщє]+)\ ([А-ЯІЇЄЩ][а-яіїщє]+)(\ ?([А-ЯІЇЄЩ][а-яіїщє]+)?)$")) {
                                                 *   throw new Exception();
                                                 * }                     */
                                                if (row.Cell(i).Value.ToString().Length > 0)
                                                {
                                                    Authors author;
                                                    var     a = (from aut in _context.Authors
                                                                 where aut.FullName.Contains(row.Cell(i).Value.ToString())
                                                                 select aut).ToList();
                                                    if (a.Count > 0)
                                                    {
                                                        author = a[0];
                                                    }
                                                    else
                                                    {
                                                        author          = new Authors();
                                                        author.FullName = row.Cell(i).Value.ToString();
                                                        _context.Add(author);
                                                    }
                                                    Authorship ab = new Authorship();
                                                    ab.Book   = book;
                                                    ab.Author = author;
                                                    _context.Authorship.Add(ab);
                                                }
                                            }
                                        }

                                        catch (Exception e)
                                        {
                                            TempData["msgDATA"] = "<script>alert('Некоректний зміст файлу');</script>";
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            TempData["msgDATA"] = "<script>alert('Некоректний формат файлу');</script>";
                        }
                }

                await _context.SaveChangesAsync();
            }
            return(RedirectToAction(nameof(Index)));
        }