コード例 #1
0
        public async Task <ActionResult <GetABookResponse> > AddABook([FromBody] PostBooksRequest bookToAdd)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var book = new Book
            {
                Title         = bookToAdd.Title,
                Author        = bookToAdd.Author,
                Genre         = bookToAdd.Genre,
                NumberOfPages = bookToAdd.NumberOfPages,
                InInventory   = true
            };

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

            var response = new GetABookResponse
            {
                Id            = book.Id,
                Title         = book.Title,
                Author        = book.Author,
                Genre         = book.Genre,
                NumberOfPages = book.NumberOfPages,
            };

            return(CreatedAtRoute("books#getabook", new { id = book.Id }, response));
        }
コード例 #2
0
        public async Task <ActionResult> AddABook([FromBody] PostBookCreate bookToAdd)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var book = new Book
            {
                Title         = bookToAdd.Title,
                Author        = bookToAdd.Author,
                Genre         = bookToAdd.Genre,
                NumberOfPages = bookToAdd.NumberOfPages,
                InStock       = true
            };

            Context.Books.Add(book);          // I have no Id!
            await Context.SaveChangesAsync(); // Suddenly I have an Id!

            var response = new GetABookResponse
            {
                Id            = book.Id,
                Title         = book.Title,
                Author        = book.Author,
                Genre         = book.Genre,
                NumberOfPages = book.NumberOfPages
            };

            return(CreatedAtRoute("books#getabook", new { bookId = response.Id }, response));
        }
コード例 #3
0
        public async Task <ActionResult> AddABook([FromBody] PostBookCreate bookToAdd)
        {
            // 3. Add it to the database
            GetABookResponse response = await Mapper.AddBook(bookToAdd);

            return(CreatedAtRoute("books#getabook", new { bookId = response.Id }, response));
        }
コード例 #4
0
        public async Task <ActionResult <GetABookResponse> > GetABook(int id)
        {
            GetABookResponse response = await Mapper.GetBookById(id);

            //if(response == null)
            //{
            //    return NotFound();
            //} else
            //{
            //    return Ok(response);
            //}
            return(this.Maybe(response));
        }
コード例 #5
0
        public async Task <ActionResult> AddABook([FromBody] PostBookCreate bookToAdd)
        {
            // X 1. Need a model for the post [FromBody]
            // 2. Validate the data coming in.
            //    - declarative Validation
            //    - Programmatic validation
            //    - Return a 400 (Bad Request)
            //if(!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}
            // 3. Add it to the database
            GetABookResponse response = await Mapper.AddBook(bookToAdd);

            return(CreatedAtRoute("books#getabook", new { bookId = response.Id }, response));
        }
コード例 #6
0
        public async Task <ActionResult <GetABookResponse> > AddABook([FromBody] PostBooksRequest bookToAdd)
        {
            // 1. Validate - if not send a 400 bad request
            // 2. Add to database.
            // 3. Return 201 Created status code
            //    that contains location header with the URL of the newly created resouce
            //    nice to do - just attach a copy of whatever they would get by following the location header.

            // 1. Decide if it is worthy. (validate it)
            //    If Not, send a 400 Bad Request
            // 2. Add it the database.
            // 3. Return:
            //    a 201 Created
            //    Location header with the URL of the newly created resource (like a birth announcment)
            //    Just attach a copy of whatever they would get by following the location header.

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

            var book = new Book
            {
                Title         = bookToAdd.Title,
                Author        = bookToAdd.Author,
                Genre         = bookToAdd.Genre,
                InInventory   = true,
                NumberOfPages = bookToAdd.NumberOfPages
            };

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

            var response = new GetABookResponse
            {
                Id            = book.Id,
                Author        = book.Author,
                Genre         = book.Genre,
                Title         = book.Title,
                NumberOfPages = book.NumberOfPages
            };

            return(CreatedAtRoute("books#getabook", new { id = book.Id }, book));
        }
コード例 #7
0
        public async Task <ActionResult> AddABook([FromBody] PostBookCreate bookToAdd)
        {
            // X 1. Need a model for the post [FromBody]
            // 2. Validate the data coming in.
            //    - declarative Validation
            //    - Programmatic validation
            //    - Return a 400 (Bad Request)
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // 3. Add it to the database
            var book = new Book
            {
                Title         = bookToAdd.Title,
                Author        = bookToAdd.Author,
                Genre         = bookToAdd.Genre,
                NumberOfPages = bookToAdd.NumberOfPages,
                InStock       = true
            };

            Context.Books.Add(book);          // I have no Id!
            await Context.SaveChangesAsync(); // Suddenly I have an ID!

            // 4. Return (if the post is to a collection)
            //    - a 201 Created status code.
            //    - Add a location header to the response. Location: http://localhost:1337/books/3
            //    - Add an entity to the response that is EXACTLY what they'd get if they followed
            //    - the location header.
            var response = new GetABookResponse
            {
                Id            = book.Id,
                Title         = book.Title,
                Author        = book.Author,
                Genre         = book.Genre,
                NumberOfPages = book.NumberOfPages
            };

            return(CreatedAtRoute("books#getabook", new { bookId = response.Id }, response));
        }
コード例 #8
0
        public async Task <ActionResult <GetABookResponse> > AddABook([FromBody] PostBooksRequest bookToAdd)
        {
            // 1. Validate it
            //    If not valid, send a 400 Bad Request
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // 2. Add it the database
            var book = new Book
            {
                Title         = bookToAdd.Title,
                Author        = bookToAdd.Author,
                Genre         = bookToAdd.Genre,
                NumberOfPages = bookToAdd.NumberOfPages,
                InInventory   = true
            };

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

            var response = new GetABookResponse
            {
                Id            = book.Id,
                Title         = book.Title,
                Author        = book.Author,
                Genre         = book.Genre,
                NumberOfPages = book.NumberOfPages
            };

            // 3. Return:
            //    201 Created
            //    Location header with the URL of the newly created resource (like a birth announcement)
            //    Attach a copy of whatever they would get by following the location header.

            return(CreatedAtRoute("books#getabook", new { id = book.Id }, response));
        }
コード例 #9
0
        public async Task <ActionResult <GetABookResponse> > AddABook([FromBody] PostBooksRequest bookToAdd)
        {
            GetABookResponse response = await Mapper.AddABook(bookToAdd);

            return(CreatedAtRoute("books#getabook", new { id = response.Id }, response));
        }
コード例 #10
0
        public async Task <ActionResult <GetABookResponse> > GetABook(int id)
        {
            GetABookResponse response = await Mapper.GetBookById(id);

            return(this.Maybe(response));
        }