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 @@ -3,6 +3,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -82,12 +83,8 @@ public ApiResponse<Page<PostSummaryDTO>> getPosts(

@GetMapping("/pinned")
@Operation(summary = "고정된 모든 게시물 가져오기", description = "고정된 모든 게시물을 가져오는 로직입니다.")
public ApiResponse<Page<PostSummaryDTO>> getPinnedPosts(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size
) {
Pageable pageable = PageRequest.of(page, size);
final Page<PostSummaryDTO> posts = announcementService.findPinnedPosts(pageable);
public ApiResponse<List<PostSummaryDTO>> getPinnedPosts() {
final List<PostSummaryDTO> posts = announcementService.findPinnedPosts();

return ApiResponse.from(HttpStatus.OK, "성공", posts);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package run.attraction.api.v1.announcement.repository;

import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -42,7 +43,7 @@ public interface AnnouncementRepository extends JpaRepository<Post, Long> {
WHERE p.isPinned = true
ORDER BY p.createdAt DESC
""")
Page<Post> findAllWithPinned(Pageable pageable);
List<Post> findAllWithPinned();

@Query("""
SELECT p
Expand All @@ -57,4 +58,11 @@ public interface AnnouncementRepository extends JpaRepository<Post, Long> {
ORDER BY p.createdAt DESC
""")
Page<Post> findPostsBySearchQuery(Pageable pageable, String searchType, String searchQuery);

@Query("""
SELECT COUNT(p)
FROM Post p
WHERE p.isPinned = true
""")
int countPinnedPosts();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package run.attraction.api.v1.announcement.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -19,12 +20,17 @@
@RequiredArgsConstructor
public class AnnouncementService {

private static final int PINNED_LIMIT = 3;

private final AnnouncementRepository announcementRepository;

@Transactional
public void createPost(final PostCreateRequestDTO request) {
PostCategory category = PostCategory.valueOf(request.postCategory().toUpperCase());
if (request.isPinned()) {
checkPinnedPostCount();
}

PostCategory category = PostCategory.valueOf(request.postCategory().toUpperCase());
Post post = Post.builder()
.title(request.title())
.content(request.content())
Expand All @@ -35,6 +41,12 @@ public void createPost(final PostCreateRequestDTO request) {
announcementRepository.save(post);
}

private void checkPinnedPostCount() {
if (announcementRepository.countPinnedPosts() >= PINNED_LIMIT) {
throw new IllegalArgumentException("고정글은 " + PINNED_LIMIT + "개를 초과할 수 없습니다.");
}
}

@Transactional(readOnly = true)
public PostDTO findPostById(final Long postId) {
final Post post = announcementRepository.findById(postId)
Expand All @@ -54,11 +66,16 @@ public void deletePostById(final Long postId) {
}

@Transactional
public void updatePostById(final Long postId, final UpdatePostRequestDTO post) {
public void updatePostById(final Long postId, final UpdatePostRequestDTO request) {
final Post beforePost = announcementRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 게시물입니다."));

beforePost.update(post.title(), post.content(), PostCategory.findByName(post.postCategory()), post.isPinned());
if (request.isPinned()) {
checkPinnedPostCount();
}

beforePost.update(request.title(), request.content(), PostCategory.findByName(request.postCategory()),
request.isPinned());
announcementRepository.save(beforePost);
}

Expand All @@ -70,10 +87,10 @@ public Page<PostSummaryDTO> findPosts(Pageable pageable) {
}

@Transactional(readOnly = true)
public Page<PostSummaryDTO> findPinnedPosts(Pageable pageable) {
final Page<Post> posts = announcementRepository.findAllWithPinned(pageable);
public List<PostSummaryDTO> findPinnedPosts() {
final List<Post> posts = announcementRepository.findAllWithPinned();

return posts.map(PostSummaryDTO::new);
return posts.stream().map(PostSummaryDTO::new).toList();
}

@Transactional(readOnly = true)
Expand Down
Loading