前言
本项目是为开发一套容器化的开发、运行、测试环境,用以支持Web开发、程序设计等课程的实验教学。
任务目标
实现通知的新建,查询,更新功能。
我的任务
Mapper层编写sql,操作数据库,建立通知的Service和controller层,编写通知的新建,查询,更新接口。
工作内容
建立Lab表,设计对应操作API
代码
实体类
Lab.java
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Lab {
private Integer id;
private Integer organizationId;
private Integer founderId;
private String name;
private String description;
private String beginTime;
private String endTime;
private Integer status;
private Date createTime;
private Date lastModifyTime;
}
LabMapper.xml
<mapper namespace="cn.sdu.weblab.mapper.LabMapper">
<resultMap id="baseLabMap" type="cn.sdu.weblab.entity.Lab">
<id column="id" property="id"></id>
<result column="organization_id" property="organizationId"></result>
<result column="founder_id" property="founderId"></result>
<result column="name" property="name"></result>
<result column="description" property="description"></result>
<result column="begin_time" property="beginTime"></result>
<result column="end_time" property="endTime"></result>
</resultMap>
<resultMap id="baseProjectMap" type="cn.sdu.weblab.entity.pojo.Project">
<id column="id" property="id"></id>
<result column="weblab_user_id" property="weblabUserId"></result>
<result column="gitlab_user_id" property="gitlabUserId"></result>
<result column="name" property="name"></result>
<result column="description" property="description"></result>
<result column="url" property="url"></result>
</resultMap>
<resultMap id="baseProjectInfoMap" type="cn.sdu.weblab.entity.pojo.ProjectInfo">
<id column="id" property="id"></id>
<result column="weblab_user_id" property="weblabUserId"></result>
<result column="gitlab_user_id" property="gitlabUserId"></result>
<result column="name" property="name"></result>
<result column="description" property="description"></result>
<result column="url" property="url"></result>
<result column="real_name" property="realName"></result>
<result column="grade" property="grade"></result>
<result column="banji" property="banji"></result>
<result column="student_id" property="studentId"></result>
<result column="head_img" property="headImg"></result>
<result column="signature" property="signature"></result>
</resultMap>
<insert id="addLab" >
insert into lab(id, organization_id, founder_id, name, description, status, begin_time, end_time)
values (#{id}, #{organizationId}, #{founderId}, #{name}, #{description}, #{status}, #{beginTime}, #{endTime})
</insert>
<update id="setLab" >
update lab set id = #{id}, organization_id = #{organizationId}, founder_id = #{founderId}, name = #{name}, description = #{description}, begin_time = #{beginTime}, end_time = #{endTime}
where id=#{id} and status=0
</update>
<select id="getLabById" resultMap="baseLabMap">
select * from lab
where id=#{id} and status=0
</select>
<update id="deleteLab">
update lab set status=1
where id=#{id}
</update>
<select id="getOrgLabList" resultMap="baseLabMap">
select * from lab
where organization_id=#{orgId} and status=0
</select>
<select id="getUserProjectList" resultMap="baseProjectMap">
select * from project a, lab_project b
where a.weblab_user_id=#{userId} and b.project_id=a.id and b.lab_id=#{labId} and status=0
</select>
<select id="getProjectList" resultMap="baseProjectInfoMap">
select p1.id ,p1.weblab_user_id ,p1.gitlab_user_id ,p1.name ,p1.description ,p1.url, u1.real_name, u1.grade, u1.banji, u1.student_id, u1.head_img, u1.signature
from lab_project l1,project p1,userInfo u1
where p1.weblab_user_id=u1.user_id and l1.project_id=p1.id and l1.lab_id=#{labId} and p1.status=0 and u1.status=0
</select>
<insert id="addProjectIntoLab">
insert into lab_project(lab_id, project_id)
values (#{labId}, #{projectId})
</insert>
<delete id="deleteProjectFromLabByLabId">
delete from lab_project
where lab_id=#{labId}
</delete>
<delete id="deleteProjectFromLabByProjectId">
delete from lab_project
where project_id=#{projectId} and lab_id=#{labId}
</delete>
</mapper>
LabServiceImpl.java
@Service
public class LabServiceImpl implements LabService {
@Autowired
LabMapper labMapper;
@Override
public void createLab(Lab lab) {
labMapper.addLab(lab);
}
@Override
public Lab getLabById(int id) {
return labMapper.getLabById(id);
}
@Override
public void removeLab(int id) {
labMapper.deleteLab(id);
labMapper.deleteProjectFromLabByLabId(id);
}
@Override
public void updateLab(Lab lab) {
labMapper.setLab(lab);
}
@Override
public List<ProjectInfo> getProjectList(int labId) {
List<ProjectInfo> list = labMapper.getProjectList(labId);
return list;
}
@Override
public List<Lab> getOrgLabList(int orgId) {
List<Lab> list = labMapper.getOrgLabList(orgId);
return list;
}
@Override
public List<Project> getUserProjectList(int userId, int labId) {
List<Project> list = labMapper.getUserProjectList(userId, labId);
return list;
}
@Override
public void addProjectIntoLab(int labId, int projectId) {
labMapper.addProjectIntoLab(labId, projectId);
}
}
LabController.java
@RestController
@RequestMapping("/lab")
@Slf4j
public class LabController {
@Autowired
private LabService labService;
@Autowired
private OrganizationService organizationService;
/**
* 新建作业
* @param organizationId
* @param name
* @param desc
* @param beginTime
* @param endTime
* @return
*/
@PostMapping(value = "/createLab")
public ResultEntity createLab(int organizationId, String name, String desc, String beginTime, String endTime) throws Exception {
if(organizationService.getOrgById(organizationId)==null)
return ResultEntity.error("Organization not found",null);
Integer founderId = UserUtil.getCurrentUser().getId();
Lab lab = new Lab(null, organizationId, founderId, name ,desc, beginTime, endTime, 0, null, null);
lab.setId(Integer.parseInt(UserUtil.getVerifyCode(6)));
labService.createLab(lab);
return ResultEntity.success(lab);
}
/**
* 更新作业
* @param id
* @param name
* @param desc
* @param beginTime
* @param endTime
* @return
*/
@PostMapping(value = "/updateLab")
public ResultEntity updateLab(int id, String name, String desc, String beginTime, String endTime) {
if(labService.getLabById(id)==null)
return ResultEntity.error("Lab not found",null);
Lab lab = labService.getLabById(id);
if(organizationService.getOrgById(lab.getOrganizationId())==null)
return ResultEntity.error("Organization not found",null);
lab.setName(name);
lab.setDescription(desc);
lab.setBeginTime(beginTime);
lab.setEndTime(endTime);
labService.updateLab(lab);
return ResultEntity.success(lab);
}
/**
* 通过ID查询作业
* @param id
* @return
*/
@PostMapping(value = "/getLabById")
public ResultEntity getLabById(int id) {
Lab lab = labService.getLabById(id);
if(lab==null)
return ResultEntity.error("Lab not found",null);
if(organizationService.getOrgById(lab.getOrganizationId())==null)
return ResultEntity.error("Organization not found",null);
return ResultEntity.success(lab);
}
/**
* 获取作业下所有项目
* @param labId
* @return
*/
@PostMapping(value = "/getProjectList")
public ResultEntity getProjectList(int labId) {
if(labService.getLabById(labId)==null)
return ResultEntity.error("Lab not found",null);
if(organizationService.getOrgById(labService.getLabById(labId).getOrganizationId())==null)
return ResultEntity.error("Organization not found",null);
List<ProjectInfo> list = labService.getProjectList(labId);
return ResultEntity.success(list);
}
/**
* 获取用户在指定作业下的项目
* @param userId
* @param labId
* @return
*/
@PostMapping(value = "/getUserProjectList")
public ResultEntity getUserProjectList(int userId, int labId) {
if(labService.getLabById(labId)==null)
return ResultEntity.error("Lab not found",null);
if(organizationService.getOrgById(labService.getLabById(labId).getOrganizationId())==null)
return ResultEntity.error("Organization not found",null);
List<Project> list = labService.getUserProjectList(userId,labId);
return ResultEntity.success(list);
}
/**
* 获取组织下所有作业
* @param orgId
* @return
*/
@PostMapping(value = "/getOrgLabList")
public ResultEntity getOrgLabList(int orgId) {
if(organizationService.getOrgById(orgId)==null)
return ResultEntity.error("Organization not found",null);
List<Lab> list = labService.getOrgLabList(orgId);
return ResultEntity.success(list);
}
/**
* 删除作业
* @param id
* @return
*/
@PostMapping(value = "/deleteLab")
public ResultEntity deleteLab(int id) {
if(labService.getLabById(id)==null)
return ResultEntity.error("Lab not found",null);
if(organizationService.getOrgById(labService.getLabById(id).getOrganizationId())==null)
return ResultEntity.error("Organization not found",null);
labService.removeLab(id);
return ResultEntity.success();
}
@PostMapping(value = "/addProjectIntoLab")
public ResultEntity addProjectIntoLab(int labId, int projectId) {
if(labService.getLabById(labId)==null)
return ResultEntity.error("Lab not found",null);
if(organizationService.getOrgById(labService.getLabById(labId).getOrganizationId())==null)
return ResultEntity.error("Organization not found",null);
labService.addProjectIntoLab(labId, projectId);
return ResultEntity.success();
}
}