Example #1
0
        public async Task <IActionResult> PostBook(BookForDetailedDto book)
        {
            if (book == null)
            {
                return(StatusCode(400));
            }

            var bookToSave = _mapper.Map <Book>(book);

            if (!await _repo.AuthorExist(bookToSave.AuthorId))
            {
                bookToSave.AuthorId = null;
            }

            if (book.Id == 0)
            {
                _repo.AddBook(bookToSave);
            }
            else
            {
                _repo.EditBook(bookToSave);
            }

            await _repo.SaveAll();

            return(StatusCode(201, book));
        }
Example #2
0
        public ActionResult Create(BookCreateViewModel viewModel)
        {
            int id = 0;

            try
            {
                if (!ModelState.IsValid)
                {
                    throw new ModelNotValidException("Podaci nisu ispravno uneseni");
                }

                _repository.AddBook(viewModel);
                id = _repository.GetLastBookId();
                TempData["InfoMsg"] = "Nova knjiga uspješno kreirana";
            }
            catch (Exception e)
            {
                if (e is ModelNotValidException)
                {
                    TempData["ErrorMsg"] = e.Message;
                }
                else
                {
                    TempData["ErrorMsg"] = "Pogreška prilikom kreiranja nove knjige";
                }

                viewModel.AuthorsSelectList = _repository.GetAuthorsSelectList();
                return(View(viewModel));
            }

            return(RedirectToAction("Details", new { @id = id }));
        }
Example #3
0
        /// <summary>
        /// Call this function if you want to create a new default index and a few books to the index
        /// </summary>
        static void CreateIndexAndAddSomeBooks()
        {
            Console.WriteLine("Let's create an index called: library ");

            // Use this method to create a new index called "library"
            if (repo.CreateLibraryIndex())
            {
                Console.WriteLine("Index has been created.");
            }

            Console.WriteLine("Let's index a few books ... ");

            var bk1 = new Book()
            {
                Id          = 1,
                ContentId   = "ISBN 978-0-307-27812-8",
                Author      = "Brian Greene",
                Title       = "The Hidden Reality",
                Genre       = "Popular Science",
                PublishDate = new DateTime(2011, 1, 1)
            };


            var bk2 = new Book()
            {
                Id          = 2,
                ContentId   = "ISBN-13: 9781451675047",
                Author      = "Richard Dawkins, Dave McKean",
                Title       = "The Magic of Reality: How We Know What's Really True",
                Genre       = "Science - General & Miscellaneous",
                PublishDate = new DateTime(2012, 9, 11)
            };

            var bk3 = new Book()
            {
                Id          = 3,
                ContentId   = "ISBN-13: 9780062225795",
                Author      = "Richard Dawkins",
                Title       = "An Appetite for Wonder: The Making of a Scientist",
                Genre       = "Biography",
                PublishDate = new DateTime(2013, 9, 24)
            };

            repo.AddBook(bk1);
            repo.AddBook(bk2);
            repo.AddBook(bk3);
        }
Example #4
0
        public ActionResult <Book> CreateBookForAuthor(int authorId, Book book)
        {
            if (!libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            libraryRepository.AddBook(authorId, book);
            libraryRepository.Save();

            return(CreatedAtRoute("GetBookForAuthor", new { bookId = book.Id, authorId = book.AuthorId }, book));
        }
        public IActionResult CreateBookForAuthor(int publisherId, BookForManipulationDto book)
        {
            if (!_repo.PublisherExists(publisherId))
            {
                return(NotFound());
            }
            var bookEntity = _mapper.Map <Book>(book);

            _repo.AddBook(publisherId, bookEntity);
            _repo.Save();
            var bookForReturn = _mapper.Map <BookForReturnDto>(bookEntity);

            return(CreatedAtRoute("GetBookForAuthor", new { publisherId, id = bookForReturn.Id }, bookForReturn));
        }
Example #6
0
        public ActionResult <BookDto> CreateBookForAuthor(Guid authorId, BookCreationDto book)
        {
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var bookEntity = _mapper.Map <Book>(book);

            _libraryRepository.AddBook(authorId, bookEntity);
            _libraryRepository.Save();

            var bookToReturn = _mapper.Map <BookDto>(bookEntity);

            return(CreatedAtAction("GetBookForAuthor", new { authorId = authorId, bookId = bookToReturn.Id }, bookToReturn));
        }
Example #7
0
        public ActionResult Create(Book bk)
        {
            try
            {
                // TODO: Add insert logic here
                if (_repo.AddBook(bk))
                {
                    return(RedirectToAction("Index"));
                }

                return(View());
            }
            catch
            {
                return(View());
            }
        }
Example #8
0
        public ActionResult <Book> Post([FromBody] LibraryAddRequest request)
        {
            var response = libraryRepository.AddBook(request);

            return(Ok(response));
        }
Example #9
0
 public void AddBook(BookViewModel newBook)
 {
     _repo.AddBook(newBook);
 }