public async Task <IActionResult> Put(AddIdNameModel model) { var entity = await PostTypeSvc.GetByNameAsync(model.Name); if (entity != null) { return(new JsonResult( new APIResult <long>() { ErrorMsg = "该帖子类型已存在" } ) { StatusCode = 400 }); } AddIdNameDTO dto = new AddIdNameDTO(); dto.Description = model.Description; dto.Name = model.Name; return(new JsonResult( new APIResult <long>() { Data = await PostTypeSvc.AddNewAsync(dto) } )); }
public async Task <long> AddNewAsync(AddIdNameDTO dto) { PostTypeEntity entity = new PostTypeEntity(); entity.Description = dto.Description; entity.Name = dto.Name; using (PostContext ctx = new PostContext()) { BaseService <PostTypeEntity> bs = new BaseService <PostTypeEntity>(ctx); var status = await bs.GetAll().SingleOrDefaultAsync(e => e.Name == dto.Name); if (status != null) { throw new Exception("帖子类型已存在"); } await ctx.PostTypes.AddAsync(entity); await ctx.SaveChangesAsync(); return(entity.Id); } }