Esempio n. 1
0
        /// <summary>
        /// POST: api/Books
        /// use DTO
        /// </summary>
        /// <param name="book"></param>
        /// <returns></returns>
        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));
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        public async Task<IActionResult> PostBook([FromBody] Book book)
        {
            if (!ModelState.IsValid)
            {
                return HttpBadRequest(ModelState);
            }

            _context.Books.Add(book);
            try
            {
                await _context.SaveChangesAsync();

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

                return CreatedAtRoute("GetBook", new { id = book.Id }, dto);
            }
            catch (DbUpdateException)
            {
                if (BookExists(book.Id))
                {
                    return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
                }
                else
                {
                    throw;
                }
            }
        }