Skip to content

Commit

Permalink
[ADD] Group Service
Browse files Browse the repository at this point in the history
  • Loading branch information
jinjoo-lab committed Oct 10, 2023
1 parent 5f45211 commit da00c13
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Member {

@Embedded private OAuth2Info oAuth2Info;

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

public Member(String nickname, String profileImage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
}

0 comments on commit da00c13

Please sign in to comment.