public async Task <Guid> CreateAsync(DepartmentCDto dto) { var parient = await _dbContent.Departments.FindAsync(dto.Pid); if (parient == null) { _logger.LogError($"创建部门出错:上级部门 { dto.Pid} 不存在 "); throw new UserOperationException("上级部门不存在"); } var entity = _mapper.Map <DepartmentEntity>(dto); entity.FullPath = parient.FullPath + "/" + entity.Name; try { await _dbContent.AddAsync(entity); await _dbContent.SaveChangesAsync(); return(entity.Id); } catch (Exception ex) { _logger.LogError(ex, "创建部门失败"); throw new UserOperationException("已存在相同名称的部门"); } }
public async Task <IActionResult> CreateAsync([FromBody] DepartmentCDto dto) { return(Ok(await _departmentService.CreateAsync(dto))); }