Exemplo n.º 1
0
        public IActionResult CreateBook(IEnumerable <int> AuthorIds, IEnumerable <int> CategoryIds,
                                        CreateUpdateBookViewModel bookToCreate)
        {
            using (var client = new HttpClient())
            {
                var book = new Book()
                {
                    Id            = bookToCreate.Book.Id,
                    Isbn          = bookToCreate.Book.Isbn,
                    Title         = bookToCreate.Book.Title,
                    DatePublished = bookToCreate.Book.DatePublished

                                    //books?authId=?&authId=?&catId=?
                };

                var uriParameters = GetAuthorsCategoriesUri(AuthorIds.ToList(), CategoryIds.ToList());

                client.BaseAddress = new Uri("http://localhost:60039/api/");
                var responseTask = client.PostAsJsonAsync($"books?{uriParameters}", book);
                responseTask.Wait();

                var result = responseTask.Result;

                if (result.IsSuccessStatusCode)
                {
                    var readTaskNewBook = result.Content.ReadAsAsync <Book>();
                    readTaskNewBook.Wait();

                    var newBook = readTaskNewBook.Result;

                    TempData["SuccessMessage"] = $"Book {book.Title} was successfully created.";
                    return(RedirectToAction("GetBookById", new { bookId = newBook.Id }));
                }

                if ((int)result.StatusCode == 422)
                {
                    ModelState.AddModelError("", "ISBN already Exists!");
                }
                else
                {
                    ModelState.AddModelError("", "Error! Book not created!");
                }
            }

            var authorList   = new AuthorsList(_authorRepository.GetAuthors().ToList());
            var categoryList = new CategoriesList(_categoryRepository.GetCategories().ToList());

            bookToCreate.AuthorSelectListItems   = authorList.GetAuthorsList(AuthorIds.ToList());
            bookToCreate.CategorySelectListItems = categoryList.GetCategoriesList(CategoryIds.ToList());
            bookToCreate.AuthorIds   = AuthorIds.ToList();
            bookToCreate.CategoryIds = CategoryIds.ToList();

            return(View(bookToCreate));
        }
Exemplo n.º 2
0
        public IActionResult Edit(List <int> authorIds, List <int> categoryIds,
                                  CreateUpdateBookViewModel bookToUpdate)
        {
            using (var client = new HttpClient())
            {
                var bookDto = new Book()
                {
                    Id          = bookToUpdate.Book.Id,
                    Title       = bookToUpdate.Book.Title,
                    Isbn        = bookToUpdate.Book.Isbn,
                    DatePublish = bookToUpdate.Book.DatePublish
                };

                var uriParameters = GetAuthorsCategoriesUri(authorIds.ToList(), categoryIds.ToList());

                client.BaseAddress = new Uri("http://localhost:5000/api/");
                var responseTask = client.PutAsJsonAsync($"books/{bookDto.Id}?{uriParameters}", bookDto);
                responseTask.Wait();

                var result = responseTask.Result;

                if (result.IsSuccessStatusCode)
                {
                    TempData["SuccessMessage"] = $"Book {bookDto.Title} was successfully updated.";
                    return(RedirectToAction("GetBookById", new { bookId = bookDto.Id }));
                }

                if ((int)result.StatusCode == 422)
                {
                    ModelState.AddModelError("", "ISBN already exists!");
                }

                else
                {
                    ModelState.AddModelError("", "Error! Book not updated.");
                }
            }

            var authorList   = new AuthorsList(authorRepository.GetAuthors().ToList());
            var categoryList = new CategoriesList(categoryRepository.GetCategories().ToList());

            bookToUpdate.AuthorSelectListItems   = authorList.GetAuthorsList(authorIds.ToList());
            bookToUpdate.CategorySelectListItems = categoryList.GetCategories(categoryIds.ToList());
            bookToUpdate.AuthorIds   = authorIds.ToList();
            bookToUpdate.CategoryIds = categoryIds.ToList();

            return(View(bookToUpdate));
        }
Exemplo n.º 3
0
        public IActionResult UpdateBook(int bookId)
        {
            var bookDto      = _bookRepository.GetBookById(bookId);
            var authorList   = new AuthorsList(_authorRepository.GetAuthors().ToList());
            var categoryList = new CategoriesList(_categoryRepository.GetCategories().ToList());

            var bookViewModel = new CreateUpdateBookViewModel
            {
                Book = bookDto,
                AuthorSelectListItems = authorList.GetAuthorsList(_authorRepository.GetAuthorsOfABook(bookId)
                                                                  .Select(a => a.Id).ToList()),
                CategorySelectListItems = categoryList.GetCategoriesList(_categoryRepository.GetAllCategoriesOfABook(bookId)
                                                                         .Select(c => c.Id).ToList())
            };

            return(View(bookViewModel));
        }
Exemplo n.º 4
0
        public IActionResult Edit(int bookId)
        {
            var bookDto     = bookRepository.GetBookById(bookId);
            var authorDto   = new AuthorsList(authorRepository.GetAuthors().ToList());
            var categoryDto = new CategoriesList(categoryRepository.GetCategories().ToList());

            var bookViewModel = new CreateUpdateBookViewModel()
            {
                Book = bookDto,
                AuthorSelectListItems = authorDto.GetAuthorsList(authorRepository.GetAllAuthorsFromBook(bookId).
                                                                 Select(a => a.Id).ToList()),
                CategorySelectListItems = categoryDto.GetCategories(categoryRepository.GetCategoriesFromBook(bookId).
                                                                    Select(a => a.Id).ToList())
            };

            return(View(bookViewModel));
        }
Exemplo n.º 5
0
        public IActionResult CreateBook()
        {
            var authors    = authorRepository.GetAuthors();
            var categories = categoryRepository.GetCategories();

            if (authors.Count() <= 0 || categories.Count() <= 0)
            {
                ModelState.AddModelError("", "Some kind or error getting authors or categories");
            }

            var authorList   = new AuthorsList(authors.ToList());
            var categoryList = new CategoriesList(categories.ToList());

            var createUpdateBook = new CreateUpdateBookViewModel {
                AuthorSelectListItems   = authorList.GetAuthorsList(),
                CategorySelectListItems = categoryList.GetCategories()
            };

            return(View(createUpdateBook));
        }