Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

양방향 연관관계 편의메서드 작성 #131

Merged
merged 12 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.gdsc_knu.official_homepage.dto.post;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.gdsc_knu.official_homepage.entity.Member;
import com.gdsc_knu.official_homepage.entity.enumeration.Role;
import lombok.AllArgsConstructor;
import lombok.Builder;

@Builder
@AllArgsConstructor
public class AccessModel {
@JsonProperty(value = "canDelete")
private boolean delete;
private boolean modify;

Expand All @@ -19,10 +19,24 @@ public boolean canModify() {
return modify;
}

public static AccessModel of(boolean canDelete, boolean canModify) {
private static AccessModel of(boolean canDelete, boolean canModify) {
return AccessModel.builder()
.delete(canDelete)
.modify(canModify)
.build();
}


public static AccessModel calcCommentAccess(Long memberId, Long postAuthorId, Long commentAuthorId) {
boolean canDelete = memberId.equals(postAuthorId) || memberId.equals(commentAuthorId);
boolean canModify = memberId.equals(commentAuthorId);
return AccessModel.of(canDelete, canModify);
}

public static AccessModel calcPostAccess(Long memberId, Member postAuthor) {
boolean canModify = memberId.equals(postAuthor.getId());
boolean canDelete = memberId.equals(postAuthor.getId())
|| postAuthor.getRole().equals(Role.ROLE_CORE);
return AccessModel.of(canDelete, canModify);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import java.util.List;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member extends BaseTimeEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,32 @@

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MemberTeam {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(nullable = false)
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(nullable = false)
private Team team;

public void changeTeam(Team team) {
this.team = team;
}

public static MemberTeam from(Member member, Team team) {
return MemberTeam.builder()
.member(member)
.team(team)
.build();
}
}
52 changes: 42 additions & 10 deletions src/main/java/com/gdsc_knu/official_homepage/entity/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,66 @@

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String teamName;

@OneToMany(mappedBy = "team")
@Builder.Default
private List<MemberTeam> memberTeams = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Team parent;

@OneToMany(mappedBy = "team", cascade = CascadeType.ALL)
@Builder.Default
private List<MemberTeam> memberTeams = new ArrayList<>();

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
@Builder.Default
private List<Team> subTeams = new ArrayList<>();

public void addSubTeam(Team subteam) {

public static Team fromParent(Team parent) {
Team newSubTeam = Team.builder()
.teamName(parent.getNewSubTeamName())
.build();
parent.addSubTeam(newSubTeam);
return newSubTeam;
}

public static Team ofName(String teamName) {
return Team.builder()
.teamName(teamName)
.build();
}

private void addSubTeam(Team subteam) {
subTeams.add(subteam);
subteam.setParent(this);
subteam.parent = this;
}

private String getNewSubTeamName() {
return String.format("%s %d팀", this.teamName, this.subTeams.size() + 1);
}

private void setParent(Team parent) {
this.parent = parent;
public Team getLastSubTeam() {
int size = this.subTeams.size();
return this.subTeams.get(size-1);
}

public void addMember(Member member) {
MemberTeam memberTeam = MemberTeam.from(member, this);
memberTeams.add(memberTeam);
member.getMemberTeams().add(memberTeam);
}

public boolean isAssignedTeam() {
return this.parent != null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@
import com.gdsc_knu.official_homepage.entity.enumeration.ApplicationStatus;
import com.gdsc_knu.official_homepage.entity.enumeration.Track;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Application extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package com.gdsc_knu.official_homepage.entity.application;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ApplicationAnswer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private int questionNumber;

@Column(length = 1024)
private String answer;

@ManyToOne
@JoinColumn(nullable = false)
private Application application;

public void updateAnswer(String answer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,24 @@ public class Comment extends BaseTimeEntity {
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
private Post post;

@Column(length = 4000)
@Column(length = 4000, nullable = false)
private String content;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
private Member author;

@ManyToOne(fetch = FetchType.LAZY)
private Comment parent;

// 작성자 비정규화
private String authorName;
@Column(length = 500)
private String authorProfile;

@ManyToOne(fetch = FetchType.LAZY)
private Comment parent;

@Builder.Default
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> replies = new ArrayList<>();
Expand All @@ -53,6 +55,8 @@ public static Comment from(String content, Member author, Post post, Comment par
.authorProfile(author.getProfileUrl())
.build();
comment.parent = (parent == null) ? comment : parent;
post.commentList.add(comment);
post.addCommentCount();
return comment;
}

Expand All @@ -65,5 +69,16 @@ public boolean isChild(){
return !this.parent.getId().equals(this.id);
}

public boolean isCommentAuthor(Long memberId) {
return this.getAuthor().getId().equals(memberId);
}


public void delete() {
this.post.commentList.remove(this);
int deleteCount = 1 + this.getReplies().size();
this.post.subtractCommentCount(deleteCount);
}


}
17 changes: 10 additions & 7 deletions src/main/java/com/gdsc_knu/official_homepage/entity/post/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ public class Post {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Builder.Default
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
List<Comment> commentList = new ArrayList<>();

@Column(nullable = false)
private String title;

@Column(length = 65535)
private String content;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
private Member member;

private String thumbnailUrl;
Expand All @@ -48,6 +46,11 @@ public class Post {
private LocalDateTime publishedAt;
private LocalDateTime modifiedAt;

@Builder.Default
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
List<Comment> commentList = new ArrayList<>();


public void update(PostRequest.Update postRequest) {
this.title = postRequest.getTitle();
this.content = postRequest.getContent();
Expand All @@ -61,12 +64,12 @@ public boolean isSaved() {
return this.status.equals(PostStatus.SAVED);
}

public void addCommentCount() {
protected void addCommentCount() {
this.commentCount++;
}

public void subtractCommentCount(int deleteCount) {
this.commentCount =- deleteCount;
protected void subtractCommentCount(int deleteCount) {
this.commentCount -= deleteCount;
}

public void addLikeCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ public class PostLike {
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
private Member member;

public static PostLike from(Post post, Member member) {
post.addLikeCount();
return PostLike.builder()
.post(post)
.member(member)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.gdsc_knu.official_homepage.dto.oauth.LoginResponseDto;
import com.gdsc_knu.official_homepage.entity.Member;
import com.gdsc_knu.official_homepage.entity.enumeration.Role;
import com.gdsc_knu.official_homepage.repository.MemberRepository;
import com.gdsc_knu.official_homepage.repository.member.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gdsc_knu.official_homepage.repository;
package com.gdsc_knu.official_homepage.repository.application;

import com.gdsc_knu.official_homepage.entity.application.Application;
import com.gdsc_knu.official_homepage.entity.enumeration.Track;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gdsc_knu.official_homepage.repository;
package com.gdsc_knu.official_homepage.repository.application;

import com.gdsc_knu.official_homepage.entity.application.Application;
import com.gdsc_knu.official_homepage.entity.application.QApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gdsc_knu.official_homepage.repository;
package com.gdsc_knu.official_homepage.repository.application;

import com.gdsc_knu.official_homepage.dto.admin.application.ApplicationStatisticType;
import com.gdsc_knu.official_homepage.dto.admin.application.ApplicationTrackType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gdsc_knu.official_homepage.repository;
package com.gdsc_knu.official_homepage.repository.member;

import com.gdsc_knu.official_homepage.entity.Member;
import com.gdsc_knu.official_homepage.entity.enumeration.Track;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gdsc_knu.official_homepage.repository;
package com.gdsc_knu.official_homepage.repository.post;

import com.gdsc_knu.official_homepage.entity.post.Comment;
import org.springframework.data.domain.Page;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gdsc_knu.official_homepage.repository;
package com.gdsc_knu.official_homepage.repository.post;

import com.gdsc_knu.official_homepage.entity.post.PostLike;
import org.springframework.data.jpa.repository.JpaRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gdsc_knu.official_homepage.repository;
package com.gdsc_knu.official_homepage.repository.post;

import com.gdsc_knu.official_homepage.entity.post.Post;
import com.gdsc_knu.official_homepage.entity.post.enumeration.Category;
Expand Down
Loading
Loading