Skip to content

Commit

Permalink
Refactor: domain별 state 추가 (#52)
Browse files Browse the repository at this point in the history
* Refactor: 각 domain state column 추가
post, member state 추가
* Refactor: 저장 및 조회 state 확인 절차 추가
생성시 state A 추가, entity에 Where annotation 사용하여 컬럼 조회시 state 확인하여 조회하게끔 수정
  • Loading branch information
wcorn committed Dec 26, 2023
1 parent 9499805 commit 9507c22
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
13 changes: 11 additions & 2 deletions src/main/java/com/fubao/project/domain/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.fubao.project.global.common.constant.MemberRole;
import com.fubao.project.global.common.constant.OAuthProvider;
import com.fubao.project.global.common.constant.State;
import com.fubao.project.global.common.entity.BaseEntity;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.Where;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -14,6 +16,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "member")
@Where(clause = "state = 'A'")
public class Member extends BaseEntity {

@Id
Expand All @@ -31,26 +34,32 @@ public class Member extends BaseEntity {
@Enumerated(EnumType.STRING)
private MemberRole memberRole;

@Column(name = "state")
@Enumerated(EnumType.STRING)
private State state;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL)
List<Post> postList = new ArrayList<>();

public static Member of(
UUID id, OAuthProvider oAuthProvider, String providerId, MemberRole memberRole
UUID id, OAuthProvider oAuthProvider, String providerId, MemberRole memberRole, State state
) {
return Member.builder()
.id(id)
.memberRole(memberRole)
.providerId(providerId)
.oAuthProvider(oAuthProvider)
.state(state)
.build();
}

@Builder
public Member(UUID id, OAuthProvider oAuthProvider, String providerId, MemberRole memberRole, List<Post> postList) {
public Member(UUID id, OAuthProvider oAuthProvider, String providerId, MemberRole memberRole, List<Post> postList, State state) {
this.id = id;
this.oauthProvider = oAuthProvider;
this.providerId = providerId;
this.memberRole = memberRole;
this.state = state;
if (postList.isEmpty()) {
this.postList = postList;
} else {
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/fubao/project/domain/entity/Post.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.fubao.project.domain.entity;

import com.fubao.project.global.common.entity.BaseEntity;
import com.fubao.project.global.common.constant.State;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.Where;

import static jakarta.persistence.GenerationType.IDENTITY;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "post")
@Where(clause = "state = 'A'")
public class Post extends BaseEntity {
@Id()
@GeneratedValue(strategy = IDENTITY)
Expand All @@ -26,20 +29,26 @@ public class Post extends BaseEntity {
@Column(name = "content")
private String content;

@Column(name = "state")
@Enumerated(EnumType.STRING)
private State state;

public static Post of(
Long id, Member member, String imageUrl, String content
Long id, Member member, String imageUrl, String content, State state
) {
return Post.builder()
.id(id)
.member(member)
.imageUrl(imageUrl)
.content(content)
.state(state)
.build();
}

@Builder
public Post(Long id, Member member, String imageUrl, String content) {
public Post(Long id, Member member, String imageUrl, String content, State state) {
this.id = id;
this.state = state;
this.member = member;
this.imageUrl = imageUrl;
this.content = content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.fubao.project.domain.repository;

import com.fubao.project.domain.entity.Post;
import com.fubao.project.global.common.constant.State;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostRepository extends JpaRepository<Post, Long> {

Page<Post> findAllByState(Pageable pageable, State state);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fubao.project.domain.repository.MemberRepository;
import com.fubao.project.domain.service.oauth.RequestOAuthInfoService;
import com.fubao.project.global.common.constant.MemberRole;
import com.fubao.project.global.common.constant.State;
import com.fubao.project.global.common.exception.ResponseCode;
import com.fubao.project.global.common.exception.CustomException;
import com.fubao.project.global.common.oauth.OAuthInfoResponse;
Expand Down Expand Up @@ -61,6 +62,7 @@ private Member signUp(OAuthInfoResponse oAuthInfoResponse) {
.oAuthProvider(oAuthInfoResponse.getOAuthProvider())
.providerId(oAuthInfoResponse.getProviderId())
.memberRole(MemberRole.MEMBER)
.state(State.A)
.build();
return memberRepository.save(member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fubao.project.domain.entity.Post;
import com.fubao.project.domain.repository.MemberRepository;
import com.fubao.project.domain.repository.PostRepository;
import com.fubao.project.global.common.constant.State;
import com.fubao.project.global.common.exception.ResponseCode;
import com.fubao.project.global.common.exception.CustomException;
import com.fubao.project.global.util.S3Util;
Expand Down Expand Up @@ -42,6 +43,7 @@ public PostWriteResponse post(MultipartFile images, PostWriteRequest postWriteRe
post = Post.builder()
.content(postWriteRequest.getContent())
.member(member)
.state(State.A)
.imageUrl(imageUrl).build();

save(post);
Expand Down Expand Up @@ -128,10 +130,6 @@ private Post findPostById(Long postId) {
return postRepository.findById(postId).orElseThrow(() -> new CustomException(ResponseCode.POST_NOT_FOUND));
}

private boolean existPost(Long postId) {
return postRepository.existsById(postId);
}

private String uploadS3Image(MultipartFile images) {
return s3Util.uploadFileToS3(DIR, images);
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/fubao/project/global/common/constant/State.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fubao.project.global.common.constant;

import lombok.Getter;

@Getter
public enum State {
A("ACTIVE"),
D("DELETE");
private final String name;

State(String name) {
this.name = name;
}
}

0 comments on commit 9507c22

Please sign in to comment.