public IActionResult CreateBook(AddBookViewModel newBook) { //TODO: Build the Author and the Book given the newBook data. Add to DbSets; SaveChanges Writer author = new Writer(); Book book = new Book(); if (newBook.Name != null) { author.Name = newBook.Name; } else { author = _db.Writers.Single(w => w.Id == newBook.AuthorId); } book.Author = author; book.Price = newBook.Price; book.SKU = newBook.SKU; book.Title = newBook.Title; _db.Books.Add(book); _db.SaveChanges(); //Shows the new book using the Search Listing return(RedirectToAction("Index")); }
public IActionResult CreateBook(AddBookViewModel newBook) { //Use ViewModel to build the Writer and then the Book; Add to DbSets; SaveChanges Writer author; if (newBook.AuthorId > 0) { author = _db.Writers.Single(w => w.Id == newBook.AuthorId); } else { author = new Writer { Name = newBook.Name }; } Book book = new Book { Title = newBook.Title, SKU = newBook.SKU, Price = newBook.Price, Author = author }; _db.Books.Add(book); _db.SaveChanges(); //Shows the new book using the Search Listing return(RedirectToAction("Index")); }
public IActionResult CreateBook(AddBookViewModel newBook) { //TODO: Build the Author and the Book given the newBook data. Add to DbSets; SaveChanges Writer writer; Book book; if (newBook.AuthorId != 0) { writer = _db.Writers.Single(w => w.Id == newBook.AuthorId); } else { writer = new Writer() { Name = newBook.Name }; } book = new Book() { Title = newBook.Title, SKU = newBook.SKU, Price = newBook.Price, Author = writer }; //_db.Writers.Add(writer); _db.Books.Add(book); _db.SaveChanges(); //Shows the new book using the Search Listing return(RedirectToAction("Index")); }
public void Post([FromBody] Writer writer) { var writers = _db.Writers.SingleOrDefault(i => i.Id == writer.Id); if (writers == null) { _db.Writers.Add(writer); } _db.SaveChanges(); }
public IActionResult CreateBook(AddBookViewModel newBook) { //TODO: Build the Author and the Book given the newBook data. Add to DbSets; SaveChanges Writer writer = new Writer(); Book book = _db.Books.SingleOrDefault(b => b.SKU == newBook.SKU); //update method if (book != null) { book.Author = writer; book.Title = newBook.Title; book.SKU = newBook.SKU; book.Price = newBook.Price; _db.Update(book); } //add new book to db else //(book == null) { if (newBook.AuthorId != 0) { writer = _db.Writers.Single(w => w.Id == newBook.AuthorId); } else //id == 0 { writer.Name = newBook.Name; _db.Writers.Add(writer); } Book booknew = new Book(); booknew.Author = writer; booknew.Title = newBook.Title; booknew.SKU = newBook.SKU; booknew.Price = newBook.Price; _db.Books.Add(booknew); } /*try * { * var SKUtest = _db.Books.Single(b => b.SKU == newBook.SKU); * Book book = _db.Books.Single(b => b.SKU == newBook.SKU); * book.Author = writer; * book.Title = newBook.Title; * book.SKU = newBook.SKU; * book.Price = newBook.Price; * * _db.Update(book); * } * * catch * { * if (newBook.AuthorId != 0) * { * writer = _db.Writers.Single(w => w.Id == newBook.AuthorId); * } * else * { * writer.Name = newBook.Name; * _db.Writers.Add(writer); * } * Book booknew = new Book(); * booknew.Author = writer; * booknew.Title = newBook.Title; * booknew.SKU = newBook.SKU; * booknew.Price = newBook.Price; * * _db.Books.Add(booknew); * }*/ _db.SaveChanges(); //Shows the new book using the Search Listing return(RedirectToAction("Index")); }
public void Post([FromBody] Writer writer) { _dbc.Writers.Add(writer); _dbc.SaveChanges(); }
public void Post([FromBody] Writer writer) { _ibdc.Add(writer); _ibdc.SaveChanges(); }
public void Post([FromBody] Writer w) { _db.Writers.Add(w); _db.SaveChanges(); }