public async Task <IDataResult <ArticleDTO> > UpdateAsync(ArticleUpdateDTO articleUpdateDTO, string modifiedByName)
        {
            var article = _mapper.Map <Article>(articleUpdateDTO);

            article.ModifiedByName = modifiedByName;

            var updatedArticle = await _unitOfWork.Articles.UpdateAsync(article);

            await _unitOfWork.SaveAsync();

            return(new DataResult <ArticleDTO>(ResultStatus.Success, Messages.Article.Update(updatedArticle.Title), new ArticleDTO
            {
                Article = updatedArticle,
                ResultStatus = ResultStatus.Success,
                Message = Messages.Article.Update(updatedArticle.Title)
            }));
        }
Exemplo n.º 2
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)));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> PutArticle(int id, ArticleUpdateDTO articleDTO)
        {
            try
            {
                if (id != articleDTO.Id)
                {
                    _logger.LogError($"Id {id} and article id {articleDTO.Id} mismatch ");

                    return(BadRequest());
                }

                var article = _mapper.Map <Article>(articleDTO);

                var role   = User.FindFirst(ClaimTypes.Role).Value;
                var userId = User.FindFirst("Id").Value;

                var model = await _context.Articles.Where(x => x.Active == true && x.Id == id& x.PublishDone == false).FirstOrDefaultAsync();

                if (model == null)
                {
                    _logger.LogError($"Article is not available/ is published ");

                    return(BadRequest());
                }

                await _service.Update(id, article, role, userId);

                return(Ok(article));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Articlde {id} update failed");
                return(BadRequest());
            }

            return(NoContent());
        }
Exemplo n.º 4
0
 public async Task <OpResponse> Put([FromBody] ArticleUpdateDTO dto)
 {
     return(await _articleService.ModifyArticle(dto));
 }