Skip to content

Commit

Permalink
[Refactor]: 커뮤니티 게시글 생성 API 코드 리팩토링 (#560)
Browse files Browse the repository at this point in the history
* [Refactor]: 커뮤니티 게시글 생성 API 리팩토링

PostSaveRequest로 받는 데이터 validation 추가

Related to: #559

* [Refactor]: CommunityPostRepository 파일 이동

repository/community -> community/repository 폴더로 이동

Related to: #559

* [Refactor]: CommunityPost.java 수정 및 패키지 이동

community domain 패키지로 이동
validation 필요한 필드 추가

Related to: #559

* [Refactor]: AnonymousPostProfileRepository 파일 이동

Related to: #559

* [Refactor]: AnonymousProfileImageRepository 패키지 변경

Related to: #559

* [Refactor]: AnonymousProfileImageRepository 패키지 변경

Related to: #559

* [Refactor]: repository 파일 이동 및 querydsl 메서드 구현

Related to: #559

* [Refactor]: request body validation 추가

Related to: #559

* [Refactor]: 리팩토링 관련 파일 이동

import문 변경있음

Related to: #559

* [Refactor]: 커뮤니티 게시글 생성 로직 코드 분리

Service 계층에 밀집된 비즈니스 로직 분리
하위 도메인 정의 및 코드 분리

Related to: #559

* [Refactor]: 커뮤니티 게시글 생성 단위 테스트 코드 생성

Related to: #559

* [Refactor]: 도메인 정의에 맞게 패키지 변경

Related to: #559

* [Refactor]: val 사용 지양

CommunityController.java에 있는 val 삭제

Related to: #559

* [Refactor]: 사용되지 않는 파일 삭제

Related to: #559

* [Refactor]: Qclass 경로 변경

Related to: #559

* [Refactor]: 익명 프로필 이미지 Map 초기화 확인 테스트 코드 작성

Related to: #559

* [Refactor]: 익명 프로필 이미지 1~5 사이의 id값 확인 테스트 코드

Related to: #559

* [Refactor]: 익명 프로필 이미지 1~5 사이의 id값 확인 테스트 코드

Related to: #559

* [Refactor]: 익명 프로필 이미지 랜덤 생성 테스트 코드

Related to: #559

* [Refactor]: 익명 프로필 닉네임 랜덤 생성 테스트 코드 작성

Related to: #559

* [Refactor]: 게시물 익명으로 생성 시 익명 프로필 생성 테스트 코드

Related to: #559

* [Refactor]: PostSaveRequest.java @Valid message 필드 추가

Related to: #559

* [Refactor]: Domain 객체 생성방식 통일

Related to: #559

* [Refactor]: 익명글에 따른 분기 처리 로직 service 계층에서 담당

Related to: #559

* [Refactor]: 익명 프로필 최신 데이터 조회 limit 상수 선언

Related to: #559

* [Refactor]: 익명 프로필 최신 데이터 조회 limit 상수 선언

Related to: #559

* [Refactor]: 서비스 레이어에 @transactional 어노테이션 추가

Related to: #559

* [Refactor]: 테스트 코드 더미데이터 생성 함수 분리

Related to: #559
  • Loading branch information
dev-Crayon authored Dec 16, 2024
1 parent 8529719 commit c5da3f0
Show file tree
Hide file tree
Showing 42 changed files with 765 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.sopt.makers.internal.common.InfiniteScrollUtil;
import org.sopt.makers.internal.community.controller.dto.request.PostSaveRequest;
import org.sopt.makers.internal.community.service.CommunityPostService;
import org.sopt.makers.internal.domain.InternalMemberDetails;
import org.sopt.makers.internal.dto.community.*;
Expand Down Expand Up @@ -97,10 +98,12 @@ public ResponseEntity<Map<String, Boolean>> upPostHit(
@PostMapping("/posts")
public ResponseEntity<PostSaveResponse> createPost(
@Parameter(hidden = true) @AuthenticationPrincipal InternalMemberDetails memberDetails,
@RequestBody PostSaveRequest request
@RequestBody @Valid PostSaveRequest request
) {
val response = communityPostService.createPost(memberDetails.getId(), request);
return ResponseEntity.status(HttpStatus.CREATED).body(response);

return ResponseEntity.status(HttpStatus.CREATED).body(
communityPostService.createPost(memberDetails.getId(), request)
);
}

@Operation(summary = "커뮤니티 글 수정")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.sopt.makers.internal.community.controller.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

public record PostSaveRequest(
@Schema(required = true)
@NotNull(message = "카테고리 id는 필수 입력값입니다.")
Long categoryId,

String title,

@Schema(required = true)
@NotBlank(message = "게시글 본문은 공백일 수 없습니다.")
String content,

@Schema(required = true)
@NotNull(message = "질문글 여부 필드는 필수 입력값입니다.")
Boolean isQuestion,

@Schema(required = true)
@NotNull(message = "익명글 여부 필드는 필수 입력값입니다.")
Boolean isBlindWriter,

@Schema(required = true)
@NotNull(message = "이미지 필드는 필수 필드입니다.")
String[] images
) {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.sopt.makers.internal.domain.community;
package org.sopt.makers.internal.community.domain;

import lombok.*;
import org.hibernate.annotations.Type;
import org.sopt.makers.internal.domain.Member;
import org.sopt.makers.internal.domain.common.AuditingTimeEntity;
import org.sopt.makers.internal.domain.community.CommunityComment;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -20,49 +20,46 @@ public class CommunityPost extends AuditingTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Long id;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "writer_id")
private Member member;

@Column
@Column(nullable = false)
private Long categoryId;

@Column
private String title;

@Column(columnDefinition = "TEXT")
@Column(columnDefinition = "TEXT", nullable = false)
private String content;

@Builder.Default
@Column
@Column(nullable = false)
private Integer hits = 0;

@Type(type = "string-array")
@Column(name = "images", columnDefinition = "text[]")
private String[] images;

@Builder.Default
@Column
private Boolean isQuestion = false;
@Column(nullable = false)
private Boolean isQuestion;

@Builder.Default
@Column
private Boolean isBlindWriter = false;
@Column(nullable = false)
private Boolean isBlindWriter;

@Builder.Default
@Column
@Column(nullable = false)
private Boolean isReported = false;

@Builder.Default
@Column
@Column(nullable = false)
private Boolean isHot = false;

@Builder.Default
@OneToMany(
cascade = CascadeType.ALL,
cascade = CascadeType.REMOVE,
orphanRemoval = true
)
@JoinColumn(name = "postId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import lombok.NoArgsConstructor;
import org.sopt.makers.internal.domain.Member;
import org.sopt.makers.internal.domain.common.AuditingTimeEntity;
import org.sopt.makers.internal.domain.community.CommunityPost;

import javax.persistence.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package org.sopt.makers.internal.domain.community;
package org.sopt.makers.internal.community.domain.anonymous;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class AnonymousNickname {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "anonymous_nickname_id")
private Long id;

@Column(nullable = false)
String nickname;

@Builder
private AnonymousNickname(Long id, String nickname) {
this.id = id;
this.nickname = nickname;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sopt.makers.internal.domain.community;
package org.sopt.makers.internal.community.domain.anonymous;

import javax.persistence.Column;
import javax.persistence.ConstraintMode;
Expand All @@ -11,7 +11,7 @@
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import org.sopt.makers.internal.community.domain.AnonymousProfileImage;
import org.sopt.makers.internal.community.domain.CommunityPost;
import org.sopt.makers.internal.domain.Member;
import org.sopt.makers.internal.domain.common.AuditingTimeEntity;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.sopt.makers.internal.community.domain.anonymous;

import lombok.*;

import javax.persistence.*;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class AnonymousProfileImage {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String imageUrl;

@Builder
private AnonymousProfileImage(Long id, String imageUrl) {
this.id = id;
this.imageUrl = imageUrl;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.sopt.makers.internal.repository.community;
package org.sopt.makers.internal.community.repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import org.sopt.makers.internal.community.repository.CommunityPostRepositoryCustom;
import org.sopt.makers.internal.domain.community.CommunityPost;
import org.sopt.makers.internal.community.domain.CommunityPost;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CommunityPostRepository extends JpaRepository<CommunityPost, Long>, CommunityPostRepositoryCustom {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.sopt.makers.internal.community.domain.QCommunityPost;
import org.sopt.makers.internal.community.domain.category.QCategory;
import org.sopt.makers.internal.domain.community.QCommunityPost;
import org.sopt.makers.internal.dto.community.PostCategoryDao;
import org.sopt.makers.internal.dto.community.QPostCategoryDao;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.sopt.makers.internal.repository.community;
package org.sopt.makers.internal.community.repository.anonymous;

import java.util.List;

import org.sopt.makers.internal.domain.community.AnonymousNickname;
import org.sopt.makers.internal.community.domain.anonymous.AnonymousNickname;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
package org.sopt.makers.internal.repository.community;
package org.sopt.makers.internal.community.repository.anonymous;

import java.util.List;
import java.util.Optional;

import org.sopt.makers.internal.domain.Member;
import org.sopt.makers.internal.domain.community.AnonymousPostProfile;
import org.sopt.makers.internal.domain.community.CommunityPost;
import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile;
import org.sopt.makers.internal.community.domain.CommunityPost;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AnonymousPostProfileRepository extends JpaRepository<AnonymousPostProfile, Long> {
public interface AnonymousPostProfileRepository extends JpaRepository<AnonymousPostProfile, Long>, AnonymousPostProfileRepositoryCustom {

// CREATE

// READ
Optional<AnonymousPostProfile> findAnonymousPostProfileByMemberIdAndCommunityPostId(Long memberId, Long communityPostId);

Optional<AnonymousPostProfile> findByMemberAndCommunityPost(Member member, CommunityPost post);

List<AnonymousPostProfile> findTop4ByOrderByCreatedAtDesc();

List<AnonymousPostProfile> findTop50ByOrderByCreatedAtDesc();

// UPDATE

//DELETE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.sopt.makers.internal.community.repository.anonymous;

import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile;

import java.util.List;

public interface AnonymousPostProfileRepositoryCustom {

List<AnonymousPostProfile> findTopByOrderByIdDescWithLimit(int limit);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.sopt.makers.internal.community.repository.anonymous;

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile;
import org.sopt.makers.internal.community.domain.anonymous.QAnonymousPostProfile;

import java.util.List;

@RequiredArgsConstructor
public class AnonymousPostProfileRepositoryCustomImpl implements AnonymousPostProfileRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public List<AnonymousPostProfile> findTopByOrderByIdDescWithLimit(int limit) {

QAnonymousPostProfile anonymousPostProfile = QAnonymousPostProfile.anonymousPostProfile;

return queryFactory
.selectFrom(anonymousPostProfile)
.orderBy(anonymousPostProfile.createdAt.desc())
.limit(limit)
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.sopt.makers.internal.community.repository;
package org.sopt.makers.internal.community.repository.anonymous;

import org.sopt.makers.internal.community.domain.AnonymousProfileImage;
import org.sopt.makers.internal.community.domain.anonymous.AnonymousProfileImage;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand Down

This file was deleted.

Loading

0 comments on commit c5da3f0

Please sign in to comment.