环境管理开发实战

环境管理开发实战

前言

        在上一集我们完成了项目管理开发实战,那么我们可以看到下面的E-R图和标题,我们可以知道,今天我们要完成环境管理开发实战!

可以看到一个项目当中是可以存在n个环境的,所以今天的环境管理开发实战,有的参数会和项目的字段有关系哦!

EnvironmentController

通过上一集,我们同样可以想到环境管理开发也是只需要那些API即可,所以话不多说,马上开始写代码,还是采用RESTful编程风格!

list

@GetMapping("/list")
    public JsonData list(@RequestParam("projectId")Long projectId) {
        return JsonData.buildSuccess(environmentService.list(projectId));
    }
save
 @PostMapping("/save")
    public JsonData save(@RequestBody EnvironmentSaveReq req) {
        return JsonData.buildSuccess(environmentService.save(req));
    }
update
 @PostMapping("/update")
    public JsonData update(@RequestBody EnvironmentUpdateReq req) {
        return JsonData.buildSuccess(environmentService.update(req));
    }

del

@PostMapping("/del")
    public JsonData delete(@RequestBody EnvironmentDelReq req) {
        return JsonData.buildSuccess(environmentService.delete(req.getId(),req.getProjectId()));
    }

可以看到,第一个api需要传入一个名为projectId的这个参数,这是因为,一个项目是可以有多个环境的,这也符合我们在E-R图当中项目和环境一对多的关系。

完整代码


@RestController
@RequestMapping("/api/v1/env")
public class EnvironmentController {
    @Resource
    private EnvironmentService environmentService;

    @GetMapping("/list")
    public JsonData list(@RequestParam("projectId")Long projectId) {
        return JsonData.buildSuccess(environmentService.list(projectId));
    }

    @PostMapping("/save")
    public JsonData save(@RequestBody EnvironmentSaveReq req) {
        return JsonData.buildSuccess(environmentService.save(req));
    }

    @PostMapping("/update")
    public JsonData update(@RequestBody EnvironmentUpdateReq req) {
        return JsonData.buildSuccess(environmentService.update(req));
    }

    @PostMapping("/del")
    public JsonData delete(@RequestBody EnvironmentDelReq req) {
        return JsonData.buildSuccess(environmentService.delete(req.getId(),req.getProjectId()));
    }

}

不用多说,完整代码中的自定义请求体类在下面。

EnvironmentSaveReq
@Data
public class EnvironmentSaveReq {
    private Long projectId;

    private String name;

    private String protocol;

    private String domain;

    private Integer port;

    private String description;

}
EnvironmentUpdateReq
@Data
public class EnvironmentUpdateReq {
    private Long id;

    private Long projectId;

    private String name;

    private String protocol;

    private String domain;

    private Integer port;

    private String description;

}
EnvironmentDelReq
@Data
public class EnvironmentDelReq {
    private Long id;

    private Long projectId;
}

可以看到这就是所有的自定义请求体类了。

接下来仅需把Service和它的Impl即可。

EnvironmentService

public interface EnvironmentService {

    List<EnvironmentDTO> list(Long projectId);

    int save(EnvironmentSaveReq req);

    int update(EnvironmentUpdateReq req);

    int delete(Long id, Long projectId);
}

EnvironmentServiceImpl

list
public List<EnvironmentDTO> list(Long projectId) {

        LambdaQueryWrapper<EnvironmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EnvironmentDO::getProjectId,projectId);

        List<EnvironmentDO> environmentDOS = environmentMapper.selectList(queryWrapper);

        return SpringBeanUtil.copyProperties(environmentDOS,EnvironmentDTO.class);
    }
save
public int save(EnvironmentSaveReq req) {
        ProjectDO projectDO = projectMapper.selectById(req.getProjectId());
        if(projectDO != null){
            EnvironmentDO environmentDO = SpringBeanUtil.copyProperties(req, EnvironmentDO.class);
            return environmentMapper.insert(environmentDO);
        }
        return 0;
    }
update
public int update(EnvironmentUpdateReq req) {
        ProjectDO projectDO = projectMapper.selectById(req.getProjectId());
        if(projectDO != null){
            EnvironmentDO environmentDO = SpringBeanUtil.copyProperties(req, EnvironmentDO.class);
            return environmentMapper.updateById(environmentDO);
        }
        return 0;
    }
delete
 public int delete(Long id, Long projectId) {

        LambdaQueryWrapper<EnvironmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EnvironmentDO::getId,id);
        queryWrapper.eq(EnvironmentDO::getProjectId,projectId);

        return environmentMapper.delete(queryWrapper);
    }
完整代码
@Service
@Slf4j
public class EnvironmentServiceImpl implements EnvironmentService {

    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private EnvironmentMapper environmentMapper;
    @Override
    public List<EnvironmentDTO> list(Long projectId) {

        LambdaQueryWrapper<EnvironmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EnvironmentDO::getProjectId,projectId);

        List<EnvironmentDO> environmentDOS = environmentMapper.selectList(queryWrapper);

        return SpringBeanUtil.copyProperties(environmentDOS,EnvironmentDTO.class);
    }

    @Override
    public int save(EnvironmentSaveReq req) {
        ProjectDO projectDO = projectMapper.selectById(req.getProjectId());
        if(projectDO != null){
            EnvironmentDO environmentDO = SpringBeanUtil.copyProperties(req, EnvironmentDO.class);
            return environmentMapper.insert(environmentDO);
        }
        return 0;
    }

    @Override
    public int update(EnvironmentUpdateReq req) {
        ProjectDO projectDO = projectMapper.selectById(req.getProjectId());
        if(projectDO != null){
            EnvironmentDO environmentDO = SpringBeanUtil.copyProperties(req, EnvironmentDO.class);
            return environmentMapper.updateById(environmentDO);
        }
        return 0;
    }

    @Override
    public int delete(Long id, Long projectId) {

        LambdaQueryWrapper<EnvironmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EnvironmentDO::getId,id);
        queryWrapper.eq(EnvironmentDO::getProjectId,projectId);

        return environmentMapper.delete(queryWrapper);
    }
}

看上去还是十分的简单的,没有很复杂的逻辑代码,也就用了一下Mybatis-Plus的LambdaQueryWrapper的类而已,其实也就是构建了SQL查询条件。

当然在最后还是会给出相关的DTO。

EnvironmentDTO 
@Data
public class EnvironmentDTO {
    private Long id;

    private Long projectId;

    private String name;

    private String protocol;

    private String domain;

    private Integer port;

    private String description;

    private Date gmtCreate;

    private Date gmtModified;
}

上一篇:games103作业2(未完)


下一篇:英特尔服务器的潜在缺陷分析