diff --git a/backend/src/main/java/com/twtw/backend/config/database/QuerydslConfig.java b/backend/src/main/java/com/twtw/backend/config/database/QuerydslConfig.java index 300d253f..f477ab53 100644 --- a/backend/src/main/java/com/twtw/backend/config/database/QuerydslConfig.java +++ b/backend/src/main/java/com/twtw/backend/config/database/QuerydslConfig.java @@ -1,16 +1,19 @@ package com.twtw.backend.config.database; import com.querydsl.jpa.impl.JPAQueryFactory; - import jakarta.persistence.EntityManager; - +import jakarta.persistence.PersistenceContext; +import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class QuerydslConfig { + @PersistenceContext + private EntityManager entityManager; + @Bean - public JPAQueryFactory jpaQueryFactory(final EntityManager entityManager) { + public JPAQueryFactory jpaQueryFactory() { return new JPAQueryFactory(entityManager); } } 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 90bddb47..5360530b 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 @@ -13,6 +13,7 @@ @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) +@Table(name = "GROUP_TABLE") public class Group { @Id @GeneratedValue(generator = "uuid2") @@ -22,13 +23,12 @@ public class Group { private String name; private String groupImage; - @OneToMany( - mappedBy = "group", - cascade = {CascadeType.PERSIST, CascadeType.MERGE}) + @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 f23785b2..ef39122e 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 @@ -33,4 +33,5 @@ public GroupMember(Group group, Member member) { group.getGroupMembers().add(this); member.getGroupMembers().add(this); } + } 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 0a8f7b6f..291733c1 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 @@ -13,49 +13,39 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - import java.util.UUID; @Service public class GroupService { private final 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) { + GroupMapper groupMapper) { 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); + Member member = authService.getMemberByJwt(); + Group group = groupMapper.toGroupEntity(groupDto); + GroupMember groupMember = groupMapper.connectGroupMember(group, member); - GroupMember groupMember = this.groupMapper.connectGroupMember(group, member); - - groupMemberRepository.save(groupMember); + groupRepository.save(group); } @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); + groupRepository.findById(groupId).orElseThrow(EntityNotFoundException::new); - groupMemberRepository.save(groupMember); + GroupMember groupMember = groupMapper.connectGroupMember(group, member); } public void removeGroup(UUID groupId) { 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 effd9def..e793cddd 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 @@ -15,6 +15,7 @@ @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) +@Table(name = "MEMBER_TABLE") public class Member { @Id @GeneratedValue(generator = "uuid2") @@ -30,9 +31,7 @@ public class Member { @Embedded private OAuth2Info oAuth2Info; - @OneToMany( - mappedBy = "member", - cascade = {CascadeType.PERSIST, CascadeType.MERGE}) + @OneToMany(mappedBy = "member") private List groupMembers = new ArrayList<>(); public Member(String nickname, String profileImage) { diff --git a/backend/src/main/resources/application-test.yml b/backend/src/main/resources/application-test.yml index 9d4b547b..42a0d300 100644 --- a/backend/src/main/resources/application-test.yml +++ b/backend/src/main/resources/application-test.yml @@ -25,6 +25,7 @@ spring: hibernate: ddl-auto: create database-platform: org.hibernate.dialect.H2Dialect + generate-ddl: true h2: console: diff --git a/backend/src/test/java/com/twtw/backend/group/GroupTest.java b/backend/src/test/java/com/twtw/backend/group/GroupTest.java new file mode 100644 index 00000000..500d5dd3 --- /dev/null +++ b/backend/src/test/java/com/twtw/backend/group/GroupTest.java @@ -0,0 +1,67 @@ +package com.twtw.backend.group; + +import com.twtw.backend.config.database.QuerydslConfig; +import com.twtw.backend.domain.group.entity.Group; +import com.twtw.backend.domain.group.entity.GroupMember; +import com.twtw.backend.domain.group.repository.GroupMemberRepository; +import com.twtw.backend.domain.group.repository.GroupRepository; +import com.twtw.backend.domain.member.entity.AuthType; +import com.twtw.backend.domain.member.entity.Member; +import com.twtw.backend.domain.member.entity.OAuth2Info; +import com.twtw.backend.domain.member.repository.MemberRepository; +import jakarta.persistence.EntityNotFoundException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +@DataJpaTest +@ActiveProfiles("test") +@Import(QuerydslConfig.class) +public class GroupTest { + @Autowired + private GroupRepository groupRepository; + + @Autowired + private GroupMemberRepository groupMemberRepository; + + @Autowired + private MemberRepository memberRepository; + + @BeforeEach + public void beforeEach(){ + Member member = new Member("jinjooone","22222"); + OAuth2Info info = new OAuth2Info("ADMIN",AuthType.KAKAO); + member.updateOAuth(info); + + memberRepository.save(member); + + Group group = new Group("AAA","1111"); + groupRepository.save(group); + } + + @Test + @Transactional + void saveGroup(){ + Member member = memberRepository.findByOAuthIdAndAuthType("ADMIN",AuthType.KAKAO).orElseThrow(EntityNotFoundException::new); + Group group = new Group("HDJ","1111"); + GroupMember groupMember = new GroupMember(group,member); + Group regroup = groupRepository.save(group); + } + + @Test + @Transactional + void joinGroup(){ + + } + +}