Exemple #1
0
        public async Task <ActionResult <GetBookDetailsResponse> > AddBook([FromBody] PostBooksRequest 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 <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));
        }
        public async Task <GetABookResponse> AddABook(PostBooksRequest bookToAdd)
        {
            // 1. Decide if it is worthy. (validate it) -- curently validation on controller
            //    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.

            //var book = new Book
            //{
            //    Title = bookToAdd.Title,
            //    Author = bookToAdd.Author,
            //    Genre = bookToAdd.Genre,
            //    InInventory = true,
            //    NumberOfPages = bookToAdd.NumberOfPages
            //};
            var book = Mapper.Map <Book>(bookToAdd);

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

            var response = Mapper.Map <GetABookResponse>(book);

            return(response);
        }
        public async Task <ActionResult <GetBookDetailsResponse> > AddABook([FromBody] PostBooksRequest bookToAdd)
        {
            GetBookDetailsResponse response = await BooksMapper.Add(bookToAdd);


            return(CreatedAtRoute("books#getbookbyid", new { id = response.Id }, response));
        }
        public async Task <GetBookDetailsResponse> Add(PostBooksRequest bookToAdd)
        {
            var book = Mapper.Map <Book>(bookToAdd);

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

            return(Mapper.Map <GetBookDetailsResponse>(book));
        }
        public async Task <GetABookResponse> AddABook(PostBooksRequest bookToAdd)
        {
            var book = Mapper.Map <Book>(bookToAdd);

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

            var response = Mapper.Map <GetABookResponse>(book);

            return(response);
        }
Exemple #7
0
        public async Task <ActionResult <GetBookDetailsResponse> > AddABook([FromBody] PostBooksRequest bookToAdd)
        {
            // Validate it. (if valid, return a 400 Bad Request)
            //if(!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}

            GetBookDetailsResponse response = await BooksMapper.Add(bookToAdd);

            return(CreatedAtRoute("books#getbookbyid", new { id = response.Id }, response));
        }
        public async Task <GetABookResponse> AddABook(PostBooksRequest bookToAdd)
        {
            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 = Mapper.Map <GetABookResponse>(book);

            return(response);
        }
        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));
        }
        public async Task <ActionResult <GetBookDetailsResponse> > AddABook([FromBody] PostBooksRequest bookToAdd)
        {
            // Validate it. (if invalid, return a 400 Bad Request)
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // Don't use imperative validation, use field validation in model class
            // Add it to the domain.
            // - PostBooksRequest -> Books
            var book = new Book
            {
                Title         = bookToAdd.Title,
                Author        = bookToAdd.Author,
                Genre         = bookToAdd.Genre,
                NumberOfPages = bookToAdd.NumberOfPages,
                InInventory   = true
            };

            // - Add it to the context
            _libContext.Books.Add(book);
            // - Have the context save everything.
            await _libContext.SaveChangesAsync(); // this also creates an ID for the book

            // Return a 201 Created Status Code.
            // - Add a location header on the response, e.g. Location: http://server/books/8
            // - Add the entity
            // - Book -> GetBooksDetailResponse

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

            return(CreatedAtRoute("books#getbookbyid", new { id = response.Id }, response));
        }
        public async Task <GetBookDetailsResponse> Add(PostBooksRequest bookToAdd)
        {
            // Add it to the domain.
            //  - PostBooksRequest -> Book
            //var book = new Book
            //{
            //    Title = bookToAdd.Title,
            //    Author = bookToAdd.Author,
            //    Genre = bookToAdd.Genre,
            //    NumberOfPages = bookToAdd.NumberOfPages,
            //    InInventory = true
            //};
            var book = Mapper.Map <Book>(bookToAdd);

            //  - Add it to the Context.
            Context.Books.Add(book);

            //  - Have the context save everything.
            await Context.SaveChangesAsync();


            // Return a 201 Created Status Code.
            //  - Add a location header on the response e.g. Location: http://server/books/8
            //  - Add the entity
            //  - Book -> Get BooksDetailResponse

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

            return(Mapper.Map <GetBookDetailsResponse>(book));

            // return response;
        }
Exemple #12
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));
        }