Exemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                Info($"Author creation attempted");
                if (authorDTO == null)
                {
                    Warn($"Empty request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    Warn($"Invalid modelstate '{ModelState}'");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError($"Author {author} creation failed."));
                }
                Info($"Author created");
                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError(e));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Attempted Call");
                if (authorDTO == null)
                { // This endpoint would not even be hit if no body is provided (Dafault Code: 415 - Unsupported Media Type), nevertheles..
                    _logger.LogWarn($"{location}: Empty Request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid) // tracks validation status of data
                {                        // This endpoint would not even be hit if any "Required" field is not provided or is provided an empty value (Dafault Code: 400), nevertheles..
                    _logger.LogWarn($"{location}: Data was incomplete");
                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError($"{location}: Failure"));
                }

                _logger.LogInfo($"{location}: Successful");
                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} {e.InnerException}"));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _loggerService.LogInfo($"{location}: Create Attempted.");
                if (authorDTO == null)
                {
                    _loggerService.LogWarn($"{location}: Empty request was submitted.");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _loggerService.LogWarn($"{location}: Author data was incomplete.");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError($"{location}: Author creation failed"));
                }
                _loggerService.LogInfo($"{location}: Creation was successful.");
                _loggerService.LogInfo($"{location}: {author}");
                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            string location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Attempting to create author");
                if (authorDTO == null)
                {
                    _logger.LogWarn($"{location}: Author must have required fields and not empty");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    InternalError($"{location}: Data was not complete");
                    return(BadRequest(ModelState));
                }

                var author       = _mapper.Map <Author>(authorDTO);
                var IsSuccessful = await _authorRepository.Create(author);

                if (!IsSuccessful)
                {
                    return(InternalError($"{location}: Error creating author"));
                }

                _logger.LogInfo($"{location}: Author created successfully");
                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            var errLocation = GetControllerAndActionNames();

            try
            {
                if (authorDTO == null)
                {
                    logger.LogWarn($"Empty request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    logger.LogWarn($"Author data was incomplete");
                    return(BadRequest(ModelState));
                }
                var author    = mapper.Map <Author>(authorDTO);
                var isSuccess = await authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(ErrorHandler("Author creation failed"));
                }
                return(Created("create", new { author }));
            }
            catch (Exception ex)
            {
                return(ErrorHandler($"{errLocation} {ex.Message} - {ex.InnerException}"));
            }
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                if (authorDTO == null)
                {
                    return(BadRequest(ModelState));
                }

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

                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError($"Author creation failed"));
                }

                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError($"{e.Message} - {e.InnerException}"));
            }
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                _logger.LogInfo("Attempted to submit Author ");

                if (authorDTO == null)
                {
                    _logger.LogWarn("Author Create is NULL!");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn("Author Create model state is incomplete!");
                    return(BadRequest(ModelState));
                }

                var author = _mapper.Map <Author>(authorDTO);

                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    _logger.LogWarn("Author Create failed!");
                    return(InternalError("Author Create failed!"));
                }

                _logger.LogInfo("Successfully created author " + authorDTO.Firstname + " " + authorDTO.Lastname);
                return(Created("Create", new { author }));
            }
            catch (Exception ex)
            {
                return(InternalError("Controller: " + GetControllerActionNames() + " Error: " + ex.ToString()));
            }
        }
Exemplo n.º 8
0
        public async Task <IActionResult> CreateAuthor([FromBody] AuthorCreateDTO authorDTO)
        {
            var location = GetControllerActionName();

            try
            {
                if (authorDTO == null)
                {
                    _logger.LogWarn($"Create Author - Bad Request");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"Create Author - Data InValid");
                    return(BadRequest(ModelState));
                }

                _logger.LogInfo("Create Author");
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    _logger.LogError("Create Author - Failed");
                    return(InternalError("Create Author - Failed"));
                }
                _logger.LogInfo("Create Author - Success");
                return(Created("Create Author - Success", new { author }));
            }
            catch (Exception e)
            {
                _logger.LogError($"Create Author - Failed");
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
Exemplo n.º 9
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorCreateDTO)
        {
            try
            {
                _logger.LogInfo($"End user attempted to create a new Author.");
                if (authorCreateDTO == null)
                {
                    _logger.LogWarn($"End user sent an empty HTTP 1.1 POST request!");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"End user sent an HTTP 1.1 POST request to create a new Author with invalid Author data!");
                    return(BadRequest(ModelState));
                }
                var newAuthor = _mapper.Map <Author>(authorCreateDTO);
                var isSuccess = await _authorRepository.Create(newAuthor);

                if (!isSuccess)
                {
                    return(InternalError($"Failed creating new Author!"));
                }
                _logger.LogInfo("Successfully created a new Author!");
                return(Created("Create", new { newAuthor }));
            }
            catch (Exception exception)
            {
                return(InternalError($"{exception.Message} - {exception.InnerException}"));
            }
        }
Exemplo n.º 10
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                if (authorDTO == null)
                {
                    _loggerSerivce.LogInfor("Empty request was submitted.");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _loggerSerivce.LogInfor("Author's data was not completed.");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError($"Authors failed to created."));
                }
                _loggerSerivce.LogInfor("Author is created!");
                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError($"{e.Message} - {e.InnerException}"));
            }
        }
Exemplo n.º 11
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                this.logger.LogInfo("Author submission attempted");

                if (authorDTO == null)
                {
                    this.logger.LogWarn("Empty request was submitted");
                    return(this.BadRequest(this.ModelState));
                }

                if (!this.ModelState.IsValid)
                {
                    this.logger.LogWarn("Author data was incomplete");
                    return(this.BadRequest(this.ModelState));
                }

                var  author    = this.mapper.Map <Author>(authorDTO);
                bool isSuccess = await this.authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(this.InternalError("Author creation failed"));
                }

                this.logger.LogInfo($"Author created successfully:{author.Firstname}, {author.Lastname}");

                return(this.Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(this.InternalError($"{e.Message} - {e.InnerException}"));
            }
        }
Exemplo n.º 12
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                if (authorDTO == null)
                {
                    _logger.LogWarn($"Create() empty");
                    return(BadRequest(ModelState));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"Create() invalid");
                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    _logger.LogWarn($"Create() failed");
                    return(StatusCode(500));
                }

                _logger.LogInfo($"Create() created");
                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                _logger.LogError($"Create() {e.Message} - {e.InnerException}");
                return(StatusCode(500));
            }
        }
Exemplo n.º 13
0
        public async Task <IActionResult> CreateAuthor([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                _logger.LogInfo($"Attemted to create an Author");
                if (authorDTO == null)
                {
                    _logger.LogWarn("Empty request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn("Required data was not provided");
                    return(BadRequest(ModelState));
                }
                _logger.LogInfo("Attemted to create Author");
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError("Author creation failed"));
                }
                _logger.LogInfo($"Successfully create an Author");
                return(Created("author created", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError($"{e.Message} - {e.InnerException}"));
            }
        }
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO AuthorDTO)
        {
            var getLocation = GetControllerActionNames();

            try
            {
                _logger.logInfo($"{getLocation} Author submission attempted");
                if (AuthorDTO == null)
                {
                    _logger.logWarning($"Empty Request Submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.logWarning($"Author Data was Incomplete");
                    return(BadRequest(ModelState));
                }
                var author       = _mapper.Map <Author>(AuthorDTO);
                var isSuccessful = await _authorRepository.Create(author);

                if (!isSuccessful)
                {
                    return(Internalserver($"Author creation Failed"));
                }

                _logger.logInfo($"{getLocation} Author Created Successfully");
                return(Created("Created", new { author }));
            }
            catch (Exception e)
            {
                return(Internalserver($"{e.Message} - {e.InnerException}"));
            }
        }
Exemplo n.º 15
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                if (authorDTO == null)
                {
                    _logger.LogWarn($"Empty Request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"Bad Data Dude");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    _logger.LogWarn($"Bad Data Dude");
                    return(InternalError($"Did not write to Flys Book store "));
                }

                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError($"{e.Message } - {e.StackTrace }"));
            }
        }
Exemplo n.º 16
0
        public async Task <ActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                loggerService.LogInfo("Author submission submitted");
                if (authorDTO == null)
                {
                    loggerService.LogWarning("empty request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    loggerService.LogWarning("Author data was Incomplete");
                    return(BadRequest(ModelState));
                }
                var author    = mapper.Map <Author>(authorDTO);
                var isSuccess = await authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError("Author creation Failed"));
                }
                loggerService.LogInfo("Author created successfully");
                return(Created("Created", new { author }));
            }
            catch (Exception ex)
            {
                return(InternalError($"{ex.Message} - {ex.InnerException}"));
            }
        }
Exemplo n.º 17
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorCreateDTO)
        {
            try
            {
                // ModelState restaura as informações preenchidas para o form
                if (authorCreateDTO == null)
                {
                    _logger.LogWarn($"Empty request.");
                    return(BadRequest(ModelState));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"Verifique os dados e tente novamente.");
                    return(BadRequest(ModelState));
                }

                var author = _mapper.Map <Author>(authorCreateDTO);

                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError($"Failed to create Author."));
                }

                _logger.LogInfo("Author created.");
                return(Created("Create", new { author }));
            }
            catch (Exception ex)
            {
                return(InternalError($"{ex.Message} - {ex.InnerException}"));
            }
        }
Exemplo n.º 18
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                _logger.LogInfo("Author Creation Method is called");
                if (authorDTO == null)
                {
                    _logger.LogWarn("Empty Request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                Author author    = _mapper.Map <Author>(authorDTO);
                var    isSuccess = await authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError("Author Creation is failed"));
                }
                return(Created("Create", new { author }));
            }
            catch (Exception ex)
            {
                return(InternalError($"{ex.Message}- {ex.InnerException}"));
            }
        }
Exemplo n.º 19
0
        public async Task <ActionResult <AuthorReadDTO> > CreateAuthor(AuthorCreateDTO authorCreateDTO)
        {
            try
            {
                var author = _mapper.Map <Author>(authorCreateDTO);

                var existAuthor = await _db.Authors.CheckIfAuthorExists(author);

                if (existAuthor != null)
                {
                    _logger.LogError("POST api/authors - Bad Request - Author already exists");
                    return(BadRequest("Error - Author already exists"));
                }

                await _db.Authors.Create(author);

                await _db.Save();

                var authorDTO = _mapper.Map <AuthorReadDTO>(author);
                _logger.LogInformation($"POST api/authors - Author added to database");

                return(CreatedAtAction(nameof(GetAuthorById), new { id = authorDTO.ID }, authorDTO));
            }
            catch (Exception ex)
            {
                _logger.LogError("GET api/authors - Problem with Database");
                return(StatusCode(500, "Internal Server Error. Cannot connect wiht Database!"));
            }
        }
Exemplo n.º 20
0
        public async Task <IActionResult> createAuthor([FromBody] AuthorCreateDTO authorDTO)
        {
            var location = getControllerDetails();

            try

            {
                _logger.LogInfo($"{location}  call Started");
                if (authorDTO == null)
                {
                    _logger.LogWarn($"{location}  Empty request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"{location}  Incomplete request was submitted");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(internalError($"{location}  Error while saving data"));
                }
                _logger.LogInfo($"{location}  created succesfully");
                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(internalError($"{location}  - {e.Message} - {e.InnerException}"));
            }
        }
Exemplo n.º 21
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            _logger.LogInfo($"Author Submission Attempted");
            try
            {
                if (authorDTO == null)
                {
                    _logger.LogWarn($"Empty Author  request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"Author Data in wrong format");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError($"Author creation failed"));
                }
                _logger.LogInfo("Author created successfully");
                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError($"{e.Message} - {e.InnerException}"));
            }
        }
Exemplo n.º 22
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                if (authorDTO == null)
                {
                    _logger.LogWarn("Empty Author was submitted.");
                    return(BadRequest(ModelState));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogWarn("Author data was incomplete.");
                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError("Author creation failed."));
                }

                return(Created("Create", new { author }));
            }
            catch (Exception ex)
            {
                return(InternalError(ex));
            }
        }
Exemplo n.º 23
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                _logger.LogInfo($"AuthorsController Intentando crear autor");
                if (authorDTO == null)
                {
                    _logger.LogWarn($"AuthorsController La petición de crear un autor se hizo sin datos");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"AuthorsController La petición de crear un autor se hizo con datos incompletos");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    _logger.LogError($"AuthorsController La creacion de autor nuevo falló");
                    return(StatusCode(500, "AuthorsController La creacion de autor nuevo falló"));
                }
                _logger.LogInfo($"AuthorsController Se creó nuevo autor correctamente");
                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError(e));
            }
        }
Exemplo n.º 24
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            _logger.LogInfo($"Author Submittion Attempted");
            try {
                if (authorDTO == null)
                {
                    _logger.LogWarn($"Empty Request was submitted");
                    // ModelState is DTOs required fields
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"Author Data was Incomplete");
                    return(BadRequest(ModelState));
                }

                // Map all the data from the authorDTO and map into the Author data set
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                // Did something outside user's control fail?
                if (!isSuccess)
                {
                    return(InternalError($"Author creation failed"));
                }
                _logger.LogInfo($"Author Created");
                return(Created("Create", new { author }));
            } // try

            catch (Exception ex) {
                return(InternalError($"{ex.Message} - {ex.InnerException}"));
            } // catch
        }     // Create (author)
Exemplo n.º 25
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Author submission attempted");
                if (authorDTO == null)
                {
                    _logger.LogWarn($"{location}: Empty request was submitted");
                    return(BadRequest(ModelState));
                    // TODO check for uniqueness
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"{location}: Submitted data not valid");
                    return(BadRequest(ModelState));
                }
                var author   = _mapper.Map <Author>(authorDTO);
                var isSucess = await _authorRepository.Create(author);

                if (!isSucess)
                {
                    return(InternalError("Author creation failed"));
                }
                _logger.LogInfo($"{location}: Author Created");
                return(Created("Author created ", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError($"{ e.Message} - {e.InnerException }"));
            }
        }
Exemplo n.º 26
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                _logger.LogInfo("Create started");
                if (authorDTO == null)
                {
                    _logger.LogWarn("Empty request");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn("Data incomplete");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError("Author creation failed}"));
                }
                _logger.LogInfo("Author created");
                return(Created("Create", new { author }));
            }
            catch (Exception ex)
            {
                return(InternalError($"Something went wrong: {ex.Message}"));
            }
        }
Exemplo n.º 27
0
        public async Task <ActionResult> CreateAuthorAsync([FromBody] AuthorCreateDTO authorCreate, CancellationToken ct = default)
        {
            //It is not necesary after NET Core 2.1, since the ApiController manages it.
            TryValidateModel(authorCreate);

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

            if (authorCreate == null)
            {
                return(BadRequest());
            }

            var author = _autoMapper.Map <Author>(authorCreate);

            await _dbContext.Authors.AddAsync(author);

            await _dbContext.SaveChangesAsync();

            _logger?.LogInformation("New author was created");

            //Returns a 202 (Created), the route name and the response values.
            var authorDTO = _autoMapper.Map <AuthorDTO>(author);

            return(new CreatedAtRouteResult(nameof(GetAuthorByIdAsync), new { id = author.Id }, authorDTO));
        }
Exemplo n.º 28
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                if (authorDTO == null)
                {
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(StatusCode(500));
                }

                return(Created("Create", new { author }));
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Exemplo n.º 29
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            try
            {
                _loggerService.LogInfo($"Authors submission attempted");
                if (authorDTO == null)
                {
                    _loggerService.LogWarn($"Empty request was submited");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _loggerService.LogWarn($"Author Data was incomplete");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSucsses = await _authorRepository.Create(author);

                if (!isSucsses)
                {
                    return(InternalError($"Author creation failed"));
                }
                _loggerService.LogInfo("Author created");
                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError($"{e.Message} - {e.InnerException}"));
            }
        }
Exemplo n.º 30
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateDTO authorDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Request was submitted");
                if (authorDTO == null)
                {
                    // a null request does not make it to this method
                    _logger.LogWarn($"{location}: Empty request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    // Firstname Lastname required validation tests are not caught here - processed sooner
                    _logger.LogWarn($"{location}: Data was incomplete");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Create(author);

                if (!isSuccess)
                {
                    return(InternalError($"{location}: Create failed"));
                }
                _logger.LogInfo($"{location}: Successful");
                return(Created("Create", new { author }));
            }
            catch (Exception e)
            {
                return(InternalError(e));
            }
        }