Exemplo n.º 1
0
        public async Task <ResponseModel> UpdateArticleAsync(Guid id, ArticleManageModel articleManageModel)
        {
            var article = await _articleRepository.GetByIdAsync(id);

            if (article == null)
            {
                return(new ResponseModel
                {
                    StatusCode = System.Net.HttpStatusCode.NotFound,
                    Message = "This Article is not exist!"
                });
            }
            else
            {
                var existedArticle = await _articleRepository.FetchFirstAsync(x => x.Title == articleManageModel.Title && x.Id != id);

                if (existedArticle != null)
                {
                    return(new ResponseModel
                    {
                        StatusCode = System.Net.HttpStatusCode.BadRequest,
                        Message = "This Article's title is exist!"
                    });
                }
                else
                {
                    articleManageModel.GetArticleFromModel(article);
                    return(await _articleRepository.UpdateAsync(article));
                }
            }
        }
Exemplo n.º 2
0
        public async Task <ResponseModel> CreateArticleAsync(ArticleManageModel articleManageModel)
        {
            var article = await _articleRepository.FetchFirstAsync(x => x.Title == articleManageModel.Title);

            if (article != null)
            {
                return(new ResponseModel()
                {
                    StatusCode = System.Net.HttpStatusCode.BadRequest,
                    Message = "This Article title is exist!",
                });
            }
            else
            {
                article = _mapper.Map <Article>(articleManageModel);

                await _articleRepository.InsertAsync(article);

                article = await GetAll().FirstOrDefaultAsync(x => x.Id == article.Id);

                return(new ResponseModel()
                {
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Data = new ArticleViewModel(article)
                });
            }
        }
        public async Task <Result <ArticleManageModel> > UpdateArticle([FromBody] ArticleManageModel model)
        {
            if (!Validate(model))
            {
                return(null);
            }
            var item    = model.Convert();
            var sUserId = _userManager.GetUserId(User);
            int userId;

            if (Int32.TryParse(sUserId, out userId))
            {
                item.UserId = userId;
            }

            item = await articleService.UpdateArticleAsync(item);

            await articleService.AttachArticleToCategoriesAsync(item.Id, model.CategoryIds);

            return(new ArticleManageModel(item));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Update(Guid id, [FromBody] ArticleManageModel articleManageModel)
        {
            var response = await _articleService.UpdateArticleAsync(id, articleManageModel);

            return(new CustomActionResult(response));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Create([FromBody] ArticleManageModel articleManageModel)
        {
            var response = await _articleService.CreateArticleAsync(articleManageModel);

            return(new CustomActionResult(response));
        }