public IHttpActionResult ChangeBook(int id, Book changedBook)
        {
            var book = this.Data.Books
                .Find(id);
            if (book == null)
            {
                return this.NotFound();
            }

            book.Id = changedBook.Id == 0 ? book.Id : changedBook.Id;
            book.Copies = changedBook.Copies == 0 ? book.Copies : changedBook.Copies;
            book.Price = changedBook.Price == 0 ? book.Price : changedBook.Price;
            book.ReleaseDate = changedBook.ReleaseDate ?? book.ReleaseDate;
            book.Title = changedBook.Title == String.Empty ? book.Title : changedBook.Title;
            book.AgeRestriction = changedBook.AgeRestriction;
            book.Edition = changedBook.Edition;
            book.Author = changedBook.Author ?? book.Author;
            book.Categories = changedBook.Categories ?? book.Categories;
            book.Description = changedBook.Description == String.Empty ? book.Description : changedBook.Description;
            book.RelatedBooks = changedBook.RelatedBooks ?? book.RelatedBooks;


            this.Data.SaveChanges();
            return this.Ok("successful change");
        }
        public IHttpActionResult PostBook(AddBookBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newBook = new Book
            {
                Title = model.Title,
                Description = model.Description,
                Price = model.Price,
                Copies = model.Copies,
                EditionType = model.Edition,
                AgeRestriction = model.AgeRestriction,
                ReleaseDate = model.ReleaseDate
            };

            var authorId = new Guid(model.AuthorId);
            var author = this.context.Authors
                .FirstOrDefault(a => a.Id == authorId);

            if (author == null)
            {
                return this.BadRequest("Invalid author id");
            }

            newBook.AuthorId = authorId;

            char[] separator = new char[] { ' ' };
            string[] categories = model.Categories.Split(separator, StringSplitOptions.RemoveEmptyEntries);

            foreach (var categoryName in categories)
            {
                var categoryInDb = this.context.Categories
                    .FirstOrDefault(c => c.Name == categoryName);

                if (categoryInDb == null)
                {
                    Category newCategory = new Category { Name = categoryName };
                    this.context.Categories.Add(newCategory);
                    this.context.SaveChanges();
                    categoryInDb = newCategory;
                }

                newBook.Categories.Add(categoryInDb);
            }

            this.context.Books.Add(newBook);
            this.context.SaveChanges();
            var bookToReturn = this.context.Books
                .Where(b => b.Id == newBook.Id)
                .Select(BookDataModel.DataModel)
                .FirstOrDefault();

            return this.Ok(bookToReturn);
        }
        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 static BookViewModel CreateViewModel(Book book)
 {
     return new BookViewModel
     {
         Id = book.Id,
         Title = book.Title,
         Description = book.Description,
         Price = book.Price,
         Copies = book.Copies,
         Author = AuthorViewModel.CreateViewModel(book.Author),
         EditionType = book.EditionType,
         Categories = book.Categories.AsQueryable().Select(CategoryViewModel.CreateViewModel)
     };
 }
예제 #5
0
        public AuthorBooksViewModel(Book book)
        {
            this.Id = book.Id;
            this.Title = book.Title;
            this.Description = book.Description;
            this.Edition = book.Edition;
            this.Price = book.Price;
            this.Copies = book.Copies;
            this.Categories = new List<BookCategoriesViewModel>();

            foreach (var category in book.Categories)
            {
                this.Categories.Add(new BookCategoriesViewModel(category));
            }
        }
 public AuthorBooksViewModel(Book book)
 {
     this.Id = book.Id;
     this.Title = book.Title;
     this.Description = book.Description;
     this.EditionType = (EditionType)book.EditionType;
     this.Price = book.Price;
     this.Copies = book.Copies;
     this.RelesaeDate = (DateTime)book.RelesaeDate;
     this.AgeRestriction = (AgeRestriction)book.AgeRestriction;
     this.Categories = new List<CategoryBooksViewModel>();
     foreach (var category in book.Categories)
     {
         Categories.Add(new CategoryBooksViewModel(category));
     }
 }
예제 #7
0
        public BookViewModel(Book book)
        {
            this.Id = book.Id;
            this.Title = book.Title;
            this.Description = book.Description;
            this.Edition = book.Edition;
            this.Price = book.Price;
            this.Copies = book.Copies;
            this.AuthorName = book.Author.FirstName + " " + book.Author.LastName;
            this.AuthorId = book.Author.Id;
            this.Categories = new List<BookCategoriesViewModel>();

            foreach (var category in book.Categories)
            {
                this.Categories.Add(new BookCategoriesViewModel(category));
            }
        }
        public BookViewModel(Book book)
        {
            Title = book.Title;
            Description = book.Description;
            Price = book.Price;
            Copies = book.Copies;
            ReleaseDate = book.ReleaseDate;
            AgeRestriction = book.AgeRestriction;
            EditionType = book.EditionType;

            BookCategories = new List<BookCategoryViewModel>();

            foreach (var category in book.Categories)
            {
                BookCategories.Add(new BookCategoryViewModel(category));
            }
        }
예제 #9
0
        public IHttpActionResult AddBook([FromBody] AddBookBindingModel book)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (this.BookShopData.Books.Any(b => b.Title == book.Title))
            {
                return this.BadRequest("Duplicate Title.");
            }

            var newBook = new Book
            {
                Title = book.Title,
                Description = book.Description,
                Price = book.Price,
                Copies = book.Copies,
                Edition = book.Edition,
                AgeRestriction = book.AgeRestriction,
                ReleaseDate = book.ReleaseDate
            };

            if (string.IsNullOrEmpty(book.Categories))
            {
                return this.BadRequest("Categories cannot be empty.");
            }

            var bookCategories = book.Categories.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            
            foreach (var bookCategory in bookCategories)
            {
                var category = this.BookShopData.Categories.FirstOrDefault(c => c.Name == bookCategory) ?? new Category
                {
                    Name = bookCategory
                };

                newBook.Categories.Add(category);
            }

            this.BookShopData.Books.Add(newBook);
            this.BookShopData.SaveChanges();

            return this.Ok(BookViewModel.Create(newBook));
        }
        public static BookViewModel ConvertToBookViewModel(Book book)
        {
            BookViewModel bookViewModel = new BookViewModel
            {
                Id = book.Id,
                Author = AuthorViewModelMinified.ConvertToAuthorViewModel(book.Author),
                Categories = book.Categories.Select(c => c.Name),
                Copies = book.Copies,
                Description = book.Description,
                EditionType = book.EditionType,
                Price = book.Price,
                Title = book.Title,
                AgeRestriction = book.AgeRestriction,
                ReleaseDate = book.ReleaseDate.Date
            };

            return bookViewModel;
        }
예제 #11
0
        public static AuthorBooksViewModel Create(Book book)
        {
            var bookView = new AuthorBooksViewModel
            {
                Title = book.Title,
                Description = book.Description,
                Edition = book.Edition,
                Price = book.Price,
                Copies = book.Copies,
                ReleaseDate = book.ReleaseDate,
                AgeRestriction = book.AgeRestriction,
                Categories = new List<string>(),

            };

            AddCategories(bookView, book.Categories);

            return bookView;
        }
예제 #12
0
        public static BookViewModel Create(Book book)
        {
            var bookView = new BookViewModel
            {
                Id = book.Id,
                Title = book.Title,
                Description = book.Description,
                Edition = book.Edition,
                Price = book.Price,
                Copies = book.Copies,
                ReleaseDate = book.ReleaseDate,
                AgeRestriction = book.AgeRestriction,
                Author = book.Author != null ? book.Author.FirstName + " " + book.Author.LastName : "",
                Categories = new List<string>()
            };

            AddCategories(bookView, book.Categories);

            return bookView;
        }
        public BookFullViewModel(Book book)
        {
            Title = book.Title;
            Description = book.Description;
            Price = book.Price;
            Copies = book.Copies;
            ReleaseDate = book.ReleaseDate;
            AgeRestriction = book.AgeRestriction;
            EditionType = book.EditionType;

            BookCategories = new List<BookCategoryViewModel>();

            foreach (var category in book.Categories)
            {
                BookCategories.Add(new BookCategoryViewModel(category));
            }
            Author = book.Author.FirstName + " " + book.Author.LastName;

            AuthorId = book.AuthorId;
        }
        public IHttpActionResult PutBook(int id, Book changedBook)
        {
            var dbBook = db.Books.FirstOrDefault(b => b.Id == id);
            if(dbBook == null)
            {
                return this.NotFound();
            }

            dbBook.Title = changedBook.Title == String.Empty ? dbBook.Title : changedBook.Title;
            dbBook.Description = changedBook.Description == String.Empty ? dbBook.Description : changedBook.Description;
            dbBook.Price = changedBook.Price == 0 ? dbBook.Price : changedBook.Price;
            dbBook.Copies = changedBook.Copies == 0 ? dbBook.Copies : changedBook.Copies;
            dbBook.EditionType = changedBook.EditionType == null ? dbBook.EditionType : changedBook.EditionType;
            dbBook.AgeRestriction = changedBook.AgeRestriction == null ? dbBook.AgeRestriction : changedBook.AgeRestriction;
            dbBook.RelesaeDate = changedBook.RelesaeDate != null ? changedBook.RelesaeDate : dbBook.RelesaeDate;
            dbBook.AuthorId = changedBook.AuthorId == 0 ? dbBook.AuthorId : changedBook.AuthorId;

            db.SaveChanges();
            string output = String.Format("Book with id {0} changed successfully!", id);

            return this.Ok(output);
        }
 public AuthorBooksTitlesViewModel(Book book)
 {
     this.Title = book.Title;
 }
예제 #16
0
 public SearchBooksViewModel(Book book)
 {
     this.Title = book.Title;
     this.Id = book.Id;
 }
        public IHttpActionResult PostBook(AddBookBindingModel bookModel)
        {
            if(bookModel == null)
            {
                this.ModelState.AddModelError("bookModel", "Book model can not be empty!");
            }
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }           

            var book = new Book()
            {
                Title = bookModel.Title,
                Description = bookModel.Description ?? null,
                Price = bookModel.Price,
                Copies = bookModel.Copies,
                EditionType = bookModel.EditionType,
                AgeRestriction = bookModel.AgeRestriction,
                RelesaeDate = bookModel.RelesaeDate,
                Author = db.Authors.FirstOrDefault(a => a.Id == bookModel.AuthorId)
            };

            var categoryNames = bookModel.Categories.Split(' ');
            foreach (var categoryName in categoryNames)
            {
                var category = db.Categories.FirstOrDefault(c => c.Name == categoryName);
                book.Categories.Add(category);
            }

            db.Books.Add(book);
            db.SaveChanges();

            return this.Ok("Book created");
        }
        public IHttpActionResult CreateBook(BookBindingModel bookModel)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newBook = new Book
            {
                Title = bookModel.Title,
                Description = bookModel.Description,
                Price = bookModel.Price,
                Copies = bookModel.Copies,
                EditionType = bookModel.EditionType,
                AuthorId = bookModel.AuthorId
            };

            var newBookCategoriesNames = bookModel.Categories.Split(' ');
            this.AddCategoriesToBook(newBookCategoriesNames, newBook);

            this.BookShopData.Books.Add(newBook);
            this.BookShopData.SaveChanges();

            var bookViewModel = BookViewModel.CreateViewModel(newBook);

            return this.Ok(bookViewModel);
        }
 private void AddCategoriesToBook(string[] categoryNames, Book book)
 {
     foreach (var categoryName in categoryNames)
     {
         var existingCategory = this.BookShopData.Categories.All()
             .FirstOrDefault(c => c.Name.Equals(categoryName));
         if (existingCategory != null)
         {
             book.Categories.Add(existingCategory);
         }
     }
 }
        public IHttpActionResult PostBook([FromBody]BookPostBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var categories = model.Categories.Split(' ').Select(c => c.Trim()).ToList();
            var categoriesList = new List<Category>();

            foreach (var cat in categories)
            {
                var databaseCategory = context.Categories.FirstOrDefault(c => c.Name == cat);
                if (databaseCategory == null)
                {
                    categoriesList.Add(new Category() { Name = cat });
                }
                else
                {
                    categoriesList.Add(databaseCategory);
                }
            }

            var bookToPost = new Book()
            {
                Title = model.Title,
                Description = model.Description,
                EditionType = model.EditionType,
                Price = model.Price,
                Copies = model.Copies,
                ReleaseDate = model.ReleaseDate,
                AgeRestriction = model.AgeRestriction,
                AuthorId = model.AuthorId,
                Categories = categoriesList
            };

            context.Books.AddOrUpdate(bookToPost);
            context.SaveChanges();

            return this.Ok(bookToPost);
        }
 public BookTitleViewModel(Book book)
 {
     BookTitle = book.Title;
 }
예제 #22
0
        public IHttpActionResult Post([FromBody]BookBindingModel book)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newBook = new Book()
            {
                Title = book.Title,
                Description = book.Decription,
                Price = book.Price,
                Copies = book.Copies,
                Edition = book.Edition,
                AgeRestriction = book.AgeRestriction,
                ReleaseDate = book.ReleaseDate,
                AuthorId = db.Authors
                    .Where(a => a.FirstName == book.AuthorFirstName && a.LastName == book.AuthorLastName).FirstOrDefault().Id
            };

            db.Books.Add(newBook);
            db.SaveChanges();

            var categoriesNames = book.CategoryNames.Split(' ');
            var currentBook = db.Books.Where(b => b.Title == book.Title).FirstOrDefault();

            foreach (var categoryName in categoriesNames)
            {
                var currentCategory = db.Categories.Where(c => c.Name == categoryName).FirstOrDefault();

                currentCategory.Books.Add(currentBook);
            }

            db.SaveChanges();

            return this.Ok(new BookViewModel(currentBook));
        }
예제 #23
0
        public IHttpActionResult CreateBook([FromBody]AddBookBindingModel bookModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest("Wrong model data.");
            }

            if (unitOfWork.AuthorRepository.GetByID(bookModel.AuthorId) == null)
            {
                return Content(HttpStatusCode.BadRequest, "There is no author with this ID.");
            }

            if (unitOfWork.BookRepository.Get(b => b.Title == bookModel.Title).Any())
            {
                return Content(HttpStatusCode.Conflict, "There is already book with this title.");
            }

            string[] categoryNames = bookModel.Categories.Split(new[] {' '});

            Book book = new Book()
            {
                Title = bookModel.Title,
                Description = bookModel.Description,
                Price = bookModel.Price,
                Copies = bookModel.Copies,
                Edition = bookModel.Edition,
                AgeRestriction = bookModel.AgeRestriction,
                ReleaseDate = bookModel.ReleaseDate,
                AuthorId = bookModel.AuthorId
            };

            foreach (string categoryName in categoryNames)
            {
                if (unitOfWork.CategoryRepository.Get(c => c.Name == categoryName).Any())
                {
                    book.Categories.Add(unitOfWork.CategoryRepository.Get(c => c.Name == categoryName).First());
                }
            }

            unitOfWork.BookRepository.Insert(book);

            unitOfWork.Save();

            return this.Ok(book);
        }
 public SearchedBooks(Book book)
 {
     this.Title = book.Title;
     this.Id = book.Id;
 }
        public IHttpActionResult AddBook(BookBindingModel model)
        {
            if (model == null)
            {
                this.ModelState.AddModelError("model", "The model cannot be null - (request is empty).");
            }

            if (!ModelState.IsValid)
            {
                return this.BadRequest(ModelState);
            }

            var categories = model.Categories.Split(' ');
            var categoryList = new List<Category>();

            if (categories.Count() != 0)
            {

                foreach (var categoryName in categories)
                {
                    Category category = new Category()
                    {
                        Name = categoryName
                    };

                    categoryList.Add(category);
                }
            }

            var book = new Book()
            {
                Title = model.Title,
                Description = model.Description ?? null,
                Edition = model.Edition,
                Price = model.Price,
                AgeRestriction = model.Restriction,
                Copies = model.Copies,
                ReleaseDate = DateTime.Now,
                Author = this.Data.Authors
                    .First(a => a.Id == model.AuthorId) ?? null,
                RelatedBooks = this.Data.Books
                    .Where(b => b.Id == model.BookId)
                    .ToList()
                    .Count == 0 ? null : this.Data.Books.Where(b => b.Id == model.BookId).ToList(),
                Categories = categoryList ?? null

            };

            this.Data.Books.Add(book);
            this.Data.SaveChanges();
            return this.Ok("created book  with id = " + book.Id);
        }
 public BookViewModel(Book book)
 {
     Title = book.Title;
 }