Ejemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] ArticleAddOrUpdateViewModel articleAddResource)
        {
            if (articleAddResource == null)
            {
                return(BadRequest());
            }
            //使用FluentValidation验证属性
            if (!ModelState.IsValid)
            {
                return(new ValidationErrorResult(ModelState));
            }

            var article = Mapper.Map <ArticleAddOrUpdateViewModel, Article>(articleAddResource);

            article.Auther = "admin";
            article.Date   = DateTime.Now;


            Repository.PostArticle(article);
            if (!await UnitForWork.SaveAsync())
            {
                throw new Exception("保存失败!");
            }

            var resultViewModel   = Mapper.Map <Article, ArticleViewModel>(article).ToDynamic();
            var resultViewModelId = resultViewModel.Where(s => s.Key == "Id").FirstOrDefault().Value;

            resultViewModel.TryAdd("links", CreateHateoasLinks((int)resultViewModelId));

            return(CreatedAtRoute("GetSingleArticle", new { id = resultViewModelId }, resultViewModel));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Put(int id, [FromBody] ArticleAddOrUpdateViewModel article)
        {
            if (article == null)
            {
                return(BadRequest());
            }
            //使用FluentValidation验证属性
            if (!ModelState.IsValid)
            {
                return(new ValidationErrorResult(ModelState));
            }

            var result = await Repository.FindArticleByIdAsync(id);

            if (result == null)
            {
                return(BadRequest("找不到该文章"));
            }

            Mapper.Map(article, result);
            result.LastModify = DateTime.Now;

            if (!await UnitForWork.SaveAsync())
            {
                return(BadRequest("Save Error!"));
            }

            return(NoContent());
        }
Ejemplo n.º 3
0
 public BLogic()
 {
     this.configuration = new MapperConfiguration(cfg =>
     {
         cfg.CreateMap <Product, ProductBL>();
         cfg.CreateMap <Buyer, BuyerBL>();
         cfg.CreateMap <Order, OrderBL>();
     });
     configuration.AssertConfigurationIsValid();
     Mapper = new Mapper(configuration);
     db     = new UnitForWork();
 }
Ejemplo n.º 4
0
        public async Task <IActionResult> Delete(int Id)
        {
            try
            {
                var article = await Repository.FindArticleByIdAsync(Id);

                if (article == null)
                {
                    return(NotFound());
                }

                Repository.DeleteArticle(article);
                await UnitForWork.SaveAsync();

                return(NoContent());
            }
            catch
            {
                return(BadRequest());
            }
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ArticleAddOrUpdateViewModel> jsonPatch)
        {
            //验证传入的属性是否存在
            if (jsonPatch == null)
            {
                return(BadRequest());
            }
            var article = await Repository.FindArticleByIdAsync(id);

            if (article == null)
            {
                return(NotFound());
            }

            //将jsonPatchDocument解析到一个ArticleAddOrUpdateViewModel,并验证其正确性
            var articleToPatch = Mapper.Map <ArticleAddOrUpdateViewModel>(article);

            jsonPatch.ApplyTo(articleToPatch, ModelState);
            TryValidateModel(articleToPatch);
            if (!ModelState.IsValid)
            {
                return(new ValidationErrorResult(ModelState));
            }

            //将修改后的Article映射回原对象,并修改修改时间
            Mapper.Map(articleToPatch, article);
            article.LastModify = DateTime.Now;


            if (!await UnitForWork.SaveAsync())
            {
                return(BadRequest());
            }

            return(NoContent());
        }