diff --git a/backend/src/main/java/com/twtw/backend/domain/group/dto/request/MakeGroupDto.java b/backend/src/main/java/com/twtw/backend/domain/group/dto/request/MakeGroupDto.java new file mode 100644 index 00000000..76520dcf --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/domain/group/dto/request/MakeGroupDto.java @@ -0,0 +1,13 @@ +package com.twtw.backend.domain.group.dto.request; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class MakeGroupDto { + private String name; + private String groupImage; +} diff --git a/backend/src/main/java/com/twtw/backend/domain/group/dto/GroupInfoDto.java b/backend/src/main/java/com/twtw/backend/domain/group/dto/response/GroupInfoDto.java similarity index 83% rename from backend/src/main/java/com/twtw/backend/domain/group/dto/GroupInfoDto.java rename to backend/src/main/java/com/twtw/backend/domain/group/dto/response/GroupInfoDto.java index 210afae3..ca3329ac 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/dto/GroupInfoDto.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/dto/response/GroupInfoDto.java @@ -1,4 +1,4 @@ -package com.twtw.backend.domain.group.dto; +package com.twtw.backend.domain.group.dto.response; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java b/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java index b7c80de0..bc282706 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java @@ -22,6 +22,11 @@ public class Group { private String name; private String groupImage; - @OneToMany(mappedBy = "group") + @OneToMany(mappedBy = "group",cascade = {CascadeType.PERSIST,CascadeType.MERGE}) private List groupMembers = new ArrayList<>(); + + public Group(String name,String groupImage){ + this.name = name; + this.groupImage = groupImage; + } } diff --git a/backend/src/main/java/com/twtw/backend/domain/group/entity/GroupMember.java b/backend/src/main/java/com/twtw/backend/domain/group/entity/GroupMember.java index c635dea5..e246ae59 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/entity/GroupMember.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/entity/GroupMember.java @@ -26,4 +26,11 @@ public class GroupMember { @ManyToOne @JoinColumn(name = "member_id") private Member member; + + public GroupMember(Group group,Member member){ + this.group = group; + this.member = member; + group.getGroupMembers().add(this); + member.getGroupMembers().add(this); + } } diff --git a/backend/src/main/java/com/twtw/backend/domain/group/mapper/GroupMapper.java b/backend/src/main/java/com/twtw/backend/domain/group/mapper/GroupMapper.java new file mode 100644 index 00000000..98c5cc54 --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/domain/group/mapper/GroupMapper.java @@ -0,0 +1,18 @@ +package com.twtw.backend.domain.group.mapper; + +import com.twtw.backend.domain.group.dto.request.MakeGroupDto; +import com.twtw.backend.domain.group.entity.Group; +import com.twtw.backend.domain.group.entity.GroupMember; +import com.twtw.backend.domain.member.entity.Member; +import org.springframework.stereotype.Component; + +@Component +public class GroupMapper { + public GroupMember connectGroupMember(Group group, Member member){ + return new GroupMember(group,member); + } + + public Group toGroupEntity(MakeGroupDto groupDto){ + return new Group(groupDto.getName(),groupDto.getGroupImage()); + } +} diff --git a/backend/src/main/java/com/twtw/backend/domain/group/service/GroupService.java b/backend/src/main/java/com/twtw/backend/domain/group/service/GroupService.java index 147e2f2a..c288200d 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/service/GroupService.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/service/GroupService.java @@ -1,14 +1,59 @@ package com.twtw.backend.domain.group.service; +import com.twtw.backend.domain.group.dto.request.MakeGroupDto; +import com.twtw.backend.domain.group.entity.Group; +import com.twtw.backend.domain.group.entity.GroupMember; +import com.twtw.backend.domain.group.mapper.GroupMapper; +import com.twtw.backend.domain.group.repository.GroupMemberRepository; import com.twtw.backend.domain.group.repository.GroupRepository; +import com.twtw.backend.domain.member.entity.Member; +import com.twtw.backend.domain.member.service.AuthService; +import com.twtw.backend.domain.member.service.MemberService; +import com.twtw.backend.global.exception.EntityNotFoundException; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.UUID; @Service public class GroupService { private final GroupRepository groupRepository; - - public GroupService(GroupRepository groupRepository) { + private final GroupMemberRepository groupMemberRepository; + private final AuthService authService; + private final GroupMapper groupMapper; + private final MemberService memberService; + public GroupService(GroupRepository groupRepository,GroupMemberRepository groupMemberRepository,AuthService authService,GroupMapper groupMapper,MemberService memberService) { this.groupRepository = groupRepository; + this.groupMemberRepository = groupMemberRepository; + this.authService = authService; + this.groupMapper = groupMapper; + this.memberService = memberService; + } + + @Transactional + public void makeGroup(MakeGroupDto groupDto){ + Member member = this.authService.getMemberByJwt(); + Group group = this.groupMapper.toGroupEntity(groupDto); + + GroupMember groupMember = this.groupMapper.connectGroupMember(group,member); + + groupMemberRepository.save(groupMember); + } + @Transactional + public void joinGroup(UUID groupId){ + Member member = this.authService.getMemberByJwt(); + Group group = this.groupRepository.findById(groupId).orElseThrow(EntityNotFoundException::new); + + GroupMember groupMember = this.groupMapper.connectGroupMember(group,member); + + groupMemberRepository.save(groupMember); } + + + public void removeGroup(UUID groupId){ + // TODO() + } + + } diff --git a/backend/src/main/java/com/twtw/backend/domain/member/entity/Member.java b/backend/src/main/java/com/twtw/backend/domain/member/entity/Member.java index 4654903d..1bd955cd 100644 --- a/backend/src/main/java/com/twtw/backend/domain/member/entity/Member.java +++ b/backend/src/main/java/com/twtw/backend/domain/member/entity/Member.java @@ -30,7 +30,7 @@ public class Member { @Embedded private OAuth2Info oAuth2Info; - @OneToMany(mappedBy = "member") + @OneToMany(mappedBy = "member",cascade = {CascadeType.PERSIST,CascadeType.MERGE}) private List groupMembers = new ArrayList<>(); public Member(String nickname, String profileImage) { diff --git a/backend/src/test/java/com/twtw/backend/BackendApplicationTests.java b/backend/src/test/java/com/twtw/backend/BackendApplicationTests.java index 4cfaf355..f41711ee 100644 --- a/backend/src/test/java/com/twtw/backend/BackendApplicationTests.java +++ b/backend/src/test/java/com/twtw/backend/BackendApplicationTests.java @@ -1,13 +1,15 @@ package com.twtw.backend; +import com.twtw.backend.domain.group.service.GroupService; +import com.twtw.backend.domain.member.entity.Member; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; @SpringBootTest @ActiveProfiles("test") class BackendApplicationTests { - @Test void contextLoads() {} }