Esempio n. 1
0
        public async Task <IActionResult> AddNewBook([FromBody] AddNewBookRequest newBookRequest)
        {
            var msg = CreateResponse(HttpStatusCode.InternalServerError, ApiErrorProvider.GetErrorResponse(ApiErrorCodes.InternalServiceError));

            if (!ModelState.IsValid)
            {
                msg = CreateResponse(HttpStatusCode.BadRequest, ApiErrorProvider.GetErrorResponse(ModelState));
            }
            else
            {
                try
                {
                    BookDomain newBookDomain = await _libraryManager.AddNewBook(newBookRequest).ConfigureAwait(false);

                    if (newBookDomain == null)
                    {
                        msg = CreateResponse(HttpStatusCode.BadRequest, ApiErrorProvider.GetErrorResponse(ApiErrorCodes.BadRequest));
                    }
                    else
                    {
                        msg = CreateResponse(HttpStatusCode.Created, newBookDomain);
                    }
                }

                catch (BookIdAlridyPresent ex)
                {
                    msg = CreateErrorResponse(ex.StatusCode, ApiErrorCodes.BookIdNotFound);
                }
                catch (Exception ex)
                {
                    msg = CreateResponse(HttpStatusCode.InternalServerError, ApiErrorProvider.GetErrorResponse(ApiErrorCodes.InternalServiceError));
                }
            }
            return(msg);
        }