示例#1
0
        public IActionResult Put(long id, [FromBody] Writer value)
        {
            Writer writer = _idbc.Writers.Find(id);

            if (writer == null)
            {
                return(NotFound());
            }
            else
            {
                writer.Name = value.Name;
                _idbc.Update(writer);
                _idbc.SaveChanges();
                return(Ok());
            }
        }
        public IActionResult Put([FromBody] Writer writer, long id)
        {
            var dbw = _dbc.Writers.SingleOrDefault(w => w.Id == id);

            if (dbw != null)
            {
                dbw.Name = writer.Name;
                _dbc.Update(dbw);
                _dbc.SaveChanges();
                return(Ok());
            }
            else
            {
                return(NotFound());
            }
        }
        public IActionResult Put(long id, [FromBody] Writer writer)
        {
            var thisWriter = _ibdc.Writers.SingleOrDefault(w => w.Id == id);

            if (thisWriter == null)
            {
                return(NotFound());
            }
            else
            {
                thisWriter.Name = writer.Name;
                _ibdc.Update(thisWriter);
                _ibdc.SaveChanges();
                return(Ok());
            }
        }
示例#4
0
        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"));
        }