Esempio n. 1
0
        public virtual IActionResult CreateBook([FromRoute][Required] Guid?authorId, [FromBody] BookForCreation body)
        {
            //TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            // return StatusCode(201, default(Book));

            //TODO: Uncomment the next line to return response 400 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            // return StatusCode(400, default(ProblemDetails));

            //TODO: Uncomment the next line to return response 404 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            // return StatusCode(404, default(ProblemDetails));

            //TODO: Uncomment the next line to return response 406 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            // return StatusCode(406, default(ProblemDetails));

            //TODO: Uncomment the next line to return response 422 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            // return StatusCode(422, default(ValidationProblemDetails));

            //TODO: Uncomment the next line to return response 500 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            // return StatusCode(500, default(Void));
            string exampleJson = null;

            var example = exampleJson != null
                        ? JsonConvert.DeserializeObject <Book>(exampleJson)
                        : default(Book);            //TODO: Change the data returned

            return(new ObjectResult(example));
        }
        public async Task <IActionResult> AddBook(BookForCreation bookForCreation)
        {
            if (bookForCreation == null)
            {
                throw new ArgumentNullException(nameof(bookForCreation));
            }

            //Mapping BookForCreation object to actual Book entity object
            var bookEntity = _mapper.Map <Entities.Book>(bookForCreation);

            //Call repository service to add the book
            _bookRepository.AddBook(bookEntity);

            //Async call to Save the book to database/store
            await _bookRepository.SaveChangesAsync(); //Author won't be refreshed here

            //Fetch (refresh) the book from the data store, including the author
            await _bookRepository.GetBookAsync(bookEntity.Id);

            //Generate the "201" status code along with "Location" header value containing URI pointed for created author
            return(CreatedAtRoute(
                       "GetBook",
                       new { id = bookEntity.Id },
                       bookEntity));
        }
Esempio n. 3
0
        public async Task <IActionResult> CreateBook([FromBody] BookForCreation book)
        {
            var bookEntity = this.mapper.Map <Entities.Book>(book);

            this.booksRepository.AddBook(bookEntity);

            await this.booksRepository.SaveChangesAsync();

            return(this.CreatedAtRoute("GetBook", new { id = bookEntity.Id }, bookEntity));
        }
Esempio n. 4
0
        public async Task <IActionResult> Post([FromBody] BookForCreation bookToAdd)
        {
            var bookEntity = _mapper.Map <Entities.Book>(bookToAdd);

            bookRepository.AddBook(bookEntity);

            await bookRepository.SaveChangesAsync();

            return(CreatedAtRoute("Get", new { id = bookEntity.Id }, bookEntity));
        }
Esempio n. 5
0
        public async Task <IActionResult> CreateBook([FromBody] BookForCreation bookForCreation)
        {
            var bookToAdd = _mapper.Map <Entities.Book>(bookForCreation);

            _bookRepository.AddBook(bookToAdd);
            await _bookRepository.saveChangesAsyn();

            await _bookRepository.GetBookAsync(bookToAdd.id);

            return(CreatedAtRoute("GetBook", new { id = bookToAdd.id }, bookToAdd));
        }
        public async Task <IActionResult> CreateBook(BookForCreation bookForCreation)
        {
            var bookEntity = _mapper.Map <Entities.Book>(bookForCreation);

            _booksRepository.AddBook(bookEntity);
            await _booksRepository.SaveChangesAsync();

            await _booksRepository.GetBookAsync(bookEntity.Id);

            return(CreatedAtRoute("GetBook", new { id = bookEntity.Id }, bookEntity));
        }
        public async Task <IActionResult> CreateBook([FromBody] BookForCreation book)
        {
            var entity = _mapper.Map <Entities.Book>(book);

            _repository.AddBook(entity);
            await _repository.SaveChangesAsync();

            await _repository.GetBookAsync(entity.Id);

            return(CreatedAtAction("GetBook", new { id = entity.Id }, entity));
        }
Esempio n. 8
0
        public async Task <IActionResult> CreateBook([FromBody] BookForCreation book) //deserialize the request body to BookForCreation instance
        {                                                                             //since the book to create is not the Models.Books nor Entities.Book. so create a class "BookForCreation"
            var bookEntity = _mapper.Map <Entities.Book>(book);

            _booksRepository.AddBook(bookEntity);
            await _booksRepository.SaveChangesAsync();

            //fetch (refetch) the book from data store, including the author
            await _booksRepository.GetBookAsync(bookEntity.Id);

            return(CreatedAtRoute("GetBook", new { id = bookEntity.Id }, bookEntity));
        }
Esempio n. 9
0
        public async Task <IActionResult> CreateBook([FromBody] BookForCreation book)
        {
            var bookEntity = _mapper.Map <Entities.Book>(book);

            _booksRepository.AddBook(bookEntity);

            await _booksRepository.SaveChangesAsync();

            // Fetch (refetch) the book from the data store, including the author.
            await _booksRepository.GetBookAsync(bookEntity.Id);

            return(CreatedAtRoute("GetBook", new { id = bookEntity.Id }, bookEntity));
        }
        public async Task <IActionResult> CreateBook(BookForCreation bookForCreation)
        {
            var book = _mapper.Map <Entities.Book>(bookForCreation);

            _booksRepository.AddBook(book);

            await _booksRepository.SaveChangesAsync();

            await _booksRepository.GetBookAsync(book.Id);  //loads author details into the context


            return(CreatedAtRoute("GetBook", new { id = book.Id }, book));
        }
Esempio n. 11
0
        public async Task <IActionResult> AddBook(BookForCreation newBook)
        {
            var book = _mapper.Map <Entities.Book>(newBook);

            _bookRepository.AddBook(book);

            await _bookRepository.SaveChangesAsync();

            // fetch the book from DB, including the author
            await _bookRepository.GetBookAsync(book.Id);

            return(CreatedAtRoute("GetBook", new { id = book.Id }, book));
        }
Esempio n. 12
0
        public async Task <IActionResult> CreateBook(BookForCreation bookForCreation, [FromServices] IMapper mapper)
        {
            var bookEntity = mapper.Map <Entities.Book>(bookForCreation);

            this.booksRepository.AddBook(bookEntity);

            await this.booksRepository.SaveChangesAsync();

            // Fetch (refresh) the book from the data store, including the author
            await this.booksRepository.GetBookAsync(bookEntity.Id);

            return(CreatedAtRoute(nameof(GetBook), new { id = bookEntity.Id }, bookEntity));
        }
Esempio n. 13
0
        public async Task <IActionResult> CreateBook([FromBody] BookForCreation book)
        {
            var bookEntity = _mapper.Map <Entities.Book>(book);

            _booksRepository.AddBook(bookEntity);

            // you have to add validaiton here
            await _booksRepository.SaveChangesAsync();

            return(CreatedAtRoute("GetBook",
                                  new  { id = bookEntity.Id },
                                  bookEntity));
        }
Esempio n. 14
0
        public async Task <IActionResult> CreateBook(BookForCreation bookForCreation)
        {
            var bookEntity = _mapper.Map <Book>(bookForCreation);

            _bookRepository.AddBook(bookEntity);
            await _bookRepository.SaveChangesAsync();

            // this is called so that author information won't be null in the response.
            await _bookRepository.GetBookAsync(bookEntity.Id);

            // put information data to response headers
            // about the book that is just created
            return(CreatedAtRoute("GetBook", new { id = bookEntity.Id }, bookEntity));
        }
        public async Task <IActionResult> CreateBook(BookForCreation bookForCreation)
        {
            var bookEntity = _mapper.Map <Entities.Book>(bookForCreation);

            _bookRepository.AddBook(bookEntity);
            await _bookRepository.SaveChangesAsync();

            // Fetch (refetch) the book from the data store, including the author
            await _bookRepository.GetBookAsync(bookEntity.Id);

            return(CreatedAtRoute(
                       "GetBook",
                       new { bookId = bookEntity.Id }, // pass through an anonymous object
                       bookEntity));
        }
        public async Task <IActionResult> SaveBook(BookForCreation bookForCreation)
        {
            var book = new Book()
            {
                Title     = bookForCreation.Title,
                Author    = bookForCreation.Author,
                CreatedOn = DateTime.Now,
                UpdatedOn = DateTime.Now
            };

            this.bookService.AddBook(book);
            await this.bookService.SaveAll();

            return(Ok(book));
        }
Esempio n. 17
0
        public async Task <IActionResult> Post([FromBody] BookForCreation bookForCreation)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var bookForDisplay = await bookService.AddBookAsync(bookForCreation);

            if (bookForCreation == null)
            {
                return(StatusCode(500, "Fail with saving data"));
            }

            return(Ok(bookForDisplay));
        }
Esempio n. 18
0
        public async Task <IActionResult> CreateBook([FromBody] BookForCreation book)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var bookEntity = _mapper.Map <Entities.Book>(book);

            _booksRepository.AddBook(bookEntity);
            await _booksRepository.SaveChangesAsync();

            await _booksRepository.GetBookAsync(bookEntity.Id);

            return(CreatedAtRoute("GetBook", new { bookEntity.Id }, bookEntity));
        }
Esempio n. 19
0
        public async Task <IActionResult> CreateBook([FromBody] BookForCreation book)
        {
            try
            {
                var bookEntity = _mapper.Map <Entities.Book>(book);
                _booksRepository.AddBook(bookEntity);
                await _booksRepository.SaveChangesAsync();

                return(CreatedAtRoute("GetBook",
                                      new { id = bookEntity.Id },
                                      bookEntity));
            }
            catch (Exception e)
            {
                return(NotFound(e.Message));
            }
        }
Esempio n. 20
0
        public async Task <ActionResult <Book> > CreateBook(
            Guid authorId,
            [FromBody] BookForCreation bookForCreation)
        {
            if (!await _authorRepository.AuthorExistsAsync(authorId))
            {
                return(NotFound());
            }

            var bookToAdd = _mapper.Map <Entities.Book>(bookForCreation);

            _bookRepository.AddBook(bookToAdd);
            await _bookRepository.SaveChangesAsync();

            return(CreatedAtRoute(
                       "GetBook",
                       new { authorId, bookId = bookToAdd.Id },
                       _mapper.Map <Book>(bookToAdd)));
        }
Esempio n. 21
0
        public async Task <BookForDisplay> AddBookAsync(BookForCreation bookForCreation)
        {
            var book = new Book
            {
                Id             = Guid.NewGuid(),
                Name           = bookForCreation.Name,
                DateOfCreation = DateTime.Now
            };

            await dbContext.Books.AddAsync(book);

            if (await dbContext.SaveChangesAsync() <= 0)
            {
                return(null);
            }

            var bookForDisplay = mapper.Map <BookForDisplay>(book);

            return(bookForDisplay);
        }
        public async Task <ActionResult <Book> > CreateBook(
            Guid authorId,
            [FromBody] BookForCreation bookForCreation)
        {
            if (!await _authorRepository.AuthorExistsAsync(authorId))
            {
                return(NotFound());
            }

            var bookToAdd = new Book {
                Title = bookForCreation.Title, Description = bookForCreation.Title
            };
            await _bookRepository.AddBook(bookToAdd);

            await _bookRepository.SaveChangesAsync();

            return(CreatedAtRoute(
                       "GetBook",
                       new { authorId, bookId = bookToAdd.Id },
                       bookToAdd));
        }
Esempio n. 23
0
        /// <summary>
        /// Create a book for a specific author
        /// </summary>
        /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception>
        /// <param name="authorId">The id of the book author</param>
        /// <param name="body"> (optional)</param>
        /// <returns>Task of ApiResponse (Book)</returns>
        public async System.Threading.Tasks.Task <ApiResponse <Book> > CreateBookAsyncWithHttpInfo(Guid?authorId, BookForCreation body = null)
        {
            // verify the required parameter 'authorId' is set
            if (authorId == null)
            {
                throw new ApiException(400, "Missing required parameter 'authorId' when calling BooksApi->CreateBook");
            }

            var    localVarPath         = "/api/v1/authors/{authorId}/books";
            var    localVarPathParams   = new Dictionary <String, String>();
            var    localVarQueryParams  = new List <KeyValuePair <String, String> >();
            var    localVarHeaderParams = new Dictionary <String, String>(this.Configuration.DefaultHeader);
            var    localVarFormParams   = new Dictionary <String, String>();
            var    localVarFileParams   = new Dictionary <String, FileParameter>();
            Object localVarPostBody     = null;

            // to determine the Content-Type header
            String[] localVarHttpContentTypes = new String[] {
                "application/json",
                "application/vnd.marvin.bookforcreation+json",
                "application/vnd.marvin.bookforcreationwithamountofpages+json"
            };
            String localVarHttpContentType = this.Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes);

            // to determine the Accept header
            String[] localVarHttpHeaderAccepts = new String[] {
                "application/json",
                "application/xml"
            };
            String localVarHttpHeaderAccept = this.Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts);

            if (localVarHttpHeaderAccept != null)
            {
                localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept);
            }

            if (authorId != null)
            {
                localVarPathParams.Add("authorId", this.Configuration.ApiClient.ParameterToString(authorId));                   // path parameter
            }
            if (body != null && body.GetType() != typeof(byte[]))
            {
                localVarPostBody = this.Configuration.ApiClient.Serialize(body); // http body (model) parameter
            }
            else
            {
                localVarPostBody = body; // byte array
            }


            // make the HTTP request
            IRestResponse localVarResponse = (IRestResponse)await this.Configuration.ApiClient.CallApiAsync(localVarPath,
                                                                                                            Method.POST, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams,
                                                                                                            localVarPathParams, localVarHttpContentType);

            int localVarStatusCode = (int)localVarResponse.StatusCode;

            if (ExceptionFactory != null)
            {
                Exception exception = ExceptionFactory("CreateBook", localVarResponse);
                if (exception != null)
                {
                    throw exception;
                }
            }

            return(new ApiResponse <Book>(localVarStatusCode,
                                          localVarResponse.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()),
                                          (Book)this.Configuration.ApiClient.Deserialize(localVarResponse, typeof(Book))));
        }
Esempio n. 24
0
        /// <summary>
        /// Create a book for a specific author
        /// </summary>
        /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception>
        /// <param name="authorId">The id of the book author</param>
        /// <param name="body"> (optional)</param>
        /// <returns>Task of Book</returns>
        public async System.Threading.Tasks.Task <Book> CreateBookAsync(Guid?authorId, BookForCreation body = null)
        {
            ApiResponse <Book> localVarResponse = await CreateBookAsyncWithHttpInfo(authorId, body);

            return(localVarResponse.Data);
        }
Esempio n. 25
0
        /// <summary>
        /// Create a book for a specific author
        /// </summary>
        /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception>
        /// <param name="authorId">The id of the book author</param>
        /// <param name="body"> (optional)</param>
        /// <returns>Book</returns>
        public Book CreateBook(Guid?authorId, BookForCreation body = null)
        {
            ApiResponse <Book> localVarResponse = CreateBookWithHttpInfo(authorId, body);

            return(localVarResponse.Data);
        }