public ActionResult Create(Book book) { db.Entry(book).State = EntityState.Added; db.SaveChanges(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Create(Book book) { using (var client = new HttpClient()) { try { client.BaseAddress = new Uri("https://localhost:44357/api/"); //HTTP GET // var responseTask = client.GetAsync("Books"); string ingestPath = "https://localhost:44357/api/Books"; //var content = new StringContent(book.ToString(), Encoding.UTF8, "application/json"); var stringBook = await Task.Run(() => JsonConvert.SerializeObject(book)); // Wrap our JSON inside a StringContent which then can be used by the HttpClient class var httpContent = new StringContent(stringBook, Encoding.UTF8, "application/json"); var response = await client.PostAsync(ingestPath, httpContent); Task <string> responseString = response.Content.ReadAsStringAsync(); string outputJson = await responseString; //res = JsonConvert.DeserializeObject<DoStuffResult>(outputJson); return(RedirectToAction("Index", "BooK")); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); //"Invalid URI: The Uri string is too long." } } return(View()); }
public JsonResult CreateBook([DataSourceRequest] DataSourceRequest request, DetailedBookViewModel book) { if (book != null && ModelState.IsValid) { var category = this.db.Categories.FirstOrDefault(x => x.Id == int.Parse(book.CategoryName)); var newBook = new Book { Title = book.Title, Description = book.Description, Author = book.Author, Category=category }; this.db.Books.Add(newBook); this.db.SaveChanges(); book.Id = newBook.Id; } return Json(new[] { book }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet); }
public int AddBook(string title, string author, string description, string isbn, decimal price, string bookImageUrl, int categoryId, string userId) { var currentUser = this.users .All() .FirstOrDefault(u => u.Id == userId); if (currentUser == null) { throw new ArgumentException("Current user cannot be found!"); } var newBook = new Book() { Title = title, Author = author, Description = description, Isbn = isbn, Price = price, BookImageUrl = bookImageUrl, AddedOn = DateTime.UtcNow, CategoryId = categoryId, OwnerId = userId, }; this.books.Add(newBook); this.books.SaveChanges(); return newBook.Id; }
public async Task <IActionResult> Delete(int id) { Book libro = null; using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://localhost:44357/api/"); //HTTP GET var responseTask = client.GetAsync(string.Format("books/{0}", id.ToString())); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsAsync <Book>(); readTask.Wait(); libro = readTask.Result; } //else //web api sent error response //{ // //log response status here.. // libros = Enumerable.Empty<Libro>(); // ModelState.AddModelError(string.Empty, "Server error. Please contact administrator."); //} } return(View(libro)); }
public void AddToCart(Book book) { // Get the matching cart and book instances var cartItem = storeDB.Carts.SingleOrDefault( c => c.CartId == ShoppingCartId && c.BookId == book.Id); if (cartItem == null) { // Create a new cart item if no cart item exists cartItem = new Cart { BookId = book.Id, CartId = ShoppingCartId, Count = 1, DateCreated = DateTime.Now }; storeDB.Carts.Add(cartItem); } else { // If the item does exist in the cart, then add one to the quantity cartItem.Count++; } // Save changes storeDB.SaveChanges(); }
private void AddIsbnXElement(Book reviewBook, XElement bookXml) { if (!string.IsNullOrEmpty(reviewBook.ISBN)) { var isbnXml = new XElement("isbn", reviewBook.ISBN); bookXml.Add(isbnXml); } }
private void AddOfficialWebSiteXElement(Book reviewBook, XElement bookXml) { if (!string.IsNullOrEmpty(reviewBook.OfficialWebSite)) { var urlXml = new XElement("url", reviewBook.OfficialWebSite); bookXml.Add(urlXml); } }
public ActionResult Addbook(BookStore.Models.Book book) { BookstoreEntities1 db = new BookstoreEntities1(); db.Books.Add(book); db.SaveChanges(); return(View()); }
public ActionResult Edit(Book book) { if (ModelState.IsValid) { db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(book); }
public void AddBook(Book book) { using (var db = new BooksContext()) { db.Books.Add(book); db.SaveChanges(); } }
public ActionResult Delete(int id) { Book b = db.Books.Find(id); if (b == null) { return(HttpNotFound()); } return(View(b)); }
private void AddAuthorsXElement(Book reviewBook, XElement bookXml) { if (reviewBook.Authors.Any()) { var authorNames = reviewBook.Authors.Select(a => a.Name).OrderBy(a => a); var authorsAsString = string.Join(", ", authorNames); var authorsXml = new XElement("authors", authorsAsString); bookXml.Add(authorsXml); } }
public ActionResult Create(Book book) { if (ModelState.IsValid) { db.Books.Add(book); db.SaveChanges(); return RedirectToAction("Index"); } return View(book); }
public ActionResult CreateBook(Book book) { if (ModelState.IsValid) { db.Books.Add(book); db.SaveChanges(); } return RedirectToAction("ListBook", "Parameter"); }
public JsonResult AddBook(Book book) { if (ModelState.IsValid) { _repository.AddBook(book); return Json(new { item = "Added" }, JsonRequestBehavior.AllowGet); } var allErrors = ModelState.Values.SelectMany(v => v.Errors); return Json(allErrors); }
public bool Update(Book book) { Book bookToUpdate = _books.Find(b => b.id == book.id); if (bookToUpdate != null) { bookToUpdate.name = book.name; bookToUpdate.author = book.author; bookToUpdate.year = book.year; return true; } else return false; }
public ActionResult DeleteConfirmed(int id) { Book b = db.Books.Find(id); if (b == null) { return(HttpNotFound()); } db.Books.Remove(b); db.SaveChanges(); return(RedirectToAction("Index")); }
public IActionResult Create(Book book) { if (ModelState.IsValid) { _context.Book.Add(book); _context.SaveChanges(); return RedirectToAction("Index"); } ViewBag.Authors = new SelectList(_context.Set<Author>().Select(t => new { t.AuthorID, AuthorName = t.FirstName + " " + t.LastName }), "AuthorID", "AuthorName", book.AuthorID); ViewBag.Genres = new SelectList(_context.Set<Genre>(), "GenreID", "GenreName", book.GenreID); return View(book); }
public ActionResult EditBook(int?id) { if (id == null) { return(HttpNotFound()); } Book book = db.Books.Find(id); if (book != null) { return(View(book)); } return(HttpNotFound()); }
public ActionResult Create(Book book) { try { if (ModelState.IsValid) { iBookRepository.InsertBook(book); iBookRepository.Save(); return RedirectToAction("Index"); } } catch (DataException) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists see your system administrator."); } return View(book); }
public void Import(string xmlFilePath) { var booksXml = XElement.Load(xmlFilePath).Elements("book"); foreach (var bookXml in booksXml) { var book = new Book(); var bookTitle = bookXml.Element("title").Value; book.Title = bookTitle; var bookAuthors = this.GetAuthors(bookXml); foreach (var author in bookAuthors) { book.Authors.Add(author); } var bookOfficialWebSite = bookXml.Element("web-site"); if (bookOfficialWebSite != null) { book.OfficialWebSite = bookOfficialWebSite.Value; } var bookReviews = this.GetReviews(bookXml); foreach (var bookReview in bookReviews) { book.Reviews.Add(bookReview); } var bookIsbn = bookXml.Element("isbn"); if (bookIsbn != null) { book.ISBN = bookIsbn.Value; } var bookPrice = bookXml.Element("price"); if (bookPrice != null) { book.Price = decimal.Parse(bookPrice.Value); } this.bookStoreDbContext.Books.Add(book); this.bookStoreDbContext.SaveChanges(); } }
public ActionResult Create(Book book, HttpPostedFileBase image) { if (ModelState.IsValid) { if (image.ContentLength > 0) { var fileName = Path.GetFileName(image.FileName); var path = Path.Combine(Server.MapPath("~/Images"), fileName); image.SaveAs(path); book.Image = fileName; db.Books.Add(book); db.SaveChanges(); return RedirectToAction("Index"); } } ViewBag.CategoryID = new SelectList(db.Categorys, "CategoryID", "Name", book.CategoryID); return View(book); }
public ActionResult EditBook(int Id, Book books) { if (ModelState.IsValid) { var bookItem = db.Books.Single(book => book.Id == Id); bookItem.Author = books.Author; bookItem.ISBN = books.ISBN; bookItem.PathImage = books.PathImage; bookItem.Price = books.Price; bookItem.Publisher = books.Publisher; bookItem.PublishYear = books.PublishYear; bookItem.Quantity = books.Quantity; bookItem.Summary = books.Summary; bookItem.Title = books.Title; db.SaveChanges(); return RedirectToAction("ListBook", "Parameter"); } return View(books); }
// POST: api/Books public HttpResponseMessage Post(Book book) { if (ModelState.IsValid) { if (book.Id == 0) { _db.Books.Add(book); _db.SaveChanges(); return Request.CreateResponse(HttpStatusCode.Created, book); } else { // THe reason they do this is because if the entity has an id the 'create' is counted // as an edit var original = _db.Books.Find(book.Id); original.Title = book.Title; original.Author = book.Author; _db.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK, book); } } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState); }
public async Task <IActionResult> Delete(Book book) { using (var client = new HttpClient()) { try { client.BaseAddress = new Uri("https://localhost:44357/"); var uri = string.Format("api/Books/{0}", book.Id.ToString()); HttpResponseMessage response = await client.DeleteAsync(uri); Task <string> responseString = response.Content.ReadAsStringAsync(); string outputJson = await responseString; return(RedirectToAction("Index", "BooK")); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); //"Invalid URI: The Uri string is too long." } } return(View()); }
public ActionResult EditBook(Book book) { db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); }
public void Add(Book b) { _books.Add(b); }
// PUT api/values/5 public bool Put(Book value) { return rep.Update(value); }