From 1f927caa1f6129246fac7f368ac8a3ee3cd6c3da Mon Sep 17 00:00:00 2001 From: liberhe <411497616@qq.com> Date: Wed, 13 Dec 2023 11:41:48 +0800 Subject: [PATCH 1/2] refactor team service --- .../com/dl/officialsite/aave/Schedule.java | 10 ++-- .../officialsite/common/enums/CodeEnums.java | 2 + .../com/dl/officialsite/sponsor/Sponsor.java | 2 + .../dl/officialsite/team/TeamController.java | 6 +-- .../com/dl/officialsite/team/TeamService.java | 46 +++++++++++++------ ...msMembersVo.java => TeamsWithMembers.java} | 2 +- 6 files changed, 44 insertions(+), 24 deletions(-) rename src/main/java/com/dl/officialsite/team/vo/{TeamsMembersVo.java => TeamsWithMembers.java} (94%) diff --git a/src/main/java/com/dl/officialsite/aave/Schedule.java b/src/main/java/com/dl/officialsite/aave/Schedule.java index fc351f9f..afe54766 100644 --- a/src/main/java/com/dl/officialsite/aave/Schedule.java +++ b/src/main/java/com/dl/officialsite/aave/Schedule.java @@ -4,7 +4,7 @@ import com.dl.officialsite.member.Member; import com.dl.officialsite.team.TeamService; import com.dl.officialsite.team.vo.TeamQueryVo; -import com.dl.officialsite.team.vo.TeamsMembersVo; +import com.dl.officialsite.team.vo.TeamsWithMembers; import java.math.BigInteger; import java.util.List; import lombok.extern.slf4j.Slf4j; @@ -42,9 +42,9 @@ public void monitorPrice() throws Exception { //查找team0 memeber获取地址 TeamQueryVo teamQueryVo = new TeamQueryVo(); teamQueryVo.setTeamName("Dapp-Learning DAO core founders"); - List teamAndMembers = teamService.getTeamAndMembers(teamQueryVo); + List teamAndMembers = teamService.getTeamWithMembersByTeamNameAndStatus(teamQueryVo); if (teamAndMembers.size() != 0) { - for (TeamsMembersVo teamAndMember : teamAndMembers) { + for (TeamsWithMembers teamAndMember : teamAndMembers) { for (Member member : teamAndMember.getMembers()) { String email = member.getEmail(); HealthInfo healthInfo = aaveService.getHealthInfo(member.getAddress()); @@ -65,9 +65,9 @@ public void monitorHealth() { //查找team0 memeber获取地址 TeamQueryVo teamQueryVo = new TeamQueryVo(); teamQueryVo.setTeamName("Dapp-Learning DAO core founders"); - List teamAndMembers = teamService.getTeamAndMembers(teamQueryVo); + List teamAndMembers = teamService.getTeamWithMembersByTeamNameAndStatus(teamQueryVo); if (teamAndMembers.size() != 0) { - for (TeamsMembersVo teamAndMember : teamAndMembers) { + for (TeamsWithMembers teamAndMember : teamAndMembers) { for (Member member : teamAndMember.getMembers()) { String email = member.getEmail(); HealthInfo healthInfo = aaveService.getHealthInfo(member.getAddress()); diff --git a/src/main/java/com/dl/officialsite/common/enums/CodeEnums.java b/src/main/java/com/dl/officialsite/common/enums/CodeEnums.java index a873770f..b207abb3 100644 --- a/src/main/java/com/dl/officialsite/common/enums/CodeEnums.java +++ b/src/main/java/com/dl/officialsite/common/enums/CodeEnums.java @@ -19,11 +19,13 @@ public enum CodeEnums { TEAM_NOT_EXIST("1005", "team not exist"), LOGIN_IN("2001", "please login"), TEAM_ADMIN_NOT_EXIST("1006", "team admin not exist"), + MEMBER_ALREADY_REQUEST_TEAM("1007", "member already request team"), NOT_FOUND_JD("1008", "not found jd"), NOT_FOUND_MEMBER("1009", "not found user"), NOT_THE_ADMIN("1010", "user not the admin"), + TEAM_JOIN_APPLICATION_NOT_EXIST("1011", "team join application not exist"), //Sharing SHARING_NOT_FOUND("5001", "Sharing not found"), SHARING_NOT_OWNER("5002", "You are no sharing user"), diff --git a/src/main/java/com/dl/officialsite/sponsor/Sponsor.java b/src/main/java/com/dl/officialsite/sponsor/Sponsor.java index b3e91679..e52eefd0 100644 --- a/src/main/java/com/dl/officialsite/sponsor/Sponsor.java +++ b/src/main/java/com/dl/officialsite/sponsor/Sponsor.java @@ -27,4 +27,6 @@ public class Sponsor { private String icon; + private String remark; + } diff --git a/src/main/java/com/dl/officialsite/team/TeamController.java b/src/main/java/com/dl/officialsite/team/TeamController.java index 62b1d7fa..2389643b 100644 --- a/src/main/java/com/dl/officialsite/team/TeamController.java +++ b/src/main/java/com/dl/officialsite/team/TeamController.java @@ -9,7 +9,7 @@ import com.dl.officialsite.team.vo.TeamMemberJoinVO; import com.dl.officialsite.team.vo.TeamQueryVo; import com.dl.officialsite.team.vo.TeamVO; -import com.dl.officialsite.team.vo.TeamsMembersVo; +import com.dl.officialsite.team.vo.TeamsWithMembers; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -87,13 +87,13 @@ BaseResponse approve(@RequestBody TeamMemberApproveVO teamMemberApproveVO, @Requ */ @PostMapping("/all") BaseResponse getTeams(@RequestBody TeamQueryVo teamQueryVo, @RequestParam String address) { - List teamAndMembers = teamService.getTeamAndMembers(teamQueryVo); + List teamAndMembers = teamService.getTeamWithMembersByTeamNameAndStatus(teamQueryVo); return BaseResponse.successWithData(teamAndMembers); } @GetMapping("/id") BaseResponse getTeamById(@RequestParam Long teamId, @RequestParam String address) { - TeamsMembersVo teamAndMembers = teamService.getTeamById(teamId); + TeamsWithMembers teamAndMembers = teamService.getTeamById(teamId); return BaseResponse.successWithData(teamAndMembers); } diff --git a/src/main/java/com/dl/officialsite/team/TeamService.java b/src/main/java/com/dl/officialsite/team/TeamService.java index 96338785..cd6cd901 100644 --- a/src/main/java/com/dl/officialsite/team/TeamService.java +++ b/src/main/java/com/dl/officialsite/team/TeamService.java @@ -15,7 +15,7 @@ import com.dl.officialsite.team.vo.TeamMemberJoinVO; import com.dl.officialsite.team.vo.TeamQueryVo; import com.dl.officialsite.team.vo.TeamVO; -import com.dl.officialsite.team.vo.TeamsMembersVo; +import com.dl.officialsite.team.vo.TeamsWithMembers; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -57,13 +57,14 @@ public class TeamService { private MemberService memberService; @Transactional + //todo only in DAO core founder team public Team add(TeamVO teamVO) { Team team = new Team(); BeanUtils.copyProperties(teamVO, team); return teamRepository.save(team); } - public List getTeamAndMembers(TeamQueryVo teamQueryVo) { + public List getTeamWithMembersByTeamNameAndStatus(TeamQueryVo teamQueryVo) { Specification queryParam = new Specification() { @Override public javax.persistence.criteria.Predicate toPredicate( @@ -80,10 +81,12 @@ public javax.persistence.criteria.Predicate toPredicate( } }; - List teamsMembersVos = new ArrayList<>(); + + //optimise todo + List teamsWithMembers = new ArrayList<>(); List teams = teamRepository.findAll(queryParam); teams.stream().forEach(team -> { - TeamsMembersVo teamsMembersVo = new TeamsMembersVo(); + TeamsWithMembers teamsMembersVo = new TeamsWithMembers(); BeanUtils.copyProperties(team, teamsMembersVo); List members = new ArrayList<>(); List memberIds = teamMemberRepository.findByTeamIdStatus(team.getId(), @@ -93,14 +96,17 @@ public javax.persistence.criteria.Predicate toPredicate( members.add(member); }); teamsMembersVo.setMembers(members); - teamsMembersVos.add(teamsMembersVo); + teamsWithMembers.add(teamsMembersVo); }); - return teamsMembersVos; + return teamsWithMembers; } @Transactional(rollbackOn = Exception.class) public void join(TeamMemberJoinVO teamMember) { + Member member = memberRepository.findById(teamMember.getMemberId()).get(); + + //todo /* if (ObjectUtils.isEmpty(member.getTelegramId()) || ObjectUtils.isEmpty( member.getWechatId())) { throw new BizException(CodeEnums.TELEGRAM_WECHAT_NOT_BIND.getCode(), @@ -108,8 +114,7 @@ public void join(TeamMemberJoinVO teamMember) { }*/ //判断是否已经退出过团队 Optional optional = teamMemberRepository.findByTeamAndMember( - teamMember.getTeamId() - , teamMember.getMemberId()); + teamMember.getTeamId(), teamMember.getMemberId()); if (optional.isPresent()) { TeamMember teamMember2 = optional.get(); if (teamMember2.getStatus() == Constants.REQUEST_TEAM) { @@ -175,6 +180,8 @@ public void join(TeamMemberJoinVO teamMember) { @Transactional(rollbackOn = Exception.class) public void approve(TeamMemberApproveVO teamMemberApproveVO) { + + //todo check team admin List memberIds = teamMemberApproveVO.getMemberIds(); List teamMembers = new ArrayList<>(); memberIds.stream().forEach(memberId -> { @@ -184,6 +191,9 @@ public void approve(TeamMemberApproveVO teamMemberApproveVO) { TeamMember teamMember = optional.get(); teamMember.setStatus(Constants.APPROVE_TEAM); teamMembers.add(teamMember); + } else { + throw new BizException(CodeEnums.TEAM_ADMIN_NOT_EXIST.getCode(), + CodeEnums.TEAM_ADMIN_NOT_EXIST.getMsg()); } }); teamMemberRepository.saveAll(teamMembers); @@ -191,11 +201,14 @@ public void approve(TeamMemberApproveVO teamMemberApproveVO) { public void exit(TeamMemberJoinVO teamMember) { + //todo check team admin or self teamMemberRepository.findByTeamAndMember(teamMember.getTeamId(), teamMember.getMemberId()).ifPresent(teamMember2 -> { teamMember2.setStatus(Constants.EXIT_TEAM); teamMemberRepository.save(teamMember2); - }); + }); // no exist case ? + // todo + Team team = teamRepository.findById(teamMember.getTeamId()).get(); Member member = memberRepository.findById(teamMember.getMemberId()).get(); String subject = team.getTeamName() + "团队成员退出"; @@ -251,10 +264,10 @@ public boolean checkMemberIsAdmin(String address) { } - public TeamsMembersVo getTeamById(Long teamId) { + public TeamsWithMembers getTeamById(Long teamId) { Optional optional = teamRepository.findById(teamId); if (optional.isPresent()) { - TeamsMembersVo teamsMembersVo = new TeamsMembersVo(); + TeamsWithMembers teamsMembersVo = new TeamsWithMembers(); Team team = optional.get(); List members = new ArrayList<>(); List memberIds = teamMemberRepository.findByTeamIdStatus(team.getId(), Constants.APPROVE_TEAM); @@ -273,15 +286,18 @@ public TeamsMembersVo getTeamById(Long teamId) { public void batchJoin(TeamMemberBatchJoinVO teamMembers) { + + // for admin interface + // todo batch insert ? teamMembers.getMemberIds().forEach(memberId -> { Optional optional = teamMemberRepository.findByTeamAndMember( teamMembers.getTeamId(), memberId); if (optional.isPresent()) { TeamMember teamMember2 = optional.get(); - if (teamMember2.getStatus() == Constants.REQUEST_TEAM) { - throw new BizException(CodeEnums.MEMBER_ALREADY_REQUEST_TEAM.getCode(), - CodeEnums.MEMBER_ALREADY_REQUEST_TEAM.getMsg()); - } +// if (teamMember2.getStatus() == Constants.REQUEST_TEAM) { +// throw new BizException(CodeEnums.MEMBER_ALREADY_REQUEST_TEAM.getCode(), +// CodeEnums.MEMBER_ALREADY_REQUEST_TEAM.getMsg()); +// } // ignore existence teamMember2.setStatus(Constants.APPROVE_TEAM); teamMemberRepository.save(teamMember2); } else { diff --git a/src/main/java/com/dl/officialsite/team/vo/TeamsMembersVo.java b/src/main/java/com/dl/officialsite/team/vo/TeamsWithMembers.java similarity index 94% rename from src/main/java/com/dl/officialsite/team/vo/TeamsMembersVo.java rename to src/main/java/com/dl/officialsite/team/vo/TeamsWithMembers.java index cb873d2d..65cc677c 100644 --- a/src/main/java/com/dl/officialsite/team/vo/TeamsMembersVo.java +++ b/src/main/java/com/dl/officialsite/team/vo/TeamsWithMembers.java @@ -11,7 +11,7 @@ * @Description TeamsMembersVo **/ @Data -public class TeamsMembersVo { +public class TeamsWithMembers { private Long id; From 071ebc36065a056dcfb2a7ad1cec251c74622e71 Mon Sep 17 00:00:00 2001 From: liberhe <411497616@qq.com> Date: Wed, 13 Dec 2023 11:42:15 +0800 Subject: [PATCH 2/2] refactor team service --- src/main/java/com/dl/officialsite/team/TeamService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/dl/officialsite/team/TeamService.java b/src/main/java/com/dl/officialsite/team/TeamService.java index cd6cd901..cabd182f 100644 --- a/src/main/java/com/dl/officialsite/team/TeamService.java +++ b/src/main/java/com/dl/officialsite/team/TeamService.java @@ -115,6 +115,8 @@ public void join(TeamMemberJoinVO teamMember) { //判断是否已经退出过团队 Optional optional = teamMemberRepository.findByTeamAndMember( teamMember.getTeamId(), teamMember.getMemberId()); + + // refactor if (optional.isPresent()) { TeamMember teamMember2 = optional.get(); if (teamMember2.getStatus() == Constants.REQUEST_TEAM) {