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

[Refactor]: 커뮤니티 게시글 생성 API 코드 리팩토링 #560

Merged
merged 28 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9940a6f
[Refactor]: 커뮤니티 게시글 생성 API 리팩토링
dev-Crayon Nov 27, 2024
8c01153
[Refactor]: CommunityPostRepository 파일 이동
dev-Crayon Nov 27, 2024
19045a1
[Refactor]: CommunityPost.java 수정 및 패키지 이동
dev-Crayon Nov 27, 2024
29e84f4
[Refactor]: AnonymousPostProfileRepository 파일 이동
dev-Crayon Nov 27, 2024
023e5a7
[Refactor]: AnonymousProfileImageRepository 패키지 변경
dev-Crayon Nov 27, 2024
e56b2aa
[Refactor]: AnonymousProfileImageRepository 패키지 변경
dev-Crayon Nov 27, 2024
97b719a
[Refactor]: repository 파일 이동 및 querydsl 메서드 구현
dev-Crayon Nov 27, 2024
ce89e5a
[Refactor]: request body validation 추가
dev-Crayon Nov 27, 2024
573a9ce
[Refactor]: 리팩토링 관련 파일 이동
dev-Crayon Nov 27, 2024
ca94af5
[Refactor]: 커뮤니티 게시글 생성 로직 코드 분리
dev-Crayon Nov 27, 2024
4a1aac2
[Refactor]: 커뮤니티 게시글 생성 단위 테스트 코드 생성
dev-Crayon Nov 27, 2024
4559e1c
[Refactor]: 도메인 정의에 맞게 패키지 변경
dev-Crayon Nov 28, 2024
7904d59
[Refactor]: val 사용 지양
dev-Crayon Nov 28, 2024
4d67c92
[Refactor]: 사용되지 않는 파일 삭제
dev-Crayon Nov 28, 2024
450ea1e
[Refactor]: Qclass 경로 변경
dev-Crayon Nov 28, 2024
3d2c71e
[Refactor]: 익명 프로필 이미지 Map 초기화 확인 테스트 코드 작성
dev-Crayon Nov 30, 2024
7aafe6d
[Refactor]: 익명 프로필 이미지 1~5 사이의 id값 확인 테스트 코드
dev-Crayon Nov 30, 2024
0b3449f
[Refactor]: 익명 프로필 이미지 1~5 사이의 id값 확인 테스트 코드
dev-Crayon Nov 30, 2024
491c716
[Refactor]: 익명 프로필 이미지 랜덤 생성 테스트 코드
dev-Crayon Nov 30, 2024
cab2bd9
[Refactor]: 익명 프로필 닉네임 랜덤 생성 테스트 코드 작성
dev-Crayon Nov 30, 2024
3dae0af
[Refactor]: 게시물 익명으로 생성 시 익명 프로필 생성 테스트 코드
dev-Crayon Nov 30, 2024
6e527e1
[Refactor]: PostSaveRequest.java @Valid message 필드 추가
dev-Crayon Dec 16, 2024
e22f3d8
[Refactor]: Domain 객체 생성방식 통일
dev-Crayon Dec 16, 2024
4f9c01b
[Refactor]: 익명글에 따른 분기 처리 로직 service 계층에서 담당
dev-Crayon Dec 16, 2024
a6d9eeb
[Refactor]: 익명 프로필 최신 데이터 조회 limit 상수 선언
dev-Crayon Dec 16, 2024
bfc1908
[Refactor]: 익명 프로필 최신 데이터 조회 limit 상수 선언
dev-Crayon Dec 16, 2024
84f4195
[Refactor]: 서비스 레이어에 @Transactional 어노테이션 추가
dev-Crayon Dec 16, 2024
e2e317f
[Refactor]: 테스트 코드 더미데이터 생성 함수 분리
dev-Crayon Dec 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요거 @Valid가 앞쪽에 와야 제대로 동작할 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 알기로는 @Valid 위치는 상관없이 동작하는 것으로 알고 있습니다.
해당 어노테이션 테스트도 해본 상태입니다!

) {
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spring Validator 쓸 때 메시지도 같이 작성해주면 좋을 것 같아요!!

fyi. 다른 도메인의 Request Dto

@NotNull(message = "수신자 정보는 필수 입력 값입니다.")
Long receiverId,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 의견 감사합니다! 바로 반영하겠습니다.

@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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

param으로 받게 하는 거 너무 좋네요 !! 👍

}
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요기 메서드명 수정이 필요해보입니다. order by 기준이 createdAt인데, 메서드에서는 id로 되어 있어서요!


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
Loading