From 0d226dba51f6a0b4c13910889b82a363bf3d8852 Mon Sep 17 00:00:00 2001 From: Suyoung Oh Date: Sun, 19 Feb 2023 15:21:44 +0900 Subject: [PATCH 1/4] =?UTF-8?q?Add=20:=20Entity=20=EA=B5=AC=EC=A1=B0=20?= =?UTF-8?q?=EC=9E=AC=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/hearts/ClinicHeart.java | 30 +++++++++++++++++++ .../domain/hearts/PostHeart.java | 30 +++++++++++++++++++ .../domain/posts/ClinicPost.java | 30 +++++++++++++++++++ .../domain/posts/Feeling.java | 7 +++++ .../posts/{Posts.java => FeelingPost.java} | 19 +++++++----- .../domain/posts/Feelings.java | 10 ------- .../domain/user/User.java | 9 ++++-- .../repository/PostsRepository.java | 12 ++++---- .../repository/UserRepository.java | 1 - 9 files changed, 121 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/PostHeart.java create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/Feeling.java rename src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/{Posts.java => FeelingPost.java} (55%) delete mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/Feelings.java diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java new file mode 100644 index 0000000..4247372 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java @@ -0,0 +1,30 @@ +package com.gdschanyang.todayfeelingbackend2.domain.hearts; + +import com.gdschanyang.todayfeelingbackend2.domain.posts.ClinicPost; +import com.gdschanyang.todayfeelingbackend2.domain.user.User; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +@Getter +@NoArgsConstructor +@Entity +public class ClinicHeart { + // 공감한 유저, 공감한 클리닉 글 + @Id + private Long id; + + // ClinicHeart : User = 1 : n 매핑 + @OneToMany + @JoinColumn(name = "CLINIC_HEART_USER") + private List users = new ArrayList(); + + // ClinicHeart : ClinicPost = 1 : n 매핑 + @OneToMany + @JoinColumn(name = "CLINIC_HEART_POST") + private List ClinicPosts = new ArrayList(); + +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/PostHeart.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/PostHeart.java new file mode 100644 index 0000000..95e34a2 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/PostHeart.java @@ -0,0 +1,30 @@ +package com.gdschanyang.todayfeelingbackend2.domain.hearts; + +import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; +import com.gdschanyang.todayfeelingbackend2.domain.user.User; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +@Getter +@NoArgsConstructor +@Entity +public class PostHeart { + // 공감한 유저, 공감한 감정글 + @Id + private Long id; + + // PostHeart : User = 1 : n 매핑 + @OneToMany + @JoinColumn(name = "FEELING_HEART_USER") + private List users = new ArrayList(); + + // PostHeart : FeelingPost = 1 : n 매핑 + @OneToMany + @JoinColumn(name = "FEELING_HEART_POST") + private List feelingPosts = new ArrayList(); + +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java new file mode 100644 index 0000000..e2f48e4 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java @@ -0,0 +1,30 @@ +package com.gdschanyang.todayfeelingbackend2.domain.posts; + +import com.gdschanyang.todayfeelingbackend2.domain.user.User; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + +@Getter +@NoArgsConstructor +@Entity +public class ClinicPost { + // 제목, 글, 글 쓴 사람 + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(length = 500, nullable = false) + private String title; + + // 글 작성은 필수 + @Column(columnDefinition = "TEXT", nullable = false) + private String content; + + // User 와 ClinicPost 1:1 매핑 + @OneToOne + @JoinColumn(name = "USER_ID") + private User user; + +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/Feeling.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/Feeling.java new file mode 100644 index 0000000..8064464 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/Feeling.java @@ -0,0 +1,7 @@ +package com.gdschanyang.todayfeelingbackend2.domain.posts; + + +public enum Feeling { + POSITIVE, + NEGATIVE +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/Posts.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java similarity index 55% rename from src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/Posts.java rename to src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java index 5ba706b..d5cafb9 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/Posts.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java @@ -1,6 +1,7 @@ package com.gdschanyang.todayfeelingbackend2.domain.posts; +import com.gdschanyang.todayfeelingbackend2.domain.user.User; import lombok.Getter; import lombok.NoArgsConstructor; @@ -9,22 +10,26 @@ @Getter @NoArgsConstructor @Entity -public class Posts { - +public class FeelingPost { + // 감정, 글, 글 쓴 사람, 삭제 여부 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) - private Feelings feelings; - - @Column(length = 500, nullable = false) - private String title; + private Feeling feeling; // 글 작성은 선택적 @Column(columnDefinition = "TEXT") private String content; + // User 와 FeelingPost 1:1 매핑 + @OneToOne + @JoinColumn(name = "USER_ID") + private User user; + + // 삭제 여부 1:삭제 0:삭제X @Column(nullable = false) - private String author; + private boolean delFlag; + } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/Feelings.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/Feelings.java deleted file mode 100644 index 3f8098b..0000000 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/Feelings.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.gdschanyang.todayfeelingbackend2.domain.posts; - - -public enum Feelings { - SAD, // 슬픔, 우울 - ANGRY, // 화남 - HAPPY, // 행복함, 기쁨 - PROUD // 뿌듯함 - -} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java index ecbd324..186d070 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java @@ -13,10 +13,15 @@ public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "USER_ID") private Long id; - @Column(nullable = false) - private String name; + // 본명 + @Column(name = "USER_REALNAME", nullable = false) + private String realName; + // 닉네임으로 대나무숲과 클릭닉 센터에서 활동 + @Column(name = "USER_NICKNAME", nullable = false) + private String nickName; } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/PostsRepository.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/PostsRepository.java index 7305362..50e6225 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/PostsRepository.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/PostsRepository.java @@ -1,18 +1,16 @@ package com.gdschanyang.todayfeelingbackend2.repository; -import com.gdschanyang.todayfeelingbackend2.domain.posts.Posts; -import com.gdschanyang.todayfeelingbackend2.domain.user.User; +import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; import java.util.Optional; -public interface PostsRepository extends JpaRepository { +public interface PostsRepository extends JpaRepository { - Posts save(Posts posts); + FeelingPost save(FeelingPost posts); - Optional findById(Long id); - Optional findByAuthor(String name); + Optional findById(Long id); - List findAll(); + List findAll(); } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/UserRepository.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/UserRepository.java index 321060c..504e7a2 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/UserRepository.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/UserRepository.java @@ -11,7 +11,6 @@ public interface UserRepository extends JpaRepository { User save(User user); Optional findById(Long id); - Optional findByName(String name); List findAll(); From c40fa5d9f47c42d237a87136be00eb42fed760b5 Mon Sep 17 00:00:00 2001 From: Suyoung Oh Date: Tue, 21 Feb 2023 15:10:25 +0900 Subject: [PATCH 2/4] =?UTF-8?q?Refactor=20:=20=ED=85=8C=EC=9D=B4=EB=B8=94?= =?UTF-8?q?=20=EC=97=B0=EA=B4=80=EA=B4=80=EA=B3=84=20=EC=9E=AC=EC=84=A4?= =?UTF-8?q?=EC=A0=95,=20=ED=8E=B8=EC=9D=98=ED=95=A8=EC=88=98=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/hearts/ClinicHeart.java | 36 +++++++++---- .../domain/hearts/FeelingHeart.java | 44 +++++++++++++++ .../domain/hearts/PostHeart.java | 30 ----------- .../domain/posts/ClinicPost.java | 27 ++++++++-- .../domain/posts/FeelingPost.java | 31 +++++++++-- .../domain/user/User.java | 54 ++++++++++++++++--- .../repository/PostsRepository.java | 4 -- .../repository/UserRepository.java | 5 -- 8 files changed, 167 insertions(+), 64 deletions(-) create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java delete mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/PostHeart.java diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java index 4247372..78ce9d4 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java @@ -6,25 +6,39 @@ import lombok.NoArgsConstructor; import javax.persistence.*; -import java.util.ArrayList; -import java.util.List; @Getter @NoArgsConstructor @Entity public class ClinicHeart { - // 공감한 유저, 공감한 클리닉 글 + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "CLINICHEART_ID") private Long id; - // ClinicHeart : User = 1 : n 매핑 - @OneToMany - @JoinColumn(name = "CLINIC_HEART_USER") - private List users = new ArrayList(); + // 2. ClinicHeart : User = n : 1 + @ManyToOne + @JoinColumn(name = "USER_ID") + private User user; + + // 3. ClinicHeart : ClinicPost = n : 1 + @ManyToOne + @JoinColumn(name = "CLINICPOST_ID") + private ClinicPost clinicPost; + + public void setUser(User user){ + this.user = user; + if(!user.getClinicHearts().contains(this)){ + user.getClinicHearts().add(this); + } + } - // ClinicHeart : ClinicPost = 1 : n 매핑 - @OneToMany - @JoinColumn(name = "CLINIC_HEART_POST") - private List ClinicPosts = new ArrayList(); + public void setClinicPost(ClinicPost clinicPost){ + this.clinicPost = clinicPost; + if(!clinicPost.getClinicHearts().contains(this)){ + clinicPost.getClinicHearts().add(this); + } + } } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java new file mode 100644 index 0000000..8fb8de8 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java @@ -0,0 +1,44 @@ +package com.gdschanyang.todayfeelingbackend2.domain.hearts; + +import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; +import com.gdschanyang.todayfeelingbackend2.domain.user.User; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + +@Getter +@NoArgsConstructor +@Entity +public class FeelingHeart { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "FEELINGHEART_ID") + private Long id; + + // 2. FeelingHeart : User = n : 1 + @ManyToOne + @JoinColumn(name = "USER_ID") + private User user; + + // 3. FeelingHeart : FeelingPost = n : 1 + @ManyToOne + @JoinColumn(name = "FEELINGPOST_ID") + private FeelingPost feelingPost; + + public void setUser(User user){ + this.user = user; + if(!user.getFeelingHearts().contains(this)){ + user.getFeelingHearts().add(this); + } + } + + public void setFeelingPost(FeelingPost feelingPost){ + this.feelingPost = feelingPost; + if(!feelingPost.getFeelingHearts().contains(this)){ + feelingPost.getFeelingHearts().add(this); + } + } + +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/PostHeart.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/PostHeart.java deleted file mode 100644 index 95e34a2..0000000 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/PostHeart.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.gdschanyang.todayfeelingbackend2.domain.hearts; - -import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; -import com.gdschanyang.todayfeelingbackend2.domain.user.User; -import lombok.Getter; -import lombok.NoArgsConstructor; - -import javax.persistence.*; -import java.util.ArrayList; -import java.util.List; - -@Getter -@NoArgsConstructor -@Entity -public class PostHeart { - // 공감한 유저, 공감한 감정글 - @Id - private Long id; - - // PostHeart : User = 1 : n 매핑 - @OneToMany - @JoinColumn(name = "FEELING_HEART_USER") - private List users = new ArrayList(); - - // PostHeart : FeelingPost = 1 : n 매핑 - @OneToMany - @JoinColumn(name = "FEELING_HEART_POST") - private List feelingPosts = new ArrayList(); - -} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java index e2f48e4..84ce2db 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java @@ -1,18 +1,22 @@ package com.gdschanyang.todayfeelingbackend2.domain.posts; +import com.gdschanyang.todayfeelingbackend2.domain.hearts.ClinicHeart; import com.gdschanyang.todayfeelingbackend2.domain.user.User; import lombok.Getter; import lombok.NoArgsConstructor; import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; @Getter @NoArgsConstructor @Entity public class ClinicPost { - // 제목, 글, 글 쓴 사람 + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "CLINICPOST_ID") private Long id; @Column(length = 500, nullable = false) @@ -22,9 +26,26 @@ public class ClinicPost { @Column(columnDefinition = "TEXT", nullable = false) private String content; - // User 와 ClinicPost 1:1 매핑 - @OneToOne + // 1. ClinicPost : User = n : 1 + @ManyToOne @JoinColumn(name = "USER_ID") private User user; + // 3. ClinicPost : ClinicHeart = 1 : n + @OneToMany(mappedBy = "clinicPost") + private List clinicHearts = new ArrayList(); + + public void setUser(User user) { + this.user = user; + if (!user.getClinicPosts().contains(this)) { + user.getClinicPosts().add(this); + } + } + + public void addClinicPost(ClinicHeart clinicHeart){ + this.clinicHearts.add(clinicHeart); + if(clinicHeart.getClinicPost() != this){ + clinicHeart.setClinicPost(this); + } + } } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java index d5cafb9..58bc823 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java @@ -1,19 +1,23 @@ package com.gdschanyang.todayfeelingbackend2.domain.posts; +import com.gdschanyang.todayfeelingbackend2.domain.hearts.FeelingHeart; import com.gdschanyang.todayfeelingbackend2.domain.user.User; import lombok.Getter; import lombok.NoArgsConstructor; import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; @Getter @NoArgsConstructor @Entity public class FeelingPost { - // 감정, 글, 글 쓴 사람, 삭제 여부 + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "FEELINGPOST_ID") private Long id; @Column(nullable = false) @@ -23,13 +27,30 @@ public class FeelingPost { @Column(columnDefinition = "TEXT") private String content; - // User 와 FeelingPost 1:1 매핑 - @OneToOne + // 1. FeelingPost : User = n : 1 + @ManyToOne @JoinColumn(name = "USER_ID") private User user; + // 3. FeelingPost : FeelingHeart = 1 : n + @OneToMany(mappedBy = "feelingPost") + private List feelingHearts = new ArrayList(); + // 삭제 여부 1:삭제 0:삭제X @Column(nullable = false) - private boolean delFlag; - + private boolean delFlag; //다시 + + public void setUser(User user) { + this.user = user; + if (!user.getFeelingPosts().contains(this)) { + user.getFeelingPosts().add(this); + } + } + + public void addFeelingPost(FeelingHeart feelingHeart){ + this.feelingHearts.add(feelingHeart); + if(feelingHeart.getFeelingPost() != this){ + feelingHeart.setFeelingPost(this); + } + } } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java index 186d070..5cc2cf8 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java @@ -1,10 +1,16 @@ package com.gdschanyang.todayfeelingbackend2.domain.user; +import com.gdschanyang.todayfeelingbackend2.domain.hearts.ClinicHeart; +import com.gdschanyang.todayfeelingbackend2.domain.hearts.FeelingHeart; +import com.gdschanyang.todayfeelingbackend2.domain.posts.ClinicPost; +import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; import lombok.Getter; import lombok.NoArgsConstructor; import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; @Getter @NoArgsConstructor @@ -16,12 +22,48 @@ public class User { @Column(name = "USER_ID") private Long id; - // 본명 - @Column(name = "USER_REALNAME", nullable = false) - private String realName; + @Column(name = "USER_NAME", nullable = false) + private String name; - // 닉네임으로 대나무숲과 클릭닉 센터에서 활동 - @Column(name = "USER_NICKNAME", nullable = false) - private String nickName; + // 1. User : FeelingPost = 1 : n -> 한 유저가 여러 감정글 작성 + @OneToMany(mappedBy = "user") + private List feelingPosts = new ArrayList(); + // 1. User : ClinicPost = 1 : n -> 한 유저가 여러 클리닉글 작성 + @OneToMany(mappedBy = "user") + private List clinicPosts = new ArrayList(); + + // 2. User : FeelingHeart = 1 : n + @OneToMany(mappedBy = "user") + private List feelingHearts = new ArrayList(); + + // 2. User : ClinicHeart = 1 : n + @OneToMany(mappedBy = "user") + private List clinicHearts = new ArrayList(); + + public void addFeelingPost(FeelingPost feelingPost) { + this.feelingPosts.add(feelingPost); + if (feelingPost.getUser() != this) { + feelingPost.setUser(this); + } + } + + public void addFeelingHeart(FeelingHeart feelingHeart){ + this.feelingHearts.add(feelingHeart); + if(feelingHeart.getUser() != this){ + feelingHeart.setUser(this); + } + } + public void addClinicPost(ClinicPost clinicPost) { + this.clinicPosts.add(clinicPost); + if (clinicPost.getUser() != this) { + clinicPost.setUser(this); + } + } + public void addClinicHeart(ClinicHeart clinicHeart){ + this.clinicHearts.add((clinicHeart)); + if(clinicHeart.getUser() != this){ + clinicHeart.setUser(this); + } + } } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/PostsRepository.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/PostsRepository.java index 50e6225..3169163 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/PostsRepository.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/PostsRepository.java @@ -8,9 +8,5 @@ public interface PostsRepository extends JpaRepository { - FeelingPost save(FeelingPost posts); - Optional findById(Long id); - - List findAll(); } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/UserRepository.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/UserRepository.java index 504e7a2..fc5cf0e 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/UserRepository.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/UserRepository.java @@ -8,10 +8,5 @@ public interface UserRepository extends JpaRepository { - User save(User user); - - Optional findById(Long id); - - List findAll(); } From 5f0db4aca2e7d580deb36d8a7ae61a7758ea4b59 Mon Sep 17 00:00:00 2001 From: Suyoung Oh Date: Sun, 5 Mar 2023 15:06:35 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat=20:=20Setter=20=EC=A0=9C=EA=B1=B0,=20B?= =?UTF-8?q?aseTimeEntity=20=EC=B6=94=EA=B0=80,=20FeelingPostService=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/BaseTimeEntity.java | 22 +++++++++ .../domain/hearts/ClinicHeart.java | 18 ++----- .../domain/hearts/FeelingHeart.java | 17 ++----- .../domain/posts/ClinicPost.java | 13 ++--- .../domain/posts/FeelingPost.java | 20 ++++---- .../domain/user/User.java | 13 +---- ...sitory.java => FeelingPostRepository.java} | 5 +- .../service/FeelingPostService.java | 48 +++++++++++++++++++ 8 files changed, 96 insertions(+), 60 deletions(-) create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/domain/BaseTimeEntity.java rename src/main/java/com/gdschanyang/todayfeelingbackend2/repository/{PostsRepository.java => FeelingPostRepository.java} (60%) create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/service/FeelingPostService.java diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/BaseTimeEntity.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/BaseTimeEntity.java new file mode 100644 index 0000000..f2695d0 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/BaseTimeEntity.java @@ -0,0 +1,22 @@ +package com.gdschanyang.todayfeelingbackend2.domain; + +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.EntityListeners; +import javax.persistence.MappedSuperclass; +import java.time.LocalDateTime; + +@Getter +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public abstract class BaseTimeEntity { + + @CreatedDate + private LocalDateTime createdDate; + + @LastModifiedDate + private LocalDateTime modifiedDate; +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java index 78ce9d4..3e92f23 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java @@ -2,13 +2,13 @@ import com.gdschanyang.todayfeelingbackend2.domain.posts.ClinicPost; import com.gdschanyang.todayfeelingbackend2.domain.user.User; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import javax.persistence.*; @Getter -@NoArgsConstructor @Entity public class ClinicHeart { @@ -27,18 +27,10 @@ public class ClinicHeart { @JoinColumn(name = "CLINICPOST_ID") private ClinicPost clinicPost; - public void setUser(User user){ - this.user = user; - if(!user.getClinicHearts().contains(this)){ - user.getClinicHearts().add(this); - } - } - - public void setClinicPost(ClinicPost clinicPost){ - this.clinicPost = clinicPost; - if(!clinicPost.getClinicHearts().contains(this)){ - clinicPost.getClinicHearts().add(this); - } + @Builder + public ClinicHeart() { + this.user.addClinicHeart(this); + this.clinicPost.addClinicPost(this); } } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java index 8fb8de8..6885923 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java @@ -2,13 +2,13 @@ import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; import com.gdschanyang.todayfeelingbackend2.domain.user.User; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import javax.persistence.*; @Getter -@NoArgsConstructor @Entity public class FeelingHeart { @@ -27,18 +27,11 @@ public class FeelingHeart { @JoinColumn(name = "FEELINGPOST_ID") private FeelingPost feelingPost; - public void setUser(User user){ - this.user = user; - if(!user.getFeelingHearts().contains(this)){ - user.getFeelingHearts().add(this); - } + @Builder + public FeelingHeart() { + this.user.addFeelingHeart(this); + this.feelingPost.addFeelingPost(this); } - public void setFeelingPost(FeelingPost feelingPost){ - this.feelingPost = feelingPost; - if(!feelingPost.getFeelingHearts().contains(this)){ - feelingPost.getFeelingHearts().add(this); - } - } } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java index 84ce2db..d078995 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java @@ -2,6 +2,7 @@ import com.gdschanyang.todayfeelingbackend2.domain.hearts.ClinicHeart; import com.gdschanyang.todayfeelingbackend2.domain.user.User; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -10,7 +11,6 @@ import java.util.List; @Getter -@NoArgsConstructor @Entity public class ClinicPost { @@ -35,17 +35,12 @@ public class ClinicPost { @OneToMany(mappedBy = "clinicPost") private List clinicHearts = new ArrayList(); - public void setUser(User user) { - this.user = user; - if (!user.getClinicPosts().contains(this)) { - user.getClinicPosts().add(this); - } + @Builder + public ClinicPost() { + this.user.addClinicPost(this); } public void addClinicPost(ClinicHeart clinicHeart){ this.clinicHearts.add(clinicHeart); - if(clinicHeart.getClinicPost() != this){ - clinicHeart.setClinicPost(this); - } } } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java index 58bc823..7434c92 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java @@ -1,8 +1,10 @@ package com.gdschanyang.todayfeelingbackend2.domain.posts; +import com.gdschanyang.todayfeelingbackend2.domain.BaseTimeEntity; import com.gdschanyang.todayfeelingbackend2.domain.hearts.FeelingHeart; import com.gdschanyang.todayfeelingbackend2.domain.user.User; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -11,9 +13,8 @@ import java.util.List; @Getter -@NoArgsConstructor @Entity -public class FeelingPost { +public class FeelingPost extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -40,17 +41,16 @@ public class FeelingPost { @Column(nullable = false) private boolean delFlag; //다시 - public void setUser(User user) { - this.user = user; - if (!user.getFeelingPosts().contains(this)) { - user.getFeelingPosts().add(this); - } + @Builder + public FeelingPost() { + this.user.addFeelingPost(this); } public void addFeelingPost(FeelingHeart feelingHeart){ this.feelingHearts.add(feelingHeart); - if(feelingHeart.getFeelingPost() != this){ - feelingHeart.setFeelingPost(this); - } + } + + public void update(String content) { + this.content = content; } } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java index 5cc2cf8..a570840 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java @@ -1,6 +1,7 @@ package com.gdschanyang.todayfeelingbackend2.domain.user; +import com.gdschanyang.todayfeelingbackend2.domain.BaseTimeEntity; import com.gdschanyang.todayfeelingbackend2.domain.hearts.ClinicHeart; import com.gdschanyang.todayfeelingbackend2.domain.hearts.FeelingHeart; import com.gdschanyang.todayfeelingbackend2.domain.posts.ClinicPost; @@ -43,27 +44,15 @@ public class User { public void addFeelingPost(FeelingPost feelingPost) { this.feelingPosts.add(feelingPost); - if (feelingPost.getUser() != this) { - feelingPost.setUser(this); - } } public void addFeelingHeart(FeelingHeart feelingHeart){ this.feelingHearts.add(feelingHeart); - if(feelingHeart.getUser() != this){ - feelingHeart.setUser(this); - } } public void addClinicPost(ClinicPost clinicPost) { this.clinicPosts.add(clinicPost); - if (clinicPost.getUser() != this) { - clinicPost.setUser(this); - } } public void addClinicHeart(ClinicHeart clinicHeart){ this.clinicHearts.add((clinicHeart)); - if(clinicHeart.getUser() != this){ - clinicHeart.setUser(this); - } } } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/PostsRepository.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/FeelingPostRepository.java similarity index 60% rename from src/main/java/com/gdschanyang/todayfeelingbackend2/repository/PostsRepository.java rename to src/main/java/com/gdschanyang/todayfeelingbackend2/repository/FeelingPostRepository.java index 3169163..1773589 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/PostsRepository.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/FeelingPostRepository.java @@ -3,10 +3,7 @@ import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; import org.springframework.data.jpa.repository.JpaRepository; -import java.util.List; -import java.util.Optional; - -public interface PostsRepository extends JpaRepository { +public interface FeelingPostRepository extends JpaRepository { } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/service/FeelingPostService.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/service/FeelingPostService.java new file mode 100644 index 0000000..6c751b0 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/service/FeelingPostService.java @@ -0,0 +1,48 @@ +package com.gdschanyang.todayfeelingbackend2.service; + +import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; +import com.gdschanyang.todayfeelingbackend2.repository.FeelingPostRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + + +@Service +public class FeelingPostService { + private final FeelingPostRepository feelingPostRepository; + + public FeelingPostService(FeelingPostRepository feelingPostRepository) { + this.feelingPostRepository = feelingPostRepository; + } + + @Transactional + public void save(FeelingPost feelingPost) { + feelingPostRepository.save(feelingPost); + } + + @Transactional + public void update(Long id, FeelingPost feelingPost) { + FeelingPost feelingPostEntity = feelingPostRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id = " + id)); + + feelingPostEntity.update(feelingPost.getContent()); + } + + @Transactional + public FeelingPost findById(Long id) { + FeelingPost feelingPost = feelingPostRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id = " + id)); + + return feelingPost; + } + + @Transactional + public void delete(Long id) { + FeelingPost feelingPost = feelingPostRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id = " + id)); + + feelingPostRepository.delete(feelingPost); + } + + + +} From 13a67f96f0bb2f19f9ca720542385207447dfb77 Mon Sep 17 00:00:00 2001 From: Suyoung Oh Date: Wed, 8 Mar 2023 23:25:00 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat=20:=20Dto=20=EC=84=A4=EC=A0=95,=20Feel?= =?UTF-8?q?ingPostController=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/FeelingPostController.java | 38 +++++++++++++++++++ .../controller/IndexController.java | 20 ++++++++++ .../domain/hearts/ClinicHeart.java | 3 +- .../domain/hearts/FeelingHeart.java | 3 +- .../domain/posts/ClinicPost.java | 9 ++++- .../domain/posts/FeelingPost.java | 10 +++-- .../service/FeelingPostService.java | 25 ++++++------ .../web/dto/FeelingPostResponseDto.java | 22 +++++++++++ .../web/dto/FeelingPostSaveRequestDto.java | 33 ++++++++++++++++ .../web/dto/FeelingPostUpdateRequestDto.java | 17 +++++++++ 10 files changed, 160 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/controller/FeelingPostController.java create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/controller/IndexController.java create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostResponseDto.java create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostSaveRequestDto.java create mode 100644 src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostUpdateRequestDto.java diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/controller/FeelingPostController.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/controller/FeelingPostController.java new file mode 100644 index 0000000..e618dae --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/controller/FeelingPostController.java @@ -0,0 +1,38 @@ +package com.gdschanyang.todayfeelingbackend2.controller; + + +import com.gdschanyang.todayfeelingbackend2.service.FeelingPostService; +import com.gdschanyang.todayfeelingbackend2.web.dto.FeelingPostResponseDto; +import com.gdschanyang.todayfeelingbackend2.web.dto.FeelingPostSaveRequestDto; +import com.gdschanyang.todayfeelingbackend2.web.dto.FeelingPostUpdateRequestDto; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + + +@RequiredArgsConstructor +@Controller +public class FeelingPostController { + + private final FeelingPostService feelingPostService; + + @GetMapping("/api/feelingpost") + public void save(@RequestBody FeelingPostSaveRequestDto requestDto) { + feelingPostService.save(requestDto); + } + + @PutMapping("/api/feelingpost/{id}") + public void update(@PathVariable Long id, @RequestBody FeelingPostUpdateRequestDto requestDto) { + feelingPostService.update(id, requestDto); + } + + @GetMapping("/api/feelingpost/{id}") + public FeelingPostResponseDto findById(@PathVariable Long id) { + return feelingPostService.findById(id); + } + + @DeleteMapping("/api/feelingpost/{id}") + public void delete(@PathVariable Long id) { + feelingPostService.delete(id); + } +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/controller/IndexController.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/controller/IndexController.java new file mode 100644 index 0000000..7bdcd83 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/controller/IndexController.java @@ -0,0 +1,20 @@ +package com.gdschanyang.todayfeelingbackend2.controller; + +import com.gdschanyang.todayfeelingbackend2.service.FeelingPostService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + + +@RequiredArgsConstructor +@Controller +public class IndexController { + + private final FeelingPostService feelingPostService; + + @GetMapping("/") + public String home() { + return "home"; + } + +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java index 3e92f23..2de98a5 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java @@ -1,5 +1,6 @@ package com.gdschanyang.todayfeelingbackend2.domain.hearts; +import com.gdschanyang.todayfeelingbackend2.domain.BaseTimeEntity; import com.gdschanyang.todayfeelingbackend2.domain.posts.ClinicPost; import com.gdschanyang.todayfeelingbackend2.domain.user.User; import lombok.Builder; @@ -10,7 +11,7 @@ @Getter @Entity -public class ClinicHeart { +public class ClinicHeart extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java index 6885923..ed3ea08 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java @@ -1,5 +1,6 @@ package com.gdschanyang.todayfeelingbackend2.domain.hearts; +import com.gdschanyang.todayfeelingbackend2.domain.BaseTimeEntity; import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; import com.gdschanyang.todayfeelingbackend2.domain.user.User; import lombok.Builder; @@ -10,7 +11,7 @@ @Getter @Entity -public class FeelingHeart { +public class FeelingHeart extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java index d078995..269f701 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java @@ -1,5 +1,6 @@ package com.gdschanyang.todayfeelingbackend2.domain.posts; +import com.gdschanyang.todayfeelingbackend2.domain.BaseTimeEntity; import com.gdschanyang.todayfeelingbackend2.domain.hearts.ClinicHeart; import com.gdschanyang.todayfeelingbackend2.domain.user.User; import lombok.Builder; @@ -11,8 +12,9 @@ import java.util.List; @Getter +@NoArgsConstructor @Entity -public class ClinicPost { +public class ClinicPost extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -36,7 +38,10 @@ public class ClinicPost { private List clinicHearts = new ArrayList(); @Builder - public ClinicPost() { + public ClinicPost(Long id, String title, String content) { + this.id = id; + this.title = title; + this.content = content; this.user.addClinicPost(this); } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java index 7434c92..5872b01 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java @@ -13,6 +13,7 @@ import java.util.List; @Getter +@NoArgsConstructor @Entity public class FeelingPost extends BaseTimeEntity { @@ -37,12 +38,15 @@ public class FeelingPost extends BaseTimeEntity { @OneToMany(mappedBy = "feelingPost") private List feelingHearts = new ArrayList(); - // 삭제 여부 1:삭제 0:삭제X + // 삭제 여부 T:삭제 F:삭제X @Column(nullable = false) - private boolean delFlag; //다시 + private char delFlag; @Builder - public FeelingPost() { + public FeelingPost(Long id, Feeling feeling, String content) { + this.id = id; + this.feeling = feeling; + this.content = content; this.user.addFeelingPost(this); } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/service/FeelingPostService.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/service/FeelingPostService.java index 6c751b0..8a80684 100644 --- a/src/main/java/com/gdschanyang/todayfeelingbackend2/service/FeelingPostService.java +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/service/FeelingPostService.java @@ -2,37 +2,37 @@ import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; import com.gdschanyang.todayfeelingbackend2.repository.FeelingPostRepository; +import com.gdschanyang.todayfeelingbackend2.web.dto.FeelingPostResponseDto; +import com.gdschanyang.todayfeelingbackend2.web.dto.FeelingPostSaveRequestDto; +import com.gdschanyang.todayfeelingbackend2.web.dto.FeelingPostUpdateRequestDto; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - +@RequiredArgsConstructor @Service public class FeelingPostService { private final FeelingPostRepository feelingPostRepository; - public FeelingPostService(FeelingPostRepository feelingPostRepository) { - this.feelingPostRepository = feelingPostRepository; - } - @Transactional - public void save(FeelingPost feelingPost) { - feelingPostRepository.save(feelingPost); + public void save(FeelingPostSaveRequestDto requestDto) { + feelingPostRepository.save(requestDto.toEntity()); } @Transactional - public void update(Long id, FeelingPost feelingPost) { + public void update(Long id, FeelingPostUpdateRequestDto requestDto) { FeelingPost feelingPostEntity = feelingPostRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id = " + id)); - feelingPostEntity.update(feelingPost.getContent()); + feelingPostEntity.update(requestDto.getContent()); } @Transactional - public FeelingPost findById(Long id) { - FeelingPost feelingPost = feelingPostRepository.findById(id) + public FeelingPostResponseDto findById(Long id) { + FeelingPost entity = feelingPostRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id = " + id)); - return feelingPost; + return new FeelingPostResponseDto(entity); } @Transactional @@ -44,5 +44,4 @@ public void delete(Long id) { } - } diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostResponseDto.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostResponseDto.java new file mode 100644 index 0000000..8dd2ae4 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostResponseDto.java @@ -0,0 +1,22 @@ +package com.gdschanyang.todayfeelingbackend2.web.dto; + + +import com.gdschanyang.todayfeelingbackend2.domain.posts.Feeling; +import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; +import lombok.Getter; + +@Getter +public class FeelingPostResponseDto { + + private Long id; + + private Feeling feeling; + + private String content; + + public FeelingPostResponseDto(FeelingPost entity) { + this.id = entity.getId(); + this.feeling = entity.getFeeling(); + this.content = entity.getContent(); + } +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostSaveRequestDto.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostSaveRequestDto.java new file mode 100644 index 0000000..d71f5d6 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostSaveRequestDto.java @@ -0,0 +1,33 @@ +package com.gdschanyang.todayfeelingbackend2.web.dto; + +import com.gdschanyang.todayfeelingbackend2.domain.posts.Feeling; +import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + + +@Getter +@NoArgsConstructor +public class FeelingPostSaveRequestDto { + + private Long id; + private Feeling feeling; + private String content; + + @Builder + public FeelingPostSaveRequestDto(Long id, Feeling feeling, String content) { + this.id = id; + this.feeling = feeling; + this.content = content; + } + + public FeelingPost toEntity() { + return FeelingPost.builder() + .id(id) + .feeling(feeling) + .content(content) + .build(); + } + +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostUpdateRequestDto.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostUpdateRequestDto.java new file mode 100644 index 0000000..33a16e6 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/web/dto/FeelingPostUpdateRequestDto.java @@ -0,0 +1,17 @@ +package com.gdschanyang.todayfeelingbackend2.web.dto; + + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class FeelingPostUpdateRequestDto { + private String content; + + @Builder + public FeelingPostUpdateRequestDto(String content) { + this.content = content; + } +}