Exemplo n.º 1
0
        public async Task <IActionResult> Update(int id, [FromBody] ArticleUpdateDTO articleUpdateDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo(LogMessages.AttemptedToUpdate(location, id));
                if (id < 1 || articleUpdateDTO == null || id != articleUpdateDTO.Id)
                {
                    _logger.LogWarn(LogMessages.BadData(location, id));
                    return(BadRequest());
                }
                var isExists = await _articleRepository.IsExists(id);

                if (!isExists)
                {
                    _logger.LogWarn(LogMessages.NotFound(location, id));
                    return(NotFound());
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn(LogMessages.IncompleteData(location, id));
                    return(BadRequest());
                }
                var article   = _mapper.Map <Article>(articleUpdateDTO);
                var isSuccess = await _articleRepository.Update(article);

                if (!isSuccess)
                {
                    return(InternalError(LogMessages.UpdateFailed(location, id)));
                }
                _logger.LogInfo(LogMessages.Success(location, id));
                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError(LogMessages.InternalError(location, e.Message, e.InnerException)));
            }
        }