前言
本项目是为开发一套容器化的开发、运行、测试环境,用以支持Web开发、程序设计等课程的实验教学。
任务目标
实现组织的新建,查询,更新功能。
我的任务
Mapper层编写sql,操作数据库,建立组织的Service和controller层,编写组织新建,查询,更新接口。
工作内容
建立Organization表,设计对应操作API
代码
实体类
Organization.java
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Organization implements Serializable {
private Integer id;
private String name;
private String code;
private String courseId;
private Integer founderId;
private String founderName;
private String description;
private Integer status;
private Date createTime;
private Date lastModifyTime;
private String beginTime;
private String endTime;
@Override
public String toString() {
return "Organization{" +
"id=" + id +
", name='" + name + '\'' +
", code=" + code + "\'" +
", founderid='" + founderId + '\'' +
", description='" + description + '\'' +
", status=" + status +
", createTime=" + createTime +
", lastModifyTime=" + lastModifyTime +
'}';
}
}
OrganizationMapper.xml
<insert id="addOrganization">
insert into organization(id,name,code,founder_id,description,status,course_id,founder_name,begin_time,end_time)
values (#{id}, #{name}, #{code}, #{founderId}, #{description}, #{status}, #{courseId}, #{founderName}, #{beginTime}, #{endTime})
</insert>
<update id="reNameById">
update organization set name=#{name} where id=#{id} and status=0
</update>
<update id="reDescriptionById">
update organization set description=#{desc} where id=#{id} and status=0
</update>
<update id="deleteOrgById">
update organization set status=1 where id=#{id}
</update>
<select id="getOrgById" resultMap="baseOrgMap">
select * from organization
where id=#{id} and status=0
</select>
<select id="getOrgByCode" resultMap="baseOrgMap">
select * from organization
where code=#{code} and status=0
</select>
OrganizationServiceImpl.java
@Service
public class OrganizationServiceImpl implements OrganizationService {
@Autowired
private OrganizationMapper organizationMapper;
@Override
public void addOrg(Organization org)
{
organizationMapper.addOrganization(org);
}
/**
* 通过id查询组织名称
* @param id
* @return
*/
@Override
public String getOrgNameById(int id) {
Organization org = organizationMapper.getOrgById(id);
//System.out.println("TTTIS --- --- " + id);
//System.out.println("OPPPP --- --- " + org.getName());
return org.getName();
}
/**
* 通过id查询组织信息
* @param id
* @return
*/
@Override
public Organization getOrgById(int id) {
Organization org = organizationMapper.getOrgById(id);
//System.out.println("TTTIS --- --- " + id);
//System.out.println("OPPPP --- --- " + org.getName());
return org;
}
/**
* 通过code查询组织信息
* @param code
* @return
*/
@Override
public Organization getOrgByCode(String code) {
Organization org = organizationMapper.getOrgByCode(code);
//System.out.println("TTTIS --- --- " + id);
//System.out.println("OPPPP --- --- " + org.getName());
return org;
}
@Override
public List<Organization> getOrgListByFounderId(int founderId) {
return organizationMapper.getOrgListByFounderId(founderId);
}
@Override
public List<Organization> getOrgListByUserId(int userId) {
return organizationMapper.getOrgListByUserId(userId);
}
/**
* 查询组织内学生列表
* @param id
* @return
*/
@Override
public UserList getStuList(int id) {
List<User> list = organizationMapper.getOrgStuList(id);
//for(User user : list) {
// System.out.println(user);
//}
UserList userList = new UserList(list);
return userList;
}
/**
* 加入组织
* @param orgId
* @param userId
*/
@Override
public void JoinOrg(Integer orgId, Integer userId) {
organizationMapper.addUserIntoOrgById(orgId, userId);
}
/**
* 退出组织
* @param orgId
* @param userId
*/
@Override
public void QuitOrg(Integer orgId, Integer userId) {
organizationMapper.deleteUserFromOrgById(orgId, userId);
}
/**
* 删除组织
* @param orgId
*/
@Override
public void removeOrg(int orgId) {
organizationMapper.deleteOrgById(orgId);
organizationMapper.deleteMessageFromOrgByOrgId(orgId);
organizationMapper.deleteLabFromOrgByOrgId(orgId);
}
/**
* 更新组织
* @param organization
*/
@Override
public void updateOrg(Organization organization) {
organizationMapper.setOrg(organization);
}
}
OrganizationController.java
@RestController
@RequestMapping("/organization")
@Slf4j
public class OrgController {
@Autowired
private OrganizationService organizationService;
@Autowired
private UserService userService;
/**
* 建立组织,生成邀请码
* @param name
* @param desc
* @return
* @throws Exception
*/
@PostMapping(value = "/createOrg")
public ResultEntity createOrg(String name,String desc,String courseId ,String beginTime ,String endTime) throws Exception {
Integer founderId = UserUtil.getCurrentUser().getId();
String founderName = userService.getUserById(founderId).getUsername();
//System.out.println(founderName);
Organization organization = new Organization(null, name, null, courseId, founderId, founderName, desc, 0, null, null ,beginTime, endTime);
String str = UserUtil.getVerifyCode(6);
while(organizationService.getOrgByCode(str)!=null)
{
str = UserUtil.getVerifyCode(6);
}
int id = Integer.parseInt(UserUtil.getVerifyCode(6));
while(organizationService.getOrgById(id)!=null)
{
id = Integer.parseInt(UserUtil.getVerifyCode(6));
}
organization.setId(id);
organization.setCode(str);
System.out.println(organization);
organizationService.addOrg(organization);
return ResultEntity.success(organization.getId());
}
@PostMapping(value = "/createOrgMessage")
public ResultEntity createOrgMessage(String name,String desc,String courseId ,String beginTime ,String endTime) throws Exception {
Integer founderId = UserUtil.getCurrentUser().getId();
String founderName = userService.getUserById(founderId).getUsername();
//System.out.println(founderName);
Organization organization = new Organization(null, name, null, courseId, founderId, founderName, desc, 0, null, null ,beginTime, endTime);
String str = UserUtil.getVerifyCode(6);
while(organizationService.getOrgByCode(str)!=null)
{
str = UserUtil.getVerifyCode(6);
}
int id = Integer.parseInt(UserUtil.getVerifyCode(6));
while(organizationService.getOrgById(id)!=null)
{
id = Integer.parseInt(UserUtil.getVerifyCode(6));
}
organization.setId(id);
organization.setCode(str);
System.out.println(organization);
organizationService.addOrg(organization);
return ResultEntity.success(organization);
}
/**
*
* 通过ID获取组织信息
* @param orgId
* @return
*/
@PostMapping(value = "/getOrgMessageById")
public ResultEntity getOrgMessageById(int orgId) {
Organization org = organizationService.getOrgById(orgId);
if(org != null)
return ResultEntity.success(org);
else
return ResultEntity.error("Organization not found",null);
}
/**
* 通过code获取组织信息
* @param code
* @return
*/
@PostMapping(value = "/getOrgMessageByCode")
public ResultEntity getOrgMessageByCode(String code) {
Organization org = organizationService.getOrgByCode(code);
if(org != null)
return ResultEntity.success(org);
else
return ResultEntity.error("Organization not found",null);
}
/**
* 学生确认加入组织
* @param orgId
* @return
*/
@PostMapping(value = "/confirmJoinOrg")
public ResultEntity ConfirmJoinOrg(int orgId) {
if(organizationService.getOrgById(orgId)==null)
return ResultEntity.error("Organization not found",null);
Integer orgId1 = Integer.valueOf(orgId);
Integer userId = UserUtil.getCurrentUser().getId();
//System.out.println("PPPP+++++" + user_id);
organizationService.JoinOrg(orgId1, userId);
return ResultEntity.success();
}
/**
* 学生确认退出组织
* @param orgId
* @return
*/
@PostMapping(value = "/confirmQuitOrg")
public ResultEntity ConfirmQuitOrg(int orgId) {
if(organizationService.getOrgById(orgId)==null)
return ResultEntity.error("Organization not found",null);
//System.out.println("PPPP+++++" + user_id);
Integer orgId2 = Integer.valueOf(orgId);
Integer userId = UserUtil.getCurrentUser().getId();
organizationService.QuitOrg(orgId2, userId);
return ResultEntity.success();
}
/**
* 获取学生列表
* @param orgId
* @return
*/
@PostMapping(value = "/getStuList")
public ResultEntity getStuList(int orgId) {
if(organizationService.getOrgById(orgId)==null)
return ResultEntity.error("Organization not found",null);
UserList stuList = organizationService.getStuList(orgId);
System.out.println(stuList);
return ResultEntity.success(stuList.getUserList());
}
/**
* 获取老师建立的组织列表ById
* @param founderId
* @return
*/
@PostMapping(value = "/getOrgListByFounderId")
public ResultEntity getOrgListByFounderId(int founderId) {
List<Organization> list = organizationService.getOrgListByFounderId(founderId);
return ResultEntity.success(list);
}
/**
* 获取当前用户创建的组织
* @return
*/
@PostMapping(value = "/getOrgListByFounder")
public ResultEntity getOrgListByFounder() {
Integer founderId = UserUtil.getCurrentUser().getId();
List<Organization> list = organizationService.getOrgListByFounderId(founderId);
return ResultEntity.success(list);
}
/**
* 获取用户参加的组织列表ById
* @param userId
* @return
*/
@PostMapping(value = "/getOrgListByUserId")
public ResultEntity getOrgList(int userId) {
List<Organization> list = organizationService.getOrgListByUserId(userId);
return ResultEntity.success(list);
}
/**
* 修改组织信息
* @param id
* @param name
* @param desc
* @param courseId
* @param beginTime
* @param endTime
* @return
*/
@PostMapping(value = "/updateOrg")
public ResultEntity updateOrg(int id, String name, String desc, String courseId, String beginTime, String endTime) {
Organization org = organizationService.getOrgById(id);
if(org==null)
return ResultEntity.error("Organization not found",null);
org.setName(name);
org.setCourseId(courseId);
org.setDescription(desc);
org.setBeginTime(beginTime);
org.setEndTime(endTime);
organizationService.updateOrg(org);
return ResultEntity.success(organizationService.getOrgById(id));
}
@PostMapping(value = "/deleteOrg")
public ResultEntity deleteOrg(int id) {
if(organizationService.getOrgById(id)==null)
return ResultEntity.error("Organization not found",null);
organizationService.removeOrg(id);
return ResultEntity.success();
}
}