|
|
@@ -1,118 +1,92 @@
|
|
|
-//package seecoder.devcloud.web.service.impl.user;
|
|
|
-//
|
|
|
-//
|
|
|
-//import org.gitlab4j.api.GitLabApiException;
|
|
|
-//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-//import org.springframework.context.ApplicationContext;
|
|
|
-//import org.springframework.stereotype.Service;
|
|
|
-//import org.springframework.transaction.annotation.Transactional;
|
|
|
-//import seecoder.devcloud.api.gitlab.GitlabApi;
|
|
|
-//import seecoder.devcloud.api.gitlab.model.GitlabGroup;
|
|
|
-//import seecoder.devcloud.common.exceptions.EntityNotFoundException;
|
|
|
-//import seecoder.devcloud.web.dao.user.GroupMapper;
|
|
|
-//import seecoder.devcloud.web.dao.user.UserMapper;
|
|
|
-//import seecoder.devcloud.web.enums.GroupType;
|
|
|
-//import seecoder.devcloud.web.po.user.Group;
|
|
|
-//import seecoder.devcloud.web.po.user.User;
|
|
|
-//import seecoder.devcloud.web.service.user.GroupService;
|
|
|
-//import seecoder.devcloud.web.vo.user.CustomUserDetails;
|
|
|
-//import seecoder.devcloud.web.vo.user.GroupVO;
|
|
|
-//
|
|
|
-//import java.util.ArrayList;
|
|
|
-//import java.util.List;
|
|
|
-//import java.util.UUID;
|
|
|
-//
|
|
|
-///**
|
|
|
-// * @ClassName GroupServiceImp
|
|
|
-// * @PackageName com.moekr.moocoder.logic.service.impl
|
|
|
-// * @Author sheen
|
|
|
-// * @Date 2019/1/9
|
|
|
-// * @Version 1.0
|
|
|
-// * @Description //TODO
|
|
|
-// **/
|
|
|
-//@Service
|
|
|
-//public class GroupServiceImp implements GroupService {
|
|
|
-// private final GroupMapper groupMapper;
|
|
|
-// private final UserMapper userMapper;
|
|
|
-// private final GitlabApi gitlabApi;
|
|
|
-// @Autowired
|
|
|
-// ApplicationContext applicationContext;
|
|
|
-//
|
|
|
-// @Autowired
|
|
|
-// public GroupServiceImp(GroupMapper groupMapper, UserMapper userMapper, GitlabApi gitlabApi) {
|
|
|
-// this.groupMapper = groupMapper;
|
|
|
-// this.userMapper = userMapper;
|
|
|
-// this.gitlabApi = gitlabApi;
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// @Transactional
|
|
|
-// public GroupVO createGroup(String name, Integer userId) throws EntityNotFoundException {
|
|
|
-// User user = userMapper.findById(userId);
|
|
|
-//
|
|
|
-// List<User> members = new ArrayList<>();
|
|
|
-// members.add(user);
|
|
|
-// GitlabGroup gitGroup;
|
|
|
-// try {
|
|
|
-// gitGroup = gitlabApi.createGroup(name);
|
|
|
-// gitlabApi.addMemberToGroup(gitGroup.getId(), user.getId());
|
|
|
-// } catch (GitLabApiException e) {
|
|
|
-// throw new RuntimeException("创建Group时发生异常[" + e.getMessage() + "]");
|
|
|
-// }
|
|
|
-// Group group = Group.builder()
|
|
|
-// .id(gitGroup.getId())
|
|
|
-// .name(name)
|
|
|
-// .namespace(gitGroup.getNamespace())
|
|
|
-// .shareLink(UUID.randomUUID().toString().replace("-", ""))
|
|
|
-// .type(GroupType.GROUP)
|
|
|
-// .members(members)
|
|
|
-// .build();
|
|
|
-// groupMapper.insert(group, "members");
|
|
|
-//
|
|
|
-// //todo 组用户表
|
|
|
-// userMapper.save(user);
|
|
|
-// // 发出小组创建的事件
|
|
|
-// GroupVO groupVO = new GroupVO(group);
|
|
|
-// //applicationContext.publishEvent(new GroupCreateEvent(this, courseId, groupVO));
|
|
|
-// return groupVO;
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// @Transactional
|
|
|
-// public GroupVO addMember(String code, CustomUserDetails userDetails) throws EntityNotFoundException {
|
|
|
-// Group group = groupMapper.findGroupByShareLink(code);
|
|
|
-// if (group == null) {
|
|
|
-// throw new EntityNotFoundException("小组码错误!");
|
|
|
-// }
|
|
|
-// User user = userMapper.findByUsername(userDetails.getUsername());
|
|
|
-// final boolean haveCourse = user.getCourses().contains(group.getCourses());
|
|
|
-// if (!haveCourse) {
|
|
|
-// throw new RuntimeException("尚未加入课程!");
|
|
|
-// } else {
|
|
|
-// final boolean isJoin = user.getGroup().stream()
|
|
|
-// .anyMatch(g -> g.getCourses().getId().equals(group.getCourses().getId()));
|
|
|
-// if (isJoin) {
|
|
|
-// throw new RuntimeException("已经加入小组!");
|
|
|
-// }
|
|
|
-// group.getMembers().add(user);
|
|
|
-// try {
|
|
|
-// gitlabApi.addMemberToGroup(group.getId(), user.getId());
|
|
|
-// } catch (GitLabApiException e) {
|
|
|
-// throw new RuntimeException("将成员加入Group时发生异常[" + e.getMessage() + "]");
|
|
|
-// }
|
|
|
-// groupMapper.save(group);
|
|
|
-// userMapper.save(user);
|
|
|
-// return new GroupVO(group);
|
|
|
-// }
|
|
|
-//
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public GroupVO getGroupsById(Integer groupId) {
|
|
|
-// Group get= groupMapper.findById(groupId).orElseGet(Group::new);
|
|
|
-//
|
|
|
-// return new GroupVO(get);
|
|
|
-// }
|
|
|
-//}
|
|
|
+package seecoder.devcloud.web.service.impl.user;
|
|
|
+
|
|
|
+
|
|
|
+import org.gitlab4j.api.GitLabApiException;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import seecoder.devcloud.api.gitlab.GitlabApi;
|
|
|
+import seecoder.devcloud.api.gitlab.model.GitlabGroup;
|
|
|
+import seecoder.devcloud.common.exceptions.EntityNotFoundException;
|
|
|
+import seecoder.devcloud.web.dao.user.GroupMapper;
|
|
|
+import seecoder.devcloud.web.dao.user.GroupMemberMapper;
|
|
|
+import seecoder.devcloud.web.dao.user.UserMapper;
|
|
|
+import seecoder.devcloud.web.enums.GroupType;
|
|
|
+import seecoder.devcloud.web.po.user.Group;
|
|
|
+import seecoder.devcloud.web.po.user.User;
|
|
|
+import seecoder.devcloud.web.service.user.GroupService;
|
|
|
+import seecoder.devcloud.web.vo.user.CustomUserDetails;
|
|
|
+import seecoder.devcloud.web.vo.user.GroupVO;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+
|
|
|
+@Service
|
|
|
+public class GroupServiceImp implements GroupService {
|
|
|
+ private final GroupMapper groupMapper;
|
|
|
+ private final UserMapper userMapper;
|
|
|
+ private final GroupMemberMapper groupMemberMapper;
|
|
|
+ private final GitlabApi gitlabApi;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public GroupServiceImp(GroupMapper groupMapper, UserMapper userMapper, GroupMemberMapper groupMemberMapper, GitlabApi gitlabApi) {
|
|
|
+ this.groupMapper = groupMapper;
|
|
|
+ this.userMapper = userMapper;
|
|
|
+ this.groupMemberMapper = groupMemberMapper;
|
|
|
+ this.gitlabApi = gitlabApi;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public GroupVO createGroup(String name, Integer userId) throws EntityNotFoundException {
|
|
|
+ User user = userMapper.findById(userId);
|
|
|
+
|
|
|
+ List<User> members = new ArrayList<>();
|
|
|
+ members.add(user);
|
|
|
+ GitlabGroup gitGroup;
|
|
|
+ try {
|
|
|
+ gitGroup = gitlabApi.createGroup(name);
|
|
|
+ gitlabApi.addMemberToGroup(gitGroup.getId(), user.getId());
|
|
|
+ } catch (GitLabApiException e) {
|
|
|
+ throw new RuntimeException("创建Group时发生异常[" + e.getMessage() + "]");
|
|
|
+ }
|
|
|
+ Group group = Group.builder()
|
|
|
+ .id(gitGroup.getId())
|
|
|
+ .name(name)
|
|
|
+ .namespace(gitGroup.getNamespace())
|
|
|
+ .shareLink(UUID.randomUUID().toString().replace("-", ""))
|
|
|
+ .type(GroupType.GROUP)
|
|
|
+ .members(members)
|
|
|
+ .build();
|
|
|
+ groupMapper.insert(group, "members");
|
|
|
+ groupMemberMapper.insert(gitGroup.getId(),userId);
|
|
|
+ return new GroupVO(group);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public GroupVO addMember(Integer userId, Integer invitedUserId, Integer groupId) throws EntityNotFoundException {
|
|
|
+ List<Integer> userIds = groupMemberMapper.selectUserIdsByGroupId(groupId);
|
|
|
+ if (!userIds.contains(userId)) {
|
|
|
+ throw new EntityNotFoundException("小组邀请发生错误,用户没有权限!");
|
|
|
+ }
|
|
|
+ if (userIds.contains(invitedUserId)) {
|
|
|
+ throw new RuntimeException("邀请的用户已经加入小组!");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ gitlabApi.addMemberToGroup(groupId, invitedUserId);
|
|
|
+ } catch (GitLabApiException e) {
|
|
|
+ throw new RuntimeException("将成员加入Group时发生异常[" + e.getMessage() + "]");
|
|
|
+ }
|
|
|
+ groupMemberMapper.insert(groupId,invitedUserId);
|
|
|
+
|
|
|
+ return new GroupVO(group);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|