Exemplo n.º 1
0
        public async Task <ActionResult <GetBookDetailsResponse> > AddABook([FromBody] PostBookRequest request)
        {
            // 1 Validate it. If Not - return a 400 Bad Request, optionally with some info
            // programmatic, imperative validation
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // 2. Save it in the database
            //    - Turn a PostBookRequest -> Book
            var bookToSave = _mapper.Map <Book>(request);

            //    - Add it to the Context
            _context.Books.Add(bookToSave);
            //    - Tell the context to save the changes.
            await _context.SaveChangesAsync();

            //    - Turn that saved book into a GetBookDetailsResponse
            var response = _mapper.Map <GetBookDetailsResponse>(bookToSave);

            // 3. Return:
            //    - 201 Created Status Code
            //    - A location header with the Url of the newly created book.
            //    - A copy of the newly created book (what they would get if they followed the location)

            return(CreatedAtRoute("books#getbookbyid", new { id = response.Id }, response));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddABook([FromBody] PostBookRequest bookToAdd)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var book = new Book
            {
                Title  = bookToAdd.Title,
                Author = bookToAdd.Author,
                Genre  = bookToAdd.Genre ?? "Unknown"
            };

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

            var bookToReturn = new GetBookResponseDocument
            {
                Id     = book.Id,
                Title  = book.Title,
                Author = book.Author,
                Genre  = book.Genre
            };

            return(CreatedAtRoute("books#getabook", new { id = book.Id }, bookToReturn));
        }
        public async Task <ActionResult <GetABookResponse> > AddABook([FromBody] PostBookRequest bookToAdd)
        {
            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,
                Title         = book.Title,
                Author        = book.Author,
                Genre         = book.Genre,
                NumberOfPages = book.NumberOfPages
            };

            return(CreatedAtRoute("books#getabook", new { id = book.Id }, response));
        }
        public async Task <ActionResult> AddBook([FromBody] PostBookRequest request)
        {
            // 1. Do validation.
            //    - If it isn't valid, return a 400, perhaps with some information.
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState)); // 400
            }
            else
            {
                // 2. Change the domain (do the work)
                //    - add the book to the database
                // PostBookRequest -> Book
                var bookToAdd = _mapper.Map <Book>(request);
                _context.Books.Add(bookToAdd);
                await _context.SaveChangesAsync();

                // 3. Return:
                //    - Status Code 201 (Created)
                //    - Add a birth announcement. That is a location header with the URL of
                //      the newly created resource. e.g. Location: http://localhost:1337/books/42
                //    - It is super nice to just give them a copy of the newly created resource
                //      This must be exactly the same as they would get by following the location header.
                var response = _mapper.Map <GetBookDetailsResponse>(bookToAdd);
                return(CreatedAtRoute("books#getbookbyid", new { id = response.Id }, response));
            }
        }
        public async Task <GetBookDetailsResponse> AddBook(PostBookRequest bookToAdd)
        {
            var book = _mapper.Map <Book>(bookToAdd);

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

            var response = _mapper.Map <GetBookDetailsResponse>(book);

            return(response);
        }
 public ActionResult AddABook([FromBody] PostBookRequest request)
 {
     if (ModelState.IsValid)
     {
         var response = new GetBookResponse(request.Title, request.Author, request.NumberOfPages, new Random().Next(300, 1545));
         return(Ok(response));
     }
     else
     {
         return(BadRequest(ModelState));
     }
 }
Exemplo n.º 7
0
        public async Task <ActionResult <GetBookDetailsResponse> > AddABook([FromBody] PostBookRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var bookToSave = _mapper.Map <Book>(request);

            _context.Books.Add(bookToSave);
            await _context.SaveChangesAsync();

            var response = _mapper.Map <GetBookDetailsResponse>(bookToSave);

            return(CreatedAtRoute("books#getbookbyid", new { id = response.Id }, response));
        }
        public async Task CanAddABook()
        {
            var bookToAdd = new PostBookRequest
            {
                title         = "Composing Electronic Music",
                author        = "Smihht",
                genre         = "Non-Fiction",
                numberOfPages = 322
            };
            var response = await Client.PostAsJsonAsync("/books", bookToAdd);

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            var location      = response.Headers.Location.LocalPath; // books/4
            var getItResponse = await Client.GetAsync(location);

            var responseData = await getItResponse.Content.ReadAsAsync <BookDetailsResponse>();

            Assert.Equal(bookToAdd.title, responseData.title);
            Assert.Equal(bookToAdd.author, responseData.author);
            //etc etc.
        }
Exemplo n.º 9
0
        public async Task <ActionResult <GetBookDetailsResponse> > AddBook([FromBody] PostBookRequest bookToAdd)
        {
            // 1. Validate (if not, return a 400)
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                // 2. Make the change
                //    a. Add it to the database (so map from PostBookCreate -> Book)
                var book = _mapper.Map <Book>(bookToAdd);
                _context.Books.Add(book);
                //    b. Save the changes.
                await _context.SaveChangesAsync();

                // 3. Return a 201 Created
                //    a. With a location header of the new book
                //    b. A representation of the book they would get if they followed the location header
                //       1. so, map from Book -> GetBookDetailsResponse (We have this already!)
                var response = _mapper.Map <GetBookDetailsResponse>(book);
                return(CreatedAtRoute("books#getbookdetails", new { id = response.Id }, response));
            }
        }
        public async Task <ActionResult <GetBookDetailsResponse> > AddBook([FromBody] PostBookRequest bookToAdd)
        {
            GetBookDetailsResponse response = await _bookCommands.AddBook(bookToAdd);

            return(CreatedAtRoute("books#getbookdetails", new { id = response.Id }, response));
        }