Ejemplo n.º 1
0
        /// <inheritdoc />
        Task IBookApiService.PutAsync(int id, BookUpdateViewModel book, string authorization)
        {
            var arguments = new object[] { id, book, authorization };
            var func      = requestBuilder.BuildRestResultFuncForMethod("PutAsync", new Type[] { typeof(int), typeof(BookUpdateViewModel), typeof(string) });

            return((Task)func(Client, arguments));
        }
Ejemplo n.º 2
0
        public void Update(BookUpdateViewModel bookView, List <int> authorId)
        {
            //List<AuthorInBook> authorInBook = GetAuthorInBook(bookView, authorId);
            Book bookReposytoryUpdate = _bookRepository.Get(bookView.Id);

            if (bookReposytoryUpdate != null)
            {
                //var mapper = new AutoMapperForBook();
                //Book bookMapp = mapper.Mapp(bookView);
                bookReposytoryUpdate.NameBook          = bookView.NameBook;
                bookReposytoryUpdate.NumberPages       = bookView.NumberPages;
                bookReposytoryUpdate.PublishingCompany = bookView.PublishingCompany;
                bookReposytoryUpdate.DatePublishing    = bookView.DatePublishing;
                _bookRepository.Update(bookReposytoryUpdate);
                UpdateRepository(bookView, authorId);
            }
            //authorInBook.GroupBy(x => x.Id).Select(z => z.First().Id = book.First().Id);
            //if (book != null)
            //{
            //    for (int i = 0; i < book.Count(); i++)
            //    {
            //        authorInBook[i].Id = bookReposytory[i].Id;
            //    }

            //    _authorInBookRepository.Update(authorInBook);
            //}
        }
        public IActionResult Update(int id, BookUpdateViewModel bookUpdateViewModel)
        {
            var book = this.bookRepository.Get(id);

            if (book != null)
            {
                if (ModelState.IsValid)
                {
                    book.ISBN            = bookUpdateViewModel.ISBN;
                    book.Title           = bookUpdateViewModel.Title;
                    book.Author          = bookUpdateViewModel.Author;
                    book.PublicationYear = bookUpdateViewModel.PublicationYear;
                    book.Type            = bookUpdateViewModel.Type;

                    this.bookRepository.Update(book);
                    TempData["Message"] = $"{book.Title} is aangepast.";
                    return(RedirectToAction("Index", "Books"));
                }
                else
                {
                    return(View(bookUpdateViewModel));
                }
            }
            else
            {
                return(NotFound());
            }
        }
Ejemplo n.º 4
0
        private List <AuthorInBook> GetAuthorInBook(BookUpdateViewModel bookView, IEnumerable <int> authorId)
        {
            var  authorInBook = new List <AuthorInBook>();
            var  mapper       = new AutoMapperForBook();
            Book book         = mapper.Mapp(bookView);

            if (authorId != null)
            {
                foreach (int id in authorId)
                {
                    Author author = _authorRepository.Get(id);
                    authorInBook.Add(new AuthorInBook
                    {
                        Author = author,
                        Book   = book
                    });
                }
            }

            if (authorId == null)
            {
                authorInBook.Add(new AuthorInBook {
                    Book = book
                });
            }
            return(authorInBook);
        }
Ejemplo n.º 5
0
        public async Task <ActionResult> Edit(int id, BookUpdateViewModel bookUpdateViewModel)
        {
            var model = await _bookService.GetByIdAsync(id);

            if (model == null)
            {
                // return NotFound();
                return(View(bookUpdateViewModel));
            }

            try
            {
                model.Author      = bookUpdateViewModel.Author;
                model.Title       = bookUpdateViewModel.Title;
                model.CoverUrl    = bookUpdateViewModel.CoverUrl;
                model.ReleaseDate = bookUpdateViewModel.ReleaseDate;
                model.Price       = bookUpdateViewModel.Price;
                model.Id          = id;
                await _bookService.UpdateAsync(model);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View(bookUpdateViewModel));
            }
        }
        public ActionResult Edit(int id)
        {
            var book = _unitOfWork.BookRepository.Find(x => x.Id == id).Include(y => y.User).Include(v => v.Category).FirstOrDefault();

            if (book == null)
            {
                return(NotFound());
            }

            var viewModel = new BookUpdateViewModel()
            {
                BookId         = book.Id,
                Name           = book.Name,
                Description    = book.Description,
                Author         = book.Author,
                Draft          = book.Draft,
                UserId         = book.User.Id,
                ReleaseYear    = book.ReleaseYear,
                CategoryId     = book.Category.Id,
                CategoriesList = _unitOfWork.CategoryRepository.GetAll().
                                 Select(x => new SelectListItem
                {
                    Text  = x.Name,
                    Value = x.Id.ToString()
                }).ToList(),
                UsersList = UserManager.Users.ToList()
                            .Select(y => new SelectListItem
                {
                    Text  = y.Email,
                    Value = y.Id
                }).ToList()
            };

            return(View(viewModel));
        }
Ejemplo n.º 7
0
        public IActionResult Update(BookUpdateViewModel model)
        {
            var bookEntity = _mapper.Map <Book>(model);

            _bookService.RemoveRelationForAuthor(model.BookId);
            _bookService.Update(bookEntity);
            _bookService.AddRelationForAuthor(bookEntity.BookAuthors);
            return(RedirectToAction("Index", "Book"));
        }
Ejemplo n.º 8
0
        public ActionResult Update(int bookId)
        {
            var model = new BookUpdateViewModel
            {
                Book       = _bookService.GetById(bookId).Data,
                Categories = _categoryService.GetList().Data
            };

            return(View(model));
        }
Ejemplo n.º 9
0
 public ActionResult Edit(BookUpdateViewModel bookView, List <int> authorsMultiSelect)
 {
     if (bookView == null)
     {
         return(HttpNotFound());
     }
     if (ModelState.IsValid)
     {
         _bookService.Update(bookView, authorsMultiSelect);
     }
     return(RedirectToAction("Index"));
 }
Ejemplo n.º 10
0
 public ActionResult Create(BookUpdateViewModel bookView, IEnumerable <int> authorsMultiSelect)
 {
     if (bookView == null)
     {
         return(HttpNotFound());
     }
     if (ModelState.IsValid)
     {
         _bookService.CreateBook(bookView, authorsMultiSelect);
     }
     return(RedirectToAction("Index"));
 }
        // PUT api/values/5
        public async Task <IHttpActionResult> Put(int id, [FromBody] BookUpdateViewModel book)
        {
            var bookToUpdate = await _uow.BookRepository
                               .GetFirstOrDefaultAsync(
                x => x.IdBook.Equals(id),
                null,
                q => q.Include(x => x.Sages),
                disableTracking : false);

            if (bookToUpdate == null)
            {
                return(NotFound());
            }

            bookToUpdate.Name        = book.Book.Name;
            bookToUpdate.Description = book.Book.Description;

            var selectedSages = new HashSet <int>(book.SelectedSages);
            var bookSages     = new HashSet <int>(bookToUpdate.Sages.Select(c => c.IdSage));

            var sages = await _uow.SageRepository.GetAllAsync(disableTracking : false);

            foreach (var sage in sages)
            {
                if (selectedSages.Contains(sage.IdSage))
                {
                    if (!bookSages.Contains(sage.IdSage))
                    {
                        bookToUpdate.Sages.Add(sage);
                    }
                }
                else
                {
                    if (bookSages.Contains(sage.IdSage))
                    {
                        bookToUpdate.Sages.Remove(sage);
                    }
                }
            }

            await _uow.BookRepository.UpdateAsync(bookToUpdate);

            if (await _uow.SaveAsync())
            {
                return(Ok());
            }

            return(BadRequest());
        }
Ejemplo n.º 12
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(HttpNotFound());
            }
            BookUpdateViewModel book = _bookService.GetBookEdit(id.Value);

            if (book == null)
            {
                return(HttpNotFound());
            }
            ViewData["Authors"] = _bookService.GetAuthors();
            return(View(book));
        }
Ejemplo n.º 13
0
        public async Task <ActionResult> Edit(BookViewModel bookViewModel)
        {
            if (ModelState.IsValid)
            {
                var book = new BookUpdateViewModel
                {
                    Book          = bookViewModel.Book,
                    SelectedSages = bookViewModel.SelectedSages
                };

                await _bookApiService.PutAsync(book.Book.IdBook, book, "Bearer " + Session["ApiAccessToken"]);

                return(RedirectToAction("Index"));
            }

            return(View(bookViewModel));
        }
Ejemplo n.º 14
0
        public async Task <ActionResult> Edit(int id)
        {
            var model = await _bookService.GetByIdAsync(id);

            if (model == null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            var editModel = new BookUpdateViewModel
            {
                Author      = model.Author,
                Title       = model.Title,
                CoverUrl    = model.CoverUrl,
                Price       = model.Price,
                ReleaseDate = model.ReleaseDate
            };

            return(View(editModel));
        }
        public IActionResult Update(int id)
        {
            var book = this.bookRepository.Get(id);

            if (book != null)
            {
                BookUpdateViewModel bookUpdateViewModel = new BookUpdateViewModel
                {
                    ISBN            = book.ISBN,
                    Title           = book.Title,
                    Author          = book.Author,
                    PublicationYear = book.PublicationYear,
                    Type            = book.Type
                };
                return(View(bookUpdateViewModel));
            }
            else
            {
                return(NotFound());
            }
        }
        public async Task <ActionResult> Edit(BookUpdateViewModel viewModel)
        {
            try
            {
                var book = _unitOfWork.BookRepository.Find(x => x.Id == viewModel.BookId).Include(y => y.User).Include(v => v.Category).FirstOrDefault();

                if (book == null)
                {
                    return(NotFound());
                }

                var user = await UserManager.FindByIdAsync(viewModel.UserId);

                var category = _unitOfWork.CategoryRepository.Get(x => x.Id == viewModel.CategoryId);
                if (!ModelState.IsValid)
                {
                    return(View(viewModel));
                }

                book.Name        = viewModel.Name;
                book.Description = viewModel.Description;
                book.Author      = viewModel.Author;
                book.ReleaseYear = viewModel.ReleaseYear;
                book.Draft       = viewModel.Draft;
                book.User        = user;
                book.Category    = category;
                _unitOfWork.BookRepository.Update(book);

                if (_unitOfWork.Complete() > 0)
                {
                    return(RedirectToAction(nameof(Index)));
                }
                return(View(viewModel));
            }
            catch
            {
                return(View(viewModel));
            }
        }
        public async Task <IActionResult> Update(int id, BookUpdateViewModel viewModel)
        {
            if (!await _sessionService.IsAuthentificateAsync())
            {
                return(RedirectToAction("Login", "Library"));
            }
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            var book = await _bookService.GetAsync(id);

            if (book == null || book.LibraryId != _sessionService.GetCurrentLibraryId())
            {
                ViewData["exist"] = false;
                return(View());
            }

            var model = _mapper.Map(viewModel, book);

            _db.Books.Update(model);
            if (viewModel.File != null)
            {
                var image = new BookPicture
                {
                    BookId   = book.Id,
                    FullPath = _fileService.SaveFile(viewModel.File),
                    Size     = viewModel.File.Length
                };
                _db.Add(image);
            }

            await _db.SaveChangesAsync();

            ViewData["exist"]   = true;
            TempData["updated"] = true;
            return(View(viewModel));
        }
Ejemplo n.º 18
0
        private void UpdateRepository(BookUpdateViewModel bookView, List <int> authorId)
        {
            List <AuthorInBook> authorInBookReposytory = _authorInBookRepository.GetBook(bookView.Id).ToList();

            if (authorInBookReposytory.Count == authorId.Count)
            {
                // количество авторов равно, изменение таблицы не требуется
            }
            if (authorInBookReposytory.Count > authorId.Count)//требуется удаление
            {
                var removedList = new List <AuthorInBook>(authorInBookReposytory);
                foreach (int id in authorId)
                {
                    AuthorInBook authorInBookRemove = removedList.Find(x => x.Author.Id == id);
                    removedList.Remove(authorInBookRemove);
                }
                _authorInBookRepository.Delete(removedList);
            }
            if (authorInBookReposytory.Count < authorId.Count)
            {
                Book bookAdd = _bookRepository.Get(bookView.Id);
                var  addList = new List <AuthorInBook>();
                foreach (AuthorInBook idAuthorInBook in authorInBookReposytory)
                {
                    int id = authorId.Find(x => x == idAuthorInBook.Author.Id);
                    authorId.Remove(id);
                }
                foreach (int id in authorId)
                {
                    var author = _authorRepository.Get(id);
                    addList.Add(new AuthorInBook()
                    {
                        Book = bookAdd, Author = author
                    });
                }
                _authorInBookRepository.Create(addList);
            }
        }
        public ActionResult UpdateBook(BookUpdateViewModel book)
        {
            if (!ModelState.IsValid)
            {
                return(new HttpStatusCodeResult(422));
            }
            var bookFromDb = this.unitOfWork.Books.GetById(book.Id);

            Mapper.Map <BookUpdateViewModel, Book>(book, bookFromDb);
            bookFromDb.Authors.RemoveAll(a => book.SelectedAuthors.All(f => f != a.Id));

            if (book.SelectedAuthors != null)
            {
                var newAuthorsIds = book.SelectedAuthors.Except(bookFromDb.Authors.Select(s => s.Id));
                var newAuthors    = this.unitOfWork.Authors.Find(f => newAuthorsIds.Any(a => a == f.Id));
                bookFromDb.Authors.AddRange(newAuthors);
            }

            this.unitOfWork.Books.Update(bookFromDb.Id, bookFromDb);
            this.unitOfWork.Save();

            return(RedirectToAction("Index", "Home"));
        }
        // return view for updating book
        public ActionResult ManageUpdateBook(int bookId)
        {
            BookUpdateViewModel viewModel = new BookUpdateViewModel();

            var book = this.unitOfWork.Books.GetById(bookId);

            viewModel = Mapper.Map <Book, BookUpdateViewModel>(book);

            var authors    = this.unitOfWork.Authors.GetAll().OrderBy(a => a.Name).ToList();
            var publishers = this.unitOfWork.Publishers.GetAll().OrderBy(p => p.Name).ToList();

            viewModel.SelectedPublishers = new SelectList(publishers, "Id", "Name", book.PublisherId);

            viewModel.SelectedAuthors = book.Authors?
                                        .Where(aut => authors.Select(t => t.Id).Any(n => n == aut.Id))
                                        .Select(f => f.Id).ToList();

            viewModel.MultiSelectedAuthors = new MultiSelectList(authors, "Id", "Name", viewModel.SelectedAuthors);

            ViewBag.Id = bookId;

            return(View(viewModel));
        }
Ejemplo n.º 21
0
        public BookUpdateViewModel GetBookEdit(int id)
        {
            List <AuthorInBook> books = _authorInBookRepository.GetBook(id).ToList();
            var bookView = new BookUpdateViewModel();

            if (books != null)
            {
                bookView = books.GroupBy(x => x.Book.Id).Select(x => new BookUpdateViewModel()
                {
                    Id                = x.First().Book.Id,
                    NameBook          = x.First().Book.NameBook,
                    NumberPages       = x.First().Book.NumberPages,
                    DatePublishing    = x.First().Book.DatePublishing,
                    PublishingCompany = x.First().Book.PublishingCompany
                }).First();

                bookView.Authors = new List <AuthorFullNameViewModel>();
                foreach (AuthorInBook authorInBook in books)
                {
                    bookView.Authors.Add(new AuthorFullNameViewModel(authorInBook.Author.Id, authorInBook.Author.FirstName, authorInBook.Author.LastName));
                }
            }
            return(bookView);
        }
 public IActionResult Update(BookUpdateViewModel viewModel)
 {
     _service.Update(viewModel);
     return(Ok());
 }
Ejemplo n.º 23
0
        public void CreateBook(BookUpdateViewModel bookView, IEnumerable <int> authorId)
        {
            IEnumerable <AuthorInBook> authorInBook = GetAuthorInBook(bookView, authorId);

            _authorInBookRepository.Create(authorInBook);
        }
 public void Update(BookUpdateViewModel viewModel)
 {
 }
Ejemplo n.º 25
0
 public Book Mapp(BookUpdateViewModel book)
 {
     Mapper.Initialize(m => m.CreateMap <BookUpdateViewModel, Book>());
     return(Mapper.Map <BookUpdateViewModel, Book>(book));
 }