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 @@ -79,6 +79,7 @@ public record Response(
public record ResponseAll(
Long id,
String name,
Part part,
Position position,
Role role,
String title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public interface NoticeUsecase {

void delete(Long noticeId, Long userId) throws UserNotMatchException;

Slice<NoticeDTO.ResponseAll> searchNotice(String keyword, int pageNumber, int pageSize);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package leets.weeth.domain.board.application.usecase;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import leets.weeth.domain.board.application.dto.NoticeDTO;
import leets.weeth.domain.board.application.exception.NoSearchResultException;
import leets.weeth.domain.board.application.exception.PageNotFoundException;
import leets.weeth.domain.board.application.mapper.NoticeMapper;
import leets.weeth.domain.board.domain.entity.Notice;
Expand Down Expand Up @@ -28,11 +33,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class NoticeUsecaseImpl implements NoticeUsecase {
Expand Down Expand Up @@ -85,6 +85,22 @@ public Slice<NoticeDTO.ResponseAll> findNotices(int pageNumber, int pageSize) {
return notices.map(notice->mapper.toAll(notice, checkFileExistsByNotice(notice.id)));
}

@Override
public Slice<NoticeDTO.ResponseAll> searchNotice(String keyword, int pageNumber, int pageSize) {
validatePageNumber(pageNumber);

keyword = keyword.strip();

Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.DESC, "id"));
Slice<Notice> notices = noticeFindService.search(keyword, pageable);

if (notices.isEmpty()){
throw new NoSearchResultException();
}

return notices.map(notice -> mapper.toAll(notice, checkFileExistsByNotice(notice.id)));
}

@Override
@Transactional
public void update(Long noticeId, NoticeDTO.Update dto, Long userId) {
Expand Down Expand Up @@ -150,4 +166,9 @@ private CommentDTO.Response mapToDtoWithChildren(Comment comment, Map<Long, List
return commentMapper.toCommentDto(comment, children, files);
}

private void validatePageNumber(int pageNumber){
if (pageNumber < 0) {
throw new PageNotFoundException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,29 @@ public Slice<PostDTO.ResponseAll> findPartPosts(PartPostDTO dto, int pageNumber,
}

@Override
public Slice<PostDTO.ResponseEducationAll> findEducationPosts(Long userId, Integer cardinalNumber, int pageNumber, int pageSize) {
public Slice<PostDTO.ResponseEducationAll> findEducationPosts(Long userId, Part part, Integer cardinalNumber, int pageNumber, int pageSize) {
User user = userGetService.find(userId);
Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.DESC, "id"));

if (user.hasRole(Role.ADMIN)) {

return postFindService.findByCategory(Category.Education, pageNumber, pageSize)
return postFindService.findByCategory(part, Category.Education, pageNumber, pageSize)
.map(post -> mapper.toEducationAll(post, checkFileExistsByPost(post.getId())));
}

Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.DESC, "id"));

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

List<Integer> userCardinals = userCardinalGetService.getCardinalNumbers(user);
if (userCardinals.isEmpty()) {
return new SliceImpl<>(Collections.emptyList(), pageable, false);
}
Slice<Post> posts = postFindService.findEducationByCardinals(userCardinals, pageable);
Slice<Post> posts = postFindService.findEducationByCardinals(part, userCardinals, pageable);

return posts.map(post -> mapper.toEducationAll(post, checkFileExistsByPost(post.getId())));
}
Expand Down Expand Up @@ -176,6 +175,22 @@ public Slice<PostDTO.ResponseAll> searchPost(String keyword, int pageNumber, int
return posts.map(post->mapper.toAll(post, checkFileExistsByPost(post.id)));
}

@Override
public Slice<PostDTO.ResponseEducationAll> searchEducation(String keyword, int pageNumber, int pageSize) {
validatePageNumber(pageNumber);

keyword = keyword.strip();

Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.DESC, "id"));
Slice<Post> posts = postFindService.searchEducation(keyword, pageable);

if(posts.isEmpty()){
throw new NoSearchResultException();
}

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

@Override
@Transactional
public void update(Long postId, PostDTO.Update dto, Long userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface PostUsecase {

Slice<PostDTO.ResponseAll> findPartPosts(PartPostDTO dto, int pageNumber, int pageSize);

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

PostDTO.ResponseStudyNames findStudyNames(Part part);

Expand All @@ -30,4 +30,6 @@ public interface PostUsecase {
void delete(Long postId, Long userId) throws UserNotMatchException;

Slice<PostDTO.ResponseAll> searchPost(String keyword, int pageNumber, int pageSize);

Slice<PostDTO.ResponseEducationAll> searchEducation(String keyword, int pageNumber, int pageSize);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;


public interface NoticeRepository extends JpaRepository<Notice, Long> {

Slice<Notice> findPageBy(Pageable page);

@Query("""
SELECT n FROM Notice n
WHERE (LOWER(n.title) LIKE LOWER(CONCAT('%', :kw, '%'))
OR LOWER(n.content) LIKE LOWER(CONCAT('%', :kw, '%')))
ORDER BY n.id DESC
""")
Slice<Notice> search(@Param("kw") String kw, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,47 @@

public interface PostRepository extends JpaRepository<Post, Long> {

Slice<Post> findPageBy(Pageable page);
@Query("""
SELECT p FROM Post p
WHERE p.category IN (
leets.weeth.domain.board.domain.entity.enums.Category.StudyLog,
leets.weeth.domain.board.domain.entity.enums.Category.Article
)
ORDER BY p.id DESC
""")
Slice<Post> findRecentPart(Pageable pageable);

@Query("""
SELECT p FROM Post p
WHERE p.category = leets.weeth.domain.board.domain.entity.enums.Category.Education
ORDER BY p.id DESC
""")
Slice<Post> findRecentEducation(Pageable pageable);

@Query("""
SELECT p FROM Post p
WHERE p.category IN (
leets.weeth.domain.board.domain.entity.enums.Category.StudyLog,
leets.weeth.domain.board.domain.entity.enums.Category.Article
)
AND (
LOWER(p.title) LIKE LOWER(CONCAT('%', :kw, '%'))
OR LOWER(p.content) LIKE LOWER(CONCAT('%', :kw, '%'))
)
ORDER BY p.id DESC
""")
Slice<Post> searchPart(@Param("kw") String kw, Pageable pageable);

@Query("""
SELECT p FROM Post p
WHERE p.category = leets.weeth.domain.board.domain.entity.enums.Category.Education
AND (
LOWER(p.title) LIKE LOWER(CONCAT('%', :kw, '%'))
OR LOWER(p.content) LIKE LOWER(CONCAT('%', :kw, '%'))
)
ORDER BY p.id DESC
""")
Slice<Post> searchEducation(@Param("kw") String kw, Pageable pageable);

@Query("""
SELECT DISTINCT p.studyName
Expand All @@ -38,18 +78,44 @@ 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);

@Query("""
SELECT p
FROM Post p
WHERE p.category = :category
AND p.cardinalNumber IN :cardinals
AND (
:partName = 'ALL'
OR FUNCTION('FIND_IN_SET', :partName, p.parts) > 0
OR FUNCTION('FIND_IN_SET', 'ALL', p.parts) > 0
)
ORDER BY p.id DESC
""")
Slice<Post> findByCategoryAndCardinalIn(@Param("category") Category category, @Param("cardinals") Collection<Integer> cardinals, Pageable pageable);
Slice<Post> findByCategoryWithPart(@Param("partName") String partName, @Param("category") Category category, Pageable pageable);

Slice<Post> findByCategory(Category category, Pageable pageable);
@Query("""
SELECT p
FROM Post p
WHERE p.category = :category
AND p.cardinalNumber = :cardinal
AND (
:partName = 'ALL'
OR FUNCTION('FIND_IN_SET', :partName, p.parts) > 0
OR FUNCTION('FIND_IN_SET', 'ALL', p.parts) > 0
)
ORDER BY p.id DESC
""")
Slice<Post> findByCategoryAndCardinalNumberWithPart(@Param("partName") String partName, @Param("category") Category category, @Param("cardinal") Integer cardinal, Pageable pageable);

Slice<Post> findByTitleContainingOrContentContainingIgnoreCase(String keyword1, String keyword2, Pageable pageable);
@Query("""
SELECT p
FROM Post p
WHERE p.category = :category
AND p.cardinalNumber IN :cardinals
AND (
:partName = 'ALL'
OR FUNCTION('FIND_IN_SET', :partName, p.parts) > 0
OR FUNCTION('FIND_IN_SET', 'ALL', p.parts) > 0
)
ORDER BY p.id DESC
""")
Slice<Post> findByCategoryAndCardinalInWithPart(@Param("partName") String partName, @Param("category") Category category, @Param("cardinals") Collection<Integer> cardinals, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package leets.weeth.domain.board.domain.service;

import java.util.List;
import leets.weeth.domain.board.application.exception.NoticeNotFoundException;
import leets.weeth.domain.board.domain.entity.Notice;
import leets.weeth.domain.board.domain.repository.NoticeRepository;
import leets.weeth.domain.board.application.exception.NoticeNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class NoticeFindService {
Expand All @@ -30,4 +29,10 @@ public Slice<Notice> findRecentNotices(Pageable pageable) {
return noticeRepository.findPageBy(pageable);
}

public Slice<Notice> search(String keyword, Pageable pageable) {
if(keyword == null || keyword.isEmpty()){
return findRecentNotices(pageable);
}
return noticeRepository.search(keyword.strip(), pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,25 @@ public List<String> findByPart(Part part) {
}

public Slice<Post> findRecentPosts(Pageable pageable) {
return postRepository.findPageBy(pageable);
return postRepository.findRecentPart(pageable);
}

public Slice<Post> findRecentEducationPosts(Pageable pageable) {
return postRepository.findRecentEducation(pageable);
}

public Slice<Post> search(String keyword, Pageable pageable) {
if(keyword == null || keyword.isEmpty()){
return findRecentPosts(pageable);
}
return postRepository.searchPart(keyword.strip(), pageable);
}

public Slice<Post> searchEducation(String keyword, Pageable pageable) {
if(keyword == null || keyword.isEmpty()){
return findRecentEducationPosts(pageable);
}
return postRepository.searchEducation(keyword.strip(), pageable);
}

public Slice<Post> findByPartAndOptionalFilters(Part part, Category category, Integer cardinalNumber, String studyName, Integer week, Pageable pageable) {
Expand All @@ -46,33 +64,25 @@ public Slice<Post> findByPartAndOptionalFilters(Part part, Category category, In
);
}

public Slice<Post> findEducationByCardinals(Collection<Integer> cardinals, Pageable pageable) {
public Slice<Post> findEducationByCardinals(Part part, Collection<Integer> cardinals, Pageable pageable) {
if (cardinals == null || cardinals.isEmpty()) {
return new SliceImpl<>(Collections.emptyList(), pageable, false);
}
return postRepository.findByCategoryAndCardinalIn(Category.Education, cardinals, pageable);
String partName = (part != null ? part.name() : Part.ALL.name());

return postRepository.findByCategoryAndCardinalInWithPart(partName, Category.Education, cardinals, pageable);
}

public Slice<Post> findEducationByCardinal(int cardinalNumber, Pageable pageable) {
public Slice<Post> findEducationByCardinal(Part part, int cardinalNumber, Pageable pageable) {
String partName = (part != null ? part.name() : Part.ALL.name());

return postRepository.findByCategoryAndCardinalNumber(
Category.Education, cardinalNumber, pageable
);
return postRepository.findByCategoryAndCardinalNumberWithPart(partName, Category.Education, cardinalNumber, pageable);
}

public Slice<Post> findByCategory(Category category, int pageNumber, int pageSize) {
public Slice<Post> findByCategory(Part part, Category category, int pageNumber, int pageSize) {
Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.DESC, "id"));
String partName = (part != null ? part.name() : Part.ALL.name());

return postRepository.findByCategory(category, pageable);
return postRepository.findByCategoryWithPart(partName, category, pageable);
}

public Slice<Post> search(String keyword, Pageable pageable) {
if(keyword == null || keyword.isEmpty()){
return findRecentPosts(pageable);
}
else{
return postRepository.findByTitleContainingOrContentContainingIgnoreCase(keyword, keyword, pageable);
}
}

}
Loading