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

feat: 삭제여부 Converter이용, Clinic파트 Service, Controller - Dto 설정 -#12 #13

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.gdschanyang.todayfeelingbackend2;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter
public class BooleanToTFConverter implements AttributeConverter<Boolean, String> {
@Override
public String convertToDatabaseColumn(Boolean attribute) {
if (attribute != null && attribute) return "T";
return "F";
}

@Override
public Boolean convertToEntityAttribute(String dbData) {
return "T".equalsIgnoreCase(dbData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@SpringBootApplication
public class TodayFeelingBackend2Application {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.gdschanyang.todayfeelingbackend2.controller;


import com.gdschanyang.todayfeelingbackend2.service.ClinicPostService;
import com.gdschanyang.todayfeelingbackend2.web.dto.clinic.ClinicPostResponseDto;
import com.gdschanyang.todayfeelingbackend2.web.dto.clinic.ClinicPostSaveRequestDto;
import com.gdschanyang.todayfeelingbackend2.web.dto.clinic.ClinicPostUpdateRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@RequiredArgsConstructor
@RestController
public class ClinicPostController {

private final ClinicPostService clinicPostService;

@PostMapping("/api/clinicpost")
public void save(@RequestBody ClinicPostSaveRequestDto requestDto) {
clinicPostService.save(requestDto);
}

@PutMapping("/api/clinicpost/{id}")
public void update(@PathVariable Long id, @RequestBody ClinicPostUpdateRequestDto requestDto) {
clinicPostService.update(id, requestDto);
}

@GetMapping("/api/clinicpost/{id}")
public ClinicPostResponseDto findById(@PathVariable Long id) {
return clinicPostService.findById(id);
}

@DeleteMapping("/api/clinicpost/{id}")
public void delete(@PathVariable Long id) {
clinicPostService.delete(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.gdschanyang.todayfeelingbackend2.controller;


import com.gdschanyang.todayfeelingbackend2.service.FeelingPostService;
import com.gdschanyang.todayfeelingbackend2.web.dto.feeling.FeelingPostResponseDto;
import com.gdschanyang.todayfeelingbackend2.web.dto.feeling.FeelingPostSaveRequestDto;
import com.gdschanyang.todayfeelingbackend2.web.dto.feeling.FeelingPostUpdateRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@RequiredArgsConstructor
@RestController
public class FeelingPostController {

private final FeelingPostService feelingPostService;

@PostMapping("/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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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
@NoArgsConstructor
@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(Long id) {
this.id = id;
this.user.addClinicHeart(this);
this.clinicPost.addClinicHeart(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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
@NoArgsConstructor
@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(Long id) {
this.id = id;
this.user.addFeelingHeart(this);
this.feelingPost.addFeelingHeart(this);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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<ClinicHeart> 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 addClinicHeart(ClinicHeart clinicHeart){
this.clinicHearts.add(clinicHeart);
}

public void update(String title, String content) {
this.title = title;
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gdschanyang.todayfeelingbackend2.domain.posts;


public enum Feeling {
POSITIVE,
NEGATIVE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.gdschanyang.todayfeelingbackend2.domain.posts;


import com.gdschanyang.todayfeelingbackend2.BooleanToTFConverter;
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<FeelingHeart> feelingHearts = new ArrayList<>();

// 삭제 여부 T:삭제 F:삭제X
@Column(nullable = false)
@Convert(converter = BooleanToTFConverter.class)
private boolean 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 update(String content) {
this.content = content;
}

public void addFeelingHeart(FeelingHeart feelingHeart) {
this.feelingHearts.add(feelingHeart);
}
}
Original file line number Diff line number Diff line change
@@ -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<FeelingPost> feelingPosts = new ArrayList<>();

// 1. User : ClinicPost = 1 : n -> 한 유저가 여러 클리닉글 작성
@OneToMany(mappedBy = "user")
private List<ClinicPost> clinicPosts = new ArrayList<>();

// 2. User : FeelingHeart = 1 : n
@OneToMany(mappedBy = "user")
private List<FeelingHeart> feelingHearts = new ArrayList<>();

// 2. User : ClinicHeart = 1 : n
@OneToMany(mappedBy = "user")
private List<ClinicHeart> 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));
}
}
Loading