public void AddNewBook(AddBookBindingModel bm, ApplicationUser currentUser) { var authors = bm .AuthorName .Split(',') .Select(name => new Author { Name = name.Trim(), }) .ToList(); Image image = new Image() { Url = bm.ImageUrl }; Book book = new Book() { Authors = authors, Title = bm.Title, Image = image, }; this.Context.Books.Add(book); this.Context.SaveChanges(); }
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 async Task <ActionResult <Guid> > Post([FromBody] AddBookBindingModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest()); } return(this.Ok(await bookService.AddAsync(mapper.Map <Services.Dtos.Book>(model)))); }
public ActionResult Edit([Bind(Include = "Title,ImageUrl,AuthorName")] AddBookBindingModel bm) { if (ModelState.IsValid) { this.CurrentUser = UserManager.FindById(this.User.Identity.GetUserId()); this.service.AddNewBook(bm, this.CurrentUser); TempData["AddedBook"] = $"Successfully edited book {bm.Title}!"; return(this.RedirectToAction("all", "book")); } return(this.RedirectToAction("edit", bm)); }
public IHttpActionResult PostBook(AddBookBindingModel bindingModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } this.bookService.AddBook(bindingModel); Book newBook = this.bookService.GetNewBooks(bindingModel.Title, bindingModel.ISBN); return(CreatedAtRoute("DefaultApi", new { id = newBook.Id }, newBook)); }
public ActionResult AddBook([Bind(Include = "Id,Title,ImageUrl,Language,Description,Price,Quantity,NumberOfPages,IssueDate,ISBN,Categories, Authors")] AddBookBindingModel bindingModel) { if (ModelState.IsValid) { this.bookService.AddBook(bindingModel); Book newBook = this.bookService.GetNewBooks(bindingModel.Title, bindingModel.ISBN); this.TempData["Success"] = $"Book {bindingModel.Title} was added successfully."; return(RedirectToAction("Details", "Books", new { id = newBook.Id })); } return(View(bindingModel)); }
public IActionResult Add(AddBookBindingModel bindingModel) { Book book = new Book(); book.Title = bindingModel.Title; book.Author = bookService.FindAuthor(bindingModel.AuthorName); book.Description = bindingModel.Description; book.BookCoverImageUrl = bindingModel.BookCoverImageUrl; bookService.AddBook(book); return(RedirectToAction("Index", "Home")); }
public async Task <IActionResult> Post([FromBody] AddBookBindingModel model) { var existsAuthor = await this.authorsService.AuthorExistAsync(model.AuthorId); if (!existsAuthor) { return(this.BadRequest("Author does not exist.")); } var book = this.mapper.Map <AddBookModel>(model); var id = await this.booksService.AddAsync(book); return(this.Ok(id)); }
public IHttpActionResult PostBook([FromBody] AddBookBindingModel bookBindingModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Book book = new Book(bookBindingModel.Title, bookBindingModel.Description, bookBindingModel.EditionType, bookBindingModel.Price, bookBindingModel.Copies, bookBindingModel.ReleaseDate); context.Books.Add(book); context.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = book.Id }, book)); }
public IActionResult Add(AddBookBindingModel bookModel) { if (ModelState.IsValid) { var author = DbContext.Authors.FirstOrDefault(a => a.Name == bookModel.AuthorName); if (author == null) { author = new Author() { Name = bookModel.AuthorName }; DbContext.Authors.Add(author); DbContext.SaveChanges(); } var category = DbContext.Categories.FirstOrDefault(c => c.Name == bookModel.CategoryName); if (category == null) { category = new Category() { Name = bookModel.CategoryName }; DbContext.Categories.Add(category); DbContext.SaveChanges(); } Book book = new Book() { Title = bookModel.Title, AuthorId = author.Id, CategoryId = category.Id, Description = bookModel.Description, CoverImageUrl = bookModel.CoverImageUrl }; DbContext.Books.Add(book); DbContext.SaveChanges(); } return(View()); }
public void AddBook(AddBookBindingModel bindingModel) { Author author = this.Context.Authors .First(a => a.Id.ToString() == bindingModel.Authors); bindingModel.Authors = null; Category category = this.Context.Categories .First(c => c.Id.ToString() == bindingModel.Categories); bindingModel.Categories = null; Book newBook = Mapper.Map <AddBookBindingModel, Book>(bindingModel); newBook.Categories.Add(category); newBook.Authors.Add(author); this.Context.Books.Add(newBook); this.Context.SaveChanges(); }
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 async Task <ActionResult> Add(AddBookBindingModel model) { var result = await _bookService.AddAsync(model.Name, model.Author, model.Price); return(Json(result, JsonRequestBehavior.AllowGet)); }