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/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 new file mode 100644 index 0000000..2de98a5 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/ClinicHeart.java @@ -0,0 +1,37 @@ +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; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + +@Getter +@Entity +public class ClinicHeart extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "CLINICHEART_ID") + private Long id; + + // 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; + + @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 new file mode 100644 index 0000000..ed3ea08 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/hearts/FeelingHeart.java @@ -0,0 +1,38 @@ +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; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + +@Getter +@Entity +public class FeelingHeart extends BaseTimeEntity { + + @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; + + @Builder + public FeelingHeart() { + this.user.addFeelingHeart(this); + this.feelingPost.addFeelingPost(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 new file mode 100644 index 0000000..269f701 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/ClinicPost.java @@ -0,0 +1,51 @@ +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; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +@Getter +@NoArgsConstructor +@Entity +public class ClinicPost extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "CLINICPOST_ID") + private Long id; + + @Column(length = 500, nullable = false) + private String title; + + // 글 작성은 필수 + @Column(columnDefinition = "TEXT", nullable = false) + private String content; + + // 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(); + + @Builder + public ClinicPost(Long id, String title, String content) { + this.id = id; + this.title = title; + this.content = content; + this.user.addClinicPost(this); + } + + public void addClinicPost(ClinicHeart clinicHeart){ + this.clinicHearts.add(clinicHeart); + } +} 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/FeelingPost.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java new file mode 100644 index 0000000..5872b01 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/posts/FeelingPost.java @@ -0,0 +1,60 @@ +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; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +@Getter +@NoArgsConstructor +@Entity +public class FeelingPost extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "FEELINGPOST_ID") + private Long id; + + @Column(nullable = false) + private Feeling feeling; + + // 글 작성은 선택적 + @Column(columnDefinition = "TEXT") + private String content; + + // 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(); + + // 삭제 여부 T:삭제 F:삭제X + @Column(nullable = false) + private char delFlag; + + @Builder + public FeelingPost(Long id, Feeling feeling, String content) { + this.id = id; + this.feeling = feeling; + this.content = content; + this.user.addFeelingPost(this); + } + + public void addFeelingPost(FeelingHeart feelingHeart){ + this.feelingHearts.add(feelingHeart); + } + + 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 new file mode 100644 index 0000000..a570840 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/domain/user/User.java @@ -0,0 +1,58 @@ +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; +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 +@Entity +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "USER_ID") + private Long id; + + @Column(name = "USER_NAME", nullable = false) + private String name; + + // 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); + } + + public void addFeelingHeart(FeelingHeart feelingHeart){ + this.feelingHearts.add(feelingHeart); + } + public void addClinicPost(ClinicPost clinicPost) { + this.clinicPosts.add(clinicPost); + } + public void addClinicHeart(ClinicHeart clinicHeart){ + this.clinicHearts.add((clinicHeart)); + } +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/FeelingPostRepository.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/FeelingPostRepository.java new file mode 100644 index 0000000..1773589 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/FeelingPostRepository.java @@ -0,0 +1,9 @@ +package com.gdschanyang.todayfeelingbackend2.repository; + +import com.gdschanyang.todayfeelingbackend2.domain.posts.FeelingPost; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface FeelingPostRepository extends JpaRepository { + + +} diff --git a/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/UserRepository.java b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/UserRepository.java new file mode 100644 index 0000000..fc5cf0e --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/repository/UserRepository.java @@ -0,0 +1,12 @@ +package com.gdschanyang.todayfeelingbackend2.repository; + +import com.gdschanyang.todayfeelingbackend2.domain.user.User; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.Optional; + +public interface UserRepository 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..8a80684 --- /dev/null +++ b/src/main/java/com/gdschanyang/todayfeelingbackend2/service/FeelingPostService.java @@ -0,0 +1,47 @@ +package com.gdschanyang.todayfeelingbackend2.service; + +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; + + @Transactional + public void save(FeelingPostSaveRequestDto requestDto) { + feelingPostRepository.save(requestDto.toEntity()); + } + + @Transactional + public void update(Long id, FeelingPostUpdateRequestDto requestDto) { + FeelingPost feelingPostEntity = feelingPostRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id = " + id)); + + feelingPostEntity.update(requestDto.getContent()); + } + + @Transactional + public FeelingPostResponseDto findById(Long id) { + FeelingPost entity = feelingPostRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id = " + id)); + + return new FeelingPostResponseDto(entity); + } + + @Transactional + public void delete(Long id) { + FeelingPost feelingPost = feelingPostRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id = " + id)); + + feelingPostRepository.delete(feelingPost); + } + + +} 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; + } +}