public IHttpActionResult Create(BookBindingModel bookBindingModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var author = this.data.Authors.Find(bookBindingModel.AuthorId);

            if (author == null)
            {
                return(this.BadRequest("Author does not exist - invalid id!"));
            }

            Book book = new Book()
            {
                Title          = bookBindingModel.Title,
                Description    = bookBindingModel.Description,
                Price          = bookBindingModel.Price,
                Copies         = bookBindingModel.Copies,
                EditionType    = bookBindingModel.EditionType,
                AgeRestriction = bookBindingModel.AgeRestriction,
                ReleaseDate    = bookBindingModel.ReleaseDate,
                Author         = author
            };

            string[] categories = bookBindingModel.Categories.Split(new [] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var categoryName in categories)
            {
                Category category = this.data
                                    .Categories
                                    .All()
                                    .FirstOrDefault(c => c.Name == categoryName);

                if (category == null)
                {
                    category = new Category
                    {
                        Name = categoryName
                    };

                    this.data.Categories.Add(category);
                }

                book.Categories.Add(category);
            }

            this.data.Books.Add(book);
            this.data.Save();

            BookViewModel bookViewModel = BookViewModel.ConvertToBookViewModel(book);

            return(this.Ok(bookViewModel));
        }
        public IHttpActionResult GetBook(int id)
        {
            var book = this.data.Books.Find(id);

            if (book == null)
            {
                return(this.BadRequest("Book does not exist - invalid id"));
            }

            var bookViewModel = BookViewModel.ConvertToBookViewModel(book);

            return(this.Ok(bookViewModel));
        }
        public IHttpActionResult UpdateBook(int id, BookBindingModel bookBindingModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var existingBook = this.data.Books.Find(id);

            if (existingBook == null)
            {
                return(this.BadRequest("Book does not exist - invalid id!"));
            }

            Author author = this.data.Authors.Find(bookBindingModel.AuthorId);

            if (author == null)
            {
                return(this.BadRequest("Author does not exist - invalid id!"));
            }

            existingBook.AgeRestriction = bookBindingModel.AgeRestriction;
            existingBook.Author         = author;
            existingBook.Copies         = bookBindingModel.Copies;
            existingBook.Description    = bookBindingModel.Description;
            existingBook.EditionType    = bookBindingModel.EditionType;
            existingBook.Price          = bookBindingModel.Price;
            existingBook.ReleaseDate    = bookBindingModel.ReleaseDate;
            existingBook.Title          = bookBindingModel.Title;

            this.data.Books.Update(existingBook);
            this.data.Save();

            BookViewModel bookViewModel = BookViewModel.ConvertToBookViewModel(existingBook);

            return(this.Ok(bookViewModel));
        }