public async Task<IHttpActionResult> PutBook(int id, Book book)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != book.Id)
            {
                return BadRequest();
            }

            db.Entry(book).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        // PUT api/Books/5
        public HttpResponseMessage PutBook(int id, Book book)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != book.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(book).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        public IHttpActionResult PostBook(Book book)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Books.Add(book);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = book.Id }, book);
        }
        public async Task<IHttpActionResult> PutBook(int id, Book book)
        {
            if (!ModelState.IsValid)
            {
                System.Diagnostics.Trace.TraceError("No Book Added");

                return BadRequest(ModelState);
            }

            if (id != book.Id)
            {
                System.Diagnostics.Trace.TraceError("Bad Request");
                return BadRequest();
            }

            db.Entry(book).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    System.Diagnostics.Trace.TraceError("Book ALready exists!");
                    return NotFound();
                }
                else
                {
                    System.Diagnostics.Trace.TraceError("Concurrency Exception!");
                    throw;
                }
            }

            System.Diagnostics.Trace.Flush();

            return StatusCode(HttpStatusCode.NoContent);
        }
        public async Task<IHttpActionResult> PostBook(Book book)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Books.Add(book);
            await db.SaveChangesAsync();

            // New code:
            // Load author name
            db.Entry(book).Reference(x => x.Author).Load();

            var dto = new BookDTO()
            {
                Id = book.Id,
                Title = book.Title,
                AuthorName = book.Author.Name
            };

            return CreatedAtRoute("DefaultApi", new { id = book.Id }, dto);
        }
        public async Task<IHttpActionResult> PostBook(Book book)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Books.Add(book);
            await db.SaveChangesAsync();

            db.Entry(book).Reference(x => x.Author).Load();

            //selecting BookDTO class for lazy loading with authorName in book table
            var dto = new BookDTO()
            {
                Id = book.Id,
                Title = book.Title,
                AuthorName = book.Author.Name
            };

            return CreatedAtRoute("DefaultApi", new { id = book.Id }, dto);
        }
        public async Task<IHttpActionResult> PostBook(Book book)
        {
            Uri uri = Request.RequestUri;
            SimpleLog.WriteStdOut(uri.ToString());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Books.Add(book);
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = book.Id }, book);
        }
Exemple #8
0
        public static void SeedData(this IApplicationBuilder app)
        {
            var context = app.ApplicationServices.GetService<BookServiceContext>();


            IQueryable<Book> bookQuery = from Book
                                            in context.Books select Book;

            if (bookQuery.Count() > 0)
            {
                return;
            }
            else
            {
                
                Author firstAuth = new Author() { Name = "Jane Austen" };
                Author secondAuth = new Author() { Name = "Charles Dickens" };
                Author thirdAuth = new Author() { Name = "Miguel de Cervantes" };

                context.Authors.Add(firstAuth);
                context.Authors.Add(secondAuth);
                context.Authors.Add(thirdAuth);
                context.SaveChanges();

                //get new Id from Author updates
                IQueryable<int> authorIDQuery = from Author in context.Authors select Author.Id;

                int[] arr = new int[authorIDQuery.Count()];
                int i = 0;

                foreach (var authIdent in authorIDQuery)
                {
                    arr[i] = authIdent;
                    ++i;
                }
                Book firstBook = new Book()
                {
                    Title = "Pride and Prejudice",
                    Year = 1813,
                    AuthorId = arr[0],
                    Price = 9.99M,
                    Genre = "Comedy of manners"
                };
                Book secondBook = new Book()
                {
                    Title = "Northanger Abbey",
                    Year = 1817,
                    AuthorId = arr[0],
                    Price = 12.95M,
                    Genre = "Gothic parody"
                };
                Book thirdBook = new Book()
                {
                    Title = "David Copperfield",
                    Year = 1850,
                    AuthorId = arr[1],
                    Price = 15,
                    Genre = "Bildungsroman"
                };
                Book fourthBook = new Book()
                {

                    Title = "Don Quixote",
                    Year = 1617,
                    AuthorId = arr[2],
                    Price = 8.95M,
                    Genre = "Picaresque"
                };

                context.Books.Add(firstBook);
                context.Books.Add(secondBook);
                context.Books.Add(thirdBook);
                context.Books.Add(fourthBook);

                context.SaveChanges();
            }
        }