Skip to content

Commit

Permalink
feat: 🎸 Group에 가입된 User를 다대다로 매핑
Browse files Browse the repository at this point in the history
Closes: #226
  • Loading branch information
sallyjellyy committed Nov 30, 2021
1 parent e3f313b commit 42b2180
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/main/java/com/postsquad/scoup/web/group/domain/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@

import com.postsquad.scoup.web.common.BaseEntity;
import com.postsquad.scoup.web.group.controller.request.GroupModificationRequest;
import com.postsquad.scoup.web.schedule.domain.ConfirmedSchedule;
import com.postsquad.scoup.web.schedule.domain.Schedule;
import com.postsquad.scoup.web.schedule.domain.ScheduleCandidate;
import com.postsquad.scoup.web.user.domain.User;
import lombok.*;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
Expand All @@ -39,6 +30,14 @@ public class Group extends BaseEntity {
@OneToMany(mappedBy = "group", cascade = CascadeType.ALL)
private final List<Schedule> schedules = new ArrayList<>();

@ManyToMany
@JoinTable(
name = "group_member",
joinColumns = @JoinColumn(name = "group_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private final List<User> members = new ArrayList<>();

protected Group(String name, String description, User owner) {
this.name = name;
this.description = description;
Expand Down Expand Up @@ -72,4 +71,9 @@ public void addSchedule(Schedule schedule) {
public void addSchedules(List<Schedule> schedules) {
this.schedules.addAll(schedules);
}

public void addMember(User user) {
user.getJoinedGroups().add(this);
this.members.add(user);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/postsquad/scoup/web/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.postsquad.scoup.web.auth.OAuthType;
import com.postsquad.scoup.web.common.BaseEntity;
import com.postsquad.scoup.web.group.domain.Group;
import lombok.*;

import javax.persistence.*;
Expand Down Expand Up @@ -36,6 +37,9 @@ public class User extends BaseEntity {
@CollectionTable(name = "oauth_user", joinColumns = @JoinColumn(name = "user_id"))
private List<OAuthUser> oAuthUsers = new ArrayList<>();

@ManyToMany(mappedBy = "members")
private List<Group> joinedGroups = new ArrayList<>();

protected User(String nickname, String username, String email, String avatarUrl, String password, List<OAuthUser> oAuthUsers) {
this.nickname = nickname;
this.username = username;
Expand Down

0 comments on commit 42b2180

Please sign in to comment.