Skip to content

Commit

Permalink
[FEAT] MemberController Logic Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
jinjoo-lab committed Oct 11, 2023
1 parent d95bede commit 87502cc
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "GROUP_TABLE")
public class Group {
@Id
@GeneratedValue(generator = "uuid2")
Expand All @@ -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<GroupMember> groupMembers = new ArrayList<>();

public Group(String name, String groupImage) {
this.name = name;
this.groupImage = groupImage;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ public GroupMember(Group group, Member member) {
group.getGroupMembers().add(this);
member.getGroupMembers().add(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "MEMBER_TABLE")
public class Member {
@Id
@GeneratedValue(generator = "uuid2")
Expand All @@ -30,9 +31,7 @@ public class Member {

@Embedded private OAuth2Info oAuth2Info;

@OneToMany(
mappedBy = "member",
cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@OneToMany(mappedBy = "member")
private List<GroupMember> groupMembers = new ArrayList<>();

public Member(String nickname, String profileImage) {
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ spring:
hibernate:
ddl-auto: create
database-platform: org.hibernate.dialect.H2Dialect
generate-ddl: true

h2:
console:
Expand Down
67 changes: 67 additions & 0 deletions backend/src/test/java/com/twtw/backend/group/GroupTest.java
Original file line number Diff line number Diff line change
@@ -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(){

}

}

0 comments on commit 87502cc

Please sign in to comment.