Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -6,7 +6,7 @@

public record PartPostDTO(
@NotNull Part part,
Copy link
Collaborator

Choose a reason for hiding this comment

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

part가 ALL일 때는 어떻게 검색되나요?

Copy link
Member Author

Choose a reason for hiding this comment

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

위 강혁님 코멘트 내용이랑 비슷한 내용인거같은데, ALL 일때는 전체 파트에 해당하는 내용이 반환이됩니다

BE, FE, PM, D

위 4개의 파트가 모두 포함됨

Category category,
@NotNull Category category,
Integer cardinalNumber,
Integer week,
String studyName
Comment on lines 7 to 12
Copy link
Collaborator

Choose a reason for hiding this comment

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

검색 용도로만 쓰이는걸까요?

Copy link
Member Author

Choose a reason for hiding this comment

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

검색용도라는게 어떤거 말씀하시는걸까요 ? 조회할때 request에서 사용되는 DTO입니다

분리한 이유를 물으신게 맞다면, @ModelAttribute 어노테이션에서 사용되는 dto라서 따로 분리해주었습니다

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,17 @@ public record ResponseEducationAll(
Long id,
String name,
List<Part> parts,
int week,
Position position,
Role role,
String title,
String content,
LocalDateTime time,
Integer commentCount,
boolean hasFile,
boolean isNew
){}

public record ResponseStudyNames(
List<String> studyNames
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public interface PostMapper {
@Mapping(target = "id", source = "post.id"),
@Mapping(target = "name", source = "post.user.name"),
@Mapping(target = "parts", source = "post.parts"),
@Mapping(target = "week", source = "post.week"),
@Mapping(target = "position", source = "post.user.position"),
@Mapping(target = "role", source = "post.user.role"),
@Mapping(target = "commentCount", source = "post.commentCount"),
@Mapping(target = "time", source = "post.modifiedAt"),
@Mapping(target = "hasFile", expression = "java(fileExists)"),
Expand All @@ -68,4 +69,7 @@ public interface PostMapper {
})
PostDTO.Response toPostDto(Post post, List<FileResponse> fileUrls, List<CommentDTO.Response> comments);

default PostDTO.ResponseStudyNames toStudyNames(List<String> studyNames) {
return new PostDTO.ResponseStudyNames(studyNames);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import leets.weeth.domain.board.application.mapper.PostMapper;
import leets.weeth.domain.board.domain.entity.Post;
import leets.weeth.domain.board.domain.entity.enums.Category;
import leets.weeth.domain.board.domain.entity.enums.Part;
import leets.weeth.domain.board.domain.service.PostDeleteService;
import leets.weeth.domain.board.domain.service.PostFindService;
import leets.weeth.domain.board.domain.service.PostSaveService;
Expand All @@ -32,16 +33,14 @@
import leets.weeth.domain.user.domain.service.UserCardinalGetService;
import leets.weeth.domain.user.domain.service.UserGetService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.*;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class PostUseCaseImpl implements PostUsecase {
Expand Down Expand Up @@ -135,23 +134,32 @@ public Slice<PostDTO.ResponseEducationAll> findEducationPosts(Long userId, Integ
.map(post -> mapper.toEducationAll(post, checkFileExistsByPost(post.getId())));
}

int targetCardinal = (cardinalNumber != null)
? cardinalNumber
: userCardinalGetService.getCurrentCardinal(user).getCardinalNumber();
Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.DESC, "id"));

if (cardinalNumber != null
&& userCardinalGetService.notContains(user,
cardinalGetService.findByUserSide(cardinalNumber))) {
Pageable empty = PageRequest.of(pageNumber, pageSize);
return new SliceImpl<>(Collections.emptyList(), empty, false);
if (cardinalNumber != null) {
if (userCardinalGetService.notContains(user, cardinalGetService.findByUserSide(cardinalNumber))) {
return new SliceImpl<>(Collections.emptyList(), pageable, false);
}
Slice<Post> posts = postFindService.findEducationByCardinal(cardinalNumber, pageable);
return posts.map(post -> mapper.toEducationAll(post, checkFileExistsByPost(post.getId())));
}

Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.DESC, "id"));
Slice<Post> posts = postFindService.findEducationByCardinal(targetCardinal, pageable);
List<Integer> userCardinals = userCardinalGetService.getCardinalNumbers(user);
if (userCardinals.isEmpty()) {
return new SliceImpl<>(Collections.emptyList(), pageable, false);
}
Slice<Post> posts = postFindService.findEducationByCardinals(userCardinals, pageable);

return posts.map(post -> mapper.toEducationAll(post, checkFileExistsByPost(post.getId())));
}

@Override
public PostDTO.ResponseStudyNames findStudyNames(Part part) {
List<String> names = postFindService.findByPart(part);

return mapper.toStudyNames(names);
}

@Override
public Slice<PostDTO.ResponseAll> searchPost(String keyword, int pageNumber, int pageSize){
validatePageNumber(pageNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import leets.weeth.domain.board.application.dto.PartPostDTO;
import leets.weeth.domain.board.application.dto.PostDTO;
import leets.weeth.domain.board.domain.entity.enums.Part;
import leets.weeth.domain.user.application.exception.UserNotMatchException;
import org.springframework.data.domain.Slice;

Expand All @@ -20,6 +21,8 @@ public interface PostUsecase {

Slice<PostDTO.ResponseEducationAll> findEducationPosts(Long userId, Integer cardinalNumber, int pageNumber, int pageSize);

PostDTO.ResponseStudyNames findStudyNames(Part part);

void update(Long postId, PostDTO.Update dto, Long userId) throws UserNotMatchException;

void updateEducation(Long postId, PostDTO.UpdateEducation dto, Long userId) throws UserNotMatchException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package leets.weeth.domain.board.domain.repository;

import java.util.Collection;
import java.util.List;
import leets.weeth.domain.board.domain.entity.Post;
import leets.weeth.domain.board.domain.entity.enums.Category;
import leets.weeth.domain.board.domain.entity.enums.Part;
Expand All @@ -14,12 +16,19 @@ public interface PostRepository extends JpaRepository<Post, Long> {

Slice<Post> findPageBy(Pageable page);

@Query("""
SELECT DISTINCT p.studyName
FROM Post p
WHERE (:part = leets.weeth.domain.board.domain.entity.enums.Part.ALL OR p.part = :part)
AND p.studyName IS NOT NULL
ORDER BY p.studyName ASC
""")
List<String> findDistinctStudyNamesByPart(@Param("part") Part part);
Copy link
Collaborator

Choose a reason for hiding this comment

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

ALL로 조회하는 경우는 어떻게 반환되나요?

Copy link
Member Author

Choose a reason for hiding this comment

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

해당 내용은 아래 시현님 코멘트와 다르게, ALL 태그로 검색하는거 자체가 정상적이지 않은 상황이라 생각했어서
초기에 구현했을땐 빈배열로 반환해주는게 맞다고 생각했었어요 (추가적인 분기처리 없이)

근데 저희가 전체 스터디 게시판도 존재하니까, ALL 태그로 검색하는 경우에는 파트 구분없이 전체 스터디 이름을 반환해주도록 쿼리문을 수정해주는게 맞다고 판단이 드네용


@Query("""
SELECT p
FROM Post p
WHERE (
p.part = :part
OR p.part = leets.weeth.domain.board.domain.entity.enums.Part.ALL
WHERE (p.part = :part OR p.part = leets.weeth.domain.board.domain.entity.enums.Part.ALL OR :part = leets.weeth.domain.board.domain.entity.enums.Part.ALL
)
AND (:category IS NULL OR p.category = :category)
AND (:cardinal IS NULL OR p.cardinalNumber = :cardinal)
Expand All @@ -29,7 +38,16 @@ public interface PostRepository extends JpaRepository<Post, Long> {
""")
Slice<Post> findByPartAndOptionalFilters(@Param("part") Part part, @Param("category") Category category, @Param("cardinal") Integer cardinal, @Param("studyName") String studyName, @Param("week") Integer week, Pageable pageable);

Slice<Post> findByCategoryAndCardinalNumber(Category category, Integer cardinalNumber, Pageable pageable);
Slice<Post> findByCategoryAndCardinalNumber(Category category, Integer cardinalNumber, Pageable pageable);

@Query("""
SELECT p
FROM Post p
WHERE p.category = :category
AND p.cardinalNumber IN :cardinals
ORDER BY p.id DESC
""")
Slice<Post> findByCategoryAndCardinalIn(@Param("category") Category category, @Param("cardinals") Collection<Integer> cardinals, Pageable pageable);

Slice<Post> findByCategory(Category category, Pageable pageable);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package leets.weeth.domain.board.domain.service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import leets.weeth.domain.board.application.exception.PostNotFoundException;
import leets.weeth.domain.board.domain.entity.Post;
Expand All @@ -10,6 +12,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

Expand All @@ -28,6 +31,10 @@ public List<Post> find(){
return postRepository.findAll();
}

public List<String> findByPart(Part part) {
return postRepository.findDistinctStudyNamesByPart(part);
}

public Slice<Post> findRecentPosts(Pageable pageable) {
return postRepository.findPageBy(pageable);
}
Expand All @@ -39,6 +46,13 @@ public Slice<Post> findByPartAndOptionalFilters(Part part, Category category, In
);
}

public Slice<Post> findEducationByCardinals(Collection<Integer> cardinals, Pageable pageable) {
if (cardinals == null || cardinals.isEmpty()) {
return new SliceImpl<>(Collections.emptyList(), pageable, false);
}
return postRepository.findByCategoryAndCardinalIn(Category.Education, cardinals, pageable);
}

public Slice<Post> findEducationByCardinal(int cardinalNumber, Pageable pageable) {

return postRepository.findByCategoryAndCardinalNumber(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import leets.weeth.domain.board.application.dto.PartPostDTO;
import leets.weeth.domain.board.application.dto.PostDTO;
import leets.weeth.domain.board.application.usecase.PostUsecase;
import leets.weeth.domain.board.domain.entity.enums.Part;
import leets.weeth.domain.user.application.exception.UserNotMatchException;
import leets.weeth.global.auth.annotation.CurrentUser;
import leets.weeth.global.common.response.CommonResponse;
Expand Down Expand Up @@ -75,6 +76,13 @@ public CommonResponse<PostDTO.Response> findPost(@PathVariable Long boardId) {
return CommonResponse.createSuccess(POST_FIND_BY_ID_SUCCESS.getMessage(),postUsecase.findPost(boardId));
}

@GetMapping("/part/studies")
@Operation(summary="파트별 스터디 이름 목록 조회")
public CommonResponse<PostDTO.ResponseStudyNames> findStudyNames(@RequestParam Part part) {

return CommonResponse.createSuccess(ResponseMessage.POST_STUDY_NAMES_FIND_SUCCESS.getMessage(), postUsecase.findStudyNames(part));
}

@GetMapping("/search")
@Operation(summary="게시글 검색 [무한스크롤]")
public CommonResponse<Slice<PostDTO.ResponseAll>> findPost(@RequestParam String keyword, @RequestParam("pageNumber") int pageNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum ResponseMessage {
POST_PART_FIND_ALL_SUCCESS("파트별 게시글 목록이 성공적으로 조회되었습니다."),
POST_EDU_FIND_SUCCESS("교육 게시글 목록이 성공적으로 조회되었습니다."),
POST_FIND_BY_ID_SUCCESS("게시글이 성공적으로 조회되었습니다."),
POST_STUDY_NAMES_FIND_SUCCESS("스터디 이름 목록이 성공적으로 조회되었습니다."),

EDUCATION_UPDATED_SUCCESS("교육자료가 성공적으로 수정되었습니다.");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package leets.weeth.domain.user.domain.repository;

import java.util.List;
import leets.weeth.domain.user.domain.entity.User;
import leets.weeth.domain.user.domain.entity.UserCardinal;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import org.springframework.data.repository.query.Param;

public interface UserCardinalRepository extends JpaRepository<UserCardinal, Long> {

Expand All @@ -15,4 +15,12 @@ public interface UserCardinalRepository extends JpaRepository<UserCardinal, Long
List<UserCardinal> findAllByUsers(List<User> users);

List<UserCardinal> findAllByOrderByUser_NameAsc();

@Query("""
select uc.cardinal.cardinalNumber
from UserCardinal uc
where uc.user = :user
order by uc.cardinal.cardinalNumber desc
""")
List<Integer> findCardinalNumbersByUser(@Param("user") User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public List<Cardinal> findInProgress() {
return cardinalRepository.findAllByStatus(CardinalStatus.IN_PROGRESS);
}

public Cardinal findLatestInProgress() {
return cardinalRepository
.findFirstByStatusOrderByCardinalNumberDesc(CardinalStatus.IN_PROGRESS);
}

public void validateCardinal(Integer cardinal) {
if (cardinalRepository.findByCardinalNumber(cardinal).isPresent()) {
throw new DuplicateCardinalException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package leets.weeth.domain.user.domain.service;

import java.util.Comparator;
import java.util.List;
import leets.weeth.domain.user.application.exception.CardinalNotFoundException;
import leets.weeth.domain.user.domain.entity.Cardinal;
import leets.weeth.domain.user.domain.entity.User;
Expand All @@ -8,9 +10,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserCardinalGetService {
Expand Down Expand Up @@ -50,4 +49,8 @@ public Cardinal getCurrentCardinal(User user) {
.max(Comparator.comparing(Cardinal::getCardinalNumber))
.orElseThrow(CardinalNotFoundException::new);
}

public List<Integer> getCardinalNumbers(User user) {
return userCardinalRepository.findCardinalNumbersByUser(user);
}
}