Beispiel #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

                                    // book?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.GetAuthorList(AuthorIds.ToList());
            bookToCreate.CategoriesSelectListItems = categoryList.GetCategoryList(CategoryIds.ToList());
            bookToCreate.AuthorIds   = AuthorIds.ToList();
            bookToCreate.CategoryIds = CategoryIds.ToList();

            return(View(bookToCreate));
        }
Beispiel #2
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 bookViewBook = new CreateUpdateBookViewModel
            {
                Book = bookDto,
                AuthorSelectListItems = authorList.GetAuthorList(_authorRepository.GetAuthorOfABook(bookId)
                                                                 .Select(a => a.Id).ToList()),
                CategoriesSelectListItems = categoryList.GetCategoryList(_categoryRepository.GetAllCategoriesOfABook(bookId)
                                                                         .Select(c => c.Id).ToList())
            };

            return(View(bookViewBook));
        }
Beispiel #3
0
        public IActionResult CreateBook()
        {
            var authors    = _authorRepository.GetAuthors();
            var categories = _categoryRepository.GetCategories();

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

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

            var createUpdateBook = new CreateUpdateBookViewModel
            {
                AuthorSelectListItems     = authorList.GetAuthorList(),
                CategoriesSelectListItems = categoryList.GetCategoryList()
            };

            return(View(createUpdateBook));
        }