public IActionResult AddBook(BookInputModel newBook) { if (ModelState.IsValid) { var book = new Book { Title = newBook.Title, Author = newBook.Author, Description = newBook.Description, Genre = newBook.Genre, Price = newBook.Price, ImageLink = newBook.ImageLink }; _accountService.AddNewBook(book); return(View("BookAdded")); } return(View()); }
public IActionResult Create(BookInputModel bookInputModel) { //create a book ViewData["Title"] = "Add book to database"; ViewData["Genres"] = GetGenres(); //fetches list of authors var authorList = _bookService.GetAuthorList(); ViewData["aList"] = authorList as List <BookCave.Models.ViewModels.AuthorsViewModel>; //if input model is valid add book to database and redirect to the new book's details page if (ModelState.IsValid) { _bookService.AddBook(bookInputModel); return(RedirectToAction("Details", bookInputModel.Id)); } //if model is invalid refresh page return(View(bookInputModel)); }
public async Task <BookModel> DeleteBook(BookInputModel deleteBook) { var query = new GraphQLRequest { Query = @" mutation($deleteBook: bookInput!) { deleteBook(deleteBook: $deleteBook) { title author isbn } }", Variables = new { deleteBook } }; var response = await _client.PostAsync(query); return(response.GetDataFieldAs <BookModel>("deleteBook")); }
public void AddBook(BookInputModel model) { var book = new Book { Title = model.Title, Author = model.Author, ReleaseYear = model.ReleaseYear, Genre = model.Genre, ISBN = model.ISBN, Price = model.Price, Stock = model.Stock, TopSeller = model.TopSeller, OnSale = model.OnSale, Discount = model.Discount, Image = model.Image, }; _bookRepo.AddBook(book); }
public void UpdateBookById(BookInputModel book, int id) { var updateBook = _dbContext.Books.FirstOrDefault(r => r.Id == id); if (updateBook == null) { return; /* Throw some exception */ } // Update properties updateBook.Name = book.Name; updateBook.Author = book.Author; updateBook.Description = book.Description; updateBook.ImageUrl = book.ImageUrl; updateBook.Isbn = book.Isbn; updateBook.Category = book.Category; updateBook.Pages = book.Pages; updateBook.ModifiedOn = DateTime.Now; }
public async Task UpdateBookById(int id, BookInputModel book) { var oldBook = _context.Books.FirstOrDefault(b => b.Id == id); if (oldBook == null) { throw new BookNotRegisteredException(); } oldBook.Title = book.Title; oldBook.Description = book.Description; oldBook.Genre = book.Genre; oldBook.Author = book.Author; oldBook.Publisher = book.Publisher; oldBook.IsRead = book.IsRead; oldBook.DateRead = book.IsRead? book.DateRead:null; oldBook.Rate = book.IsRead? book.Rate:null; await _context.SaveChangesAsync(); }
public void UpdateBookById(BookInputModel book, int id) { var updateBook = _bookDbContext.Books.ToList().FirstOrDefault(r => r.Id == id); if (updateBook == null) { return; } updateBook.Name = book.Name; updateBook.Author = book.Author; updateBook.Description = book.Description; updateBook.ImageUrl = book.ImageUrl; updateBook.Isbn = book.Isbn; updateBook.Category = book.Category; updateBook.Pages = book.Pages.HasValue ? book.Pages.Value: updateBook.Pages; updateBook.ModifiedOn = DateTime.Now; _bookDbContext.SaveChanges(); }
public async Task Update(int id, BookInputModel book) { var entity = await _bookContext.Books.FindAsync(id); if (entity == null) { throw new Exception("Employee does not exist"); } _bookContext.Entry(entity).CurrentValues.SetValues(book); try { await _bookContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } }
public async Task <BookViewModel> AddBook(BookInputModel bookInputModel) { var checkBook = _context.Books.FirstOrDefault(b => b.Title == bookInputModel.Title && b.Publisher == bookInputModel.Publisher); if (checkBook != null) { throw new BookAlreadyExistException(); } var book = new Book() { Title = bookInputModel.Title, Description = bookInputModel.Description, Genre = bookInputModel.Genre, Author = bookInputModel.Author, Publisher = bookInputModel.Publisher, IsRead = bookInputModel.IsRead, DateRead = bookInputModel.IsRead? bookInputModel.DateRead:null, Rate = bookInputModel.IsRead? bookInputModel.Rate:null, DateAdded = DateTime.Today, }; _context.Books.Add(book); await _context.SaveChangesAsync(); var bookViewModel = new BookViewModel() { Id = book.Id, Title = book.Title, Description = book.Description, Genre = book.Genre, Author = book.Author, Publisher = book.Publisher, IsRead = book.IsRead, DateRead = book.IsRead ? book.DateRead : null, Rate = book.IsRead ? book.Rate : null, DateAdded = book.DateAdded, }; return(bookViewModel); }
public async Task <IActionResult> PostAsync([FromBody] BookInputModel value) { if (value == null) { return(BadRequest("Invalid input")); } var result = await _booksService.AddAsync(value); if (result == null) { return(BadRequest("Book not inserted")); } return(CreatedAtRoute("BookGetAsync", new { id = result.Id }, result)); }
public async Task <Book> AddReview(BookInputModel review) { using (GraphQLClient graphQlClient = new GraphQLClient("http://*****:*****@" mutation($review:bookInput!){ addBook(review:$review){ title author isbn } }" , Variables = new { review } }; var response = await graphQlClient.PostAsync(query); return(response.GetDataFieldAs <Book>("addBook")); } }
public BookViewModel Put(int id, BookInputModel bookInputModel) { //-- antigo // var book = _mapper.Map<Book>(bookInputModel); // _uow.BookRepository.Update(book); // _uow.Commit(); _uow.BookRepository.DeleteBooksAuthor(id); _uow.Commit(); var book = _mapper.Map <Book>(bookInputModel); foreach (var a in book.BoAuthors) { a.Books = book; } _uow.BookRepository.Update(book); _uow.Commit(); return(_mapper.Map <BookViewModel>(book)); }
public void CreateBook_Should_Return_Error_InvalidInput() { var book = new BookInputModel { Author = "Gosho", CreatedOn = DateTime.UtcNow, Genre = "Ivan", BookCoverImage = null, Title = null, Summary = null }; var controller = new BookCreatorApp.Controllers.BooksController(bookService.Object); controller.ModelState.AddModelError("Title", "StringLength"); var result = controller.CreateBook(book).GetAwaiter().GetResult(); result.Should() .BeOfType <ViewResult>() .Which.Model.Should() .BeOfType <BookInputModel>(); }
public async Task <BookViewModel> AddAsync(BookInputModel bookInputModel) { var book = mapper.Map <Book>(bookInputModel); book.PublishingCompany = await publishingCompanyRepository.GetAsync(bookInputModel.PublishingCompanyId); /* * var authors = new List<Author>(); * * foreach (var authorId in bookInputModel.AuthorIds) * { * authors.Add(await authorRepository.GetAsync(authorId)); * } */ var authors = await Task.WhenAll(bookInputModel.AuthorIds.Select(async a => await authorRepository.GetAsync(a))); /* * book.AuthorsLink = new List<BookAuthor>(); * * foreach (var author in authors) * { * book.AuthorsLink.Add(new BookAuthor() { Book = book, Author = author }); * } */ //book.AuthorsLink = new List<BookAuthor> { new BookAuthor { Book = book, Author = authors[0]} }; book.AuthorsLink = new List <BookAuthor>(); book.AuthorsLink.AddRange(authors.Select(a => new BookAuthor { Book = book, Author = a })); var result = await repository.AddAsync(book); var bookViewModel = mapper.Map <BookViewModel>(result); return(bookViewModel); }
// PUT api/books/{id} public IHttpActionResult Put(int id, BookInputModel book) { if (id < 0) { return(this.BadRequest()); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var bookToEdit = this.context.Books.Find(id); if (bookToEdit == null) { return(this.BadRequest(string.Format("No book with id {0} found", id))); } book.UpdateBook(bookToEdit); this.context.SaveChanges(); return(this.Ok()); }
public void AddBook(BookInputModel bookInputModel) { //returns new book var book = new Book { ISBN = bookInputModel.ISBN, Language = bookInputModel.Language, Image = bookInputModel.Image, Title = bookInputModel.Title, Genre = bookInputModel.Genre, Info = bookInputModel.Info, AuthorId = (int)bookInputModel.AuthorId, Publisher = bookInputModel.Publisher, PageCount = bookInputModel.PageCount, ReleaseYear = (int)bookInputModel.ReleaseYear, Price = (double)bookInputModel.Price, Discount = 0, Rating = 0, RatingCount = 0, Stock = 10 }; _bookRepo.AddBook(book); }
public void SeedDataChangeBook(BookInputModel updatedBook) { _bookRepo.SeedDataChangeBook(updatedBook); }
public void SeedDataCreateBook(BookInputModel model) { _bookRepo.SeedDataCreateBook(model); }
public IHttpActionResult Put(int id, BookInputModel bookModel) { Book book = this.data.Books.GetById(id); if (book == null) { return this.BadRequest("The specified book id is invalid"); } if (bookModel.AuthorId != null) { Author author = this.data.Authors.GetById(bookModel.AuthorId); if (author == null) { return this.BadRequest("Invalid author id"); } book.AuthorId = author.Id; } if (bookModel.Title != null) { book.Title = bookModel.Title; } if (bookModel.Price != null) { book.Price = Convert.ToDecimal(bookModel.Price); } if (bookModel.Copies != null) { book.Copies = Convert.ToInt32(bookModel.Copies); } if (bookModel.Description != null) { book.Description = bookModel.Description; } if (bookModel.Edition != null) { book.Edition = bookModel.Edition; } if (bookModel.Categories != null) { string[] categories = bookModel.Categories.Split(','); for (int index = 0; index < categories.Length; index++) { string currentCategory = categories[index].Trim(); Category category = this.data.Categories.All() .FirstOrDefault(c => c.Name.ToLower() == currentCategory.ToLower()); if (category == null) { return this.BadRequest("Invalid category name " + currentCategory + ". "); } if (!book.Categories.Contains(category)) { book.Categories.Add(category); } } } this.data.SaveChanges(); return this.Ok(); }
public IHttpActionResult Post(BookInputModel bookModel) { if (!this.ModelState.IsValid) { return this.BadRequest("The model is not valid"); } Author bookAuthor = this.data.Authors.GetById(bookModel.AuthorId); if (bookAuthor == null) { return this.BadRequest("Invalid author id"); } Book book = new Book() { Author = bookAuthor, AuthorId = bookAuthor.Id, Copies = Convert.ToInt32(bookModel.Copies), Description = bookModel.Description, Edition = bookModel.Edition, Price = Convert.ToDecimal(bookModel.Price), Title = bookModel.Title }; if (bookModel.Categories != null) { string[] categories = bookModel.Categories.Split(','); for (int index = 0; index < categories.Length; index++) { string currentCategory = categories[index].Trim(); Category category = this.data.Categories.All() .FirstOrDefault(c => c.Name.ToLower() == currentCategory.ToLower()); if (category == null) { return this.BadRequest("Invalid category name " + currentCategory + ". "); } book.Categories.Add(category); } } this.data.Books.Add(book); this.data.SaveChanges(); return this.Ok(new { book.Id }); }
public int CreateBook(BookInputModel book) { return(_context.CreateBook(book)); }
public async Task <IActionResult> UpdateAsync(int id, BookInputModel bookInputModel) { await service.UpdateAsync(id, bookInputModel); return(NoContent()); }
public void AddBook(BookInputModel model) { _bookRepo.AddBook(model); }
public void UpdateBook(BookInputModel book) { _bookRepo.UpdateBook(book); }
public void SeedDataDeleteBook(BookInputModel deleteBook) { _bookRepo.SeedDataDeleteBook(deleteBook); }
public IActionResult AddBook(BookInputModel input) { Console.WriteLine("\n**TYPPI ADF BOOK: " + input.Image); bookService.AddBook(input); return(Ok()); }
public IActionResult Delete(BookInputModel deleteBook) { _bookService.SeedDataDeleteBook(deleteBook); return(RedirectToAction("Index")); }
public int CreateBook(BookInputModel book) { var entity = Mapper.Map <Book>(book); _context.Books.Add(entity); }
public async Task <IActionResult> DeleteBook(BookInputModel reviewModel) { await _productGraphClient.DeleteBook(reviewModel); return(RedirectToAction("Index")); }
public int CreateBook(BookInputModel book) { return(_bookRepository.CreateBook(book)); }
public void AddBook(BookInputModel newBook) { _bookRepo.AddBook(newBook); }
public async Task <IActionResult> AddAsync(BookInputModel bookInputModel) { return(Ok(await service.AddAsync(bookInputModel))); }