예제 #1
0
        public async Task <IActionResult> Post(UpdateIdNameModel model)
        {
            var entity = await PostStatusSvc.GetByNameAsync(model.Name);

            if (entity != null)
            {
                if (entity.Id != model.Id)
                {
                    return(new JsonResult(new APIResult <long>()
                    {
                        ErrorMsg = "该类型名称已存在"
                    })
                    {
                        StatusCode = 400
                    });
                }
            }
            UpdateIdNameDTO dto = new UpdateIdNameDTO();

            dto.Id          = model.Id;
            dto.Name        = model.Name;
            dto.Description = model.Description;
            await PostStatusSvc.UpdateAsync(dto);

            return(Ok());
        }
        public async Task UpdateAsync(UpdateIdNameDTO dto)
        {
            using (PostContext ctx = new PostContext())
            {
                BaseService <PostTypeEntity> bs = new BaseService <PostTypeEntity>(ctx);
                var nameEntity = await bs.GetAll().SingleOrDefaultAsync(e => e.Name == dto.Name);

                if (nameEntity != null)
                {
                    if (nameEntity.Id != dto.Id)
                    {
                        throw new Exception("帖子类型已存在");
                    }
                }
                var en = await bs.GetAll().SingleAsync(e => e.Id == dto.Id);

                en.Description = dto.Description;
                en.Name        = dto.Name;
                await ctx.SaveChangesAsync();
            }
        }