Ejemplo n.º 1
0
        public IActionResult Add(BookVM model, int[] categories, int[] people, int[]?interparray)
        {
            model.Imagepath = "";

            if (model.Coverimage != null)
            {
                var guid = Guid.NewGuid().ToString();

                var path = Path.Combine(
                    Directory.GetCurrentDirectory(),
                    "wwwroot/Admin/Coverimg", guid + ".jpg");

                using (var stream = new FileStream(path, FileMode.Create))
                {
                    model.Coverimage.CopyTo(stream);
                }

                model.Imagepath = guid + ".jpg";
            }


            if (ModelState.IsValid)
            {
                Book book = new Book();
                book.Name        = model.Name;
                book.Publisher   = model.Publisher;
                book.PublishDate = model.PublishDate;
                book.Imagepath   = model.Imagepath;
                book.Edition     = model.Edition;
                book.Summary     = model.Summary;

                _bookcontext.Books.Add(book);
                _bookcontext.SaveChanges();

                int BookID = book.ID;
                foreach (var item in categories)
                {
                    BookCategory bookCategory = new BookCategory();
                    bookCategory.CategoryID = item;
                    bookCategory.BookID     = BookID;

                    _bookcontext.BookCategories.Add(bookCategory);
                }

                foreach (var item in people)
                {
                    BookPerson bookperson = new BookPerson();
                    bookperson.PersonID = item;
                    bookperson.BookID   = BookID;
                    bookperson.DutyID   = 0;

                    _bookcontext.BookPeople.Add(bookperson);
                }

                foreach (var item in interparray)
                {
                    BookPerson bookperson = new BookPerson();
                    bookperson.PersonID = item;
                    bookperson.BookID   = BookID;
                    bookperson.DutyID   = 1;

                    _bookcontext.BookPeople.Add(bookperson);
                }

                _bookcontext.SaveChanges();

                return(Redirect("/Admin/Book/"));
            }

            else
            {
                ViewBag.categorybag = _bookcontext.Categories.Where(q => q.IsDeleted == false).ToList();
                ViewBag.personbag   = _bookcontext.People.Where(q => q.IsDeleted == false).ToList();

                return(View());
            }
        }
Ejemplo n.º 2
0
        public IActionResult Edit(BookVM model, int[] categories, int[] people, int[]?interparray)
        {
            Book book = _bookcontext.Books.FirstOrDefault(q => q.ID == model.BookID);

            book.Name        = model.Name;
            book.Publisher   = model.Publisher;
            book.PublishDate = model.PublishDate;
            book.Edition     = model.Edition;
            book.Summary     = model.Summary;

            if (model.Coverimage != null)
            {
                model.Imagepath = "";

                var guid = Guid.NewGuid().ToString();

                var path = Path.Combine(
                    Directory.GetCurrentDirectory(),
                    "wwwroot/Admin/Coverimg", guid + ".jpg");

                using (var stream = new FileStream(path, FileMode.Create))
                {
                    model.Coverimage.CopyTo(stream);
                }

                model.Imagepath = guid + ".jpg";


                book.Imagepath = model.Imagepath;
            }


            _bookcontext.SaveChanges();

            int BookID = book.ID;


            List <BookCategory> bookcategories = _bookcontext.BookCategories.Where(q => q.BookID == model.BookID).ToList();

            foreach (var item in bookcategories)
            {
                _bookcontext.BookCategories.Remove(item);
            }

            foreach (var item in categories)
            {
                BookCategory bookCategory = new BookCategory();
                bookCategory.CategoryID = item;
                bookCategory.BookID     = BookID;

                _bookcontext.BookCategories.Add(bookCategory);
            }


            List <BookPerson> bookpeople = _bookcontext.BookPeople.Where(q => q.BookID == model.BookID).ToList();

            foreach (var item in bookpeople)
            {
                item.IsDeleted = true;
                _bookcontext.BookPeople.Remove(item);
            }

            _bookcontext.SaveChanges();

            foreach (var item in people)
            {
                BookPerson bookPerson = new BookPerson();
                bookPerson.PersonID = item;
                bookPerson.BookID   = BookID;
                bookPerson.DutyID   = 0;

                _bookcontext.BookPeople.Add(bookPerson);
            }

            foreach (var item in interparray)
            {
                BookPerson bookPerson1 = new BookPerson();
                bookPerson1.PersonID = item;
                bookPerson1.BookID   = BookID;
                bookPerson1.DutyID   = 1;

                _bookcontext.BookPeople.Add(bookPerson1);
            }

            _bookcontext.SaveChanges();

            return(Redirect("/Admin/Book/"));
        }