Skip to content

자유게시글, 질문글에 대한 베스트글 요청 API #76

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

Merged
merged 4 commits into from
Oct 2, 2024
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 @@ -20,6 +20,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Tag(name = "Post", description = "Post 관련 API")
@RestController
@RequiredArgsConstructor
Expand All @@ -38,21 +40,33 @@ public ResponseEntity<SavePostResponse> createPost(@RequestBody SavePostReqeust
@Operation(summary = "Post 목록 조회 API", description = "Post의 목록을 조회하는 API")
@Parameter(name = "page", description = "페이지 번호")
@Parameter(name = "size", description = "페이지 크기")
@Parameter(name = "criteria", description = "정렬 기준 (createAt | likeCount)")
@Parameter(name = "criteria", description = "정렬 기준 (createdAt | likeCount)")
@GetMapping
public ResponseEntity<PageResponse<GetPostSummaryResponse>> getPostList(
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "20") int size,
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria
) {
Pageable pageable = switch (criteria) {
case "likeCount" -> PageRequest.of(page, size, Sort.by(criteria).descending());
default -> PageRequest.of(page, size, Sort.by(criteria));
case "likeCount" -> PageRequest.of(page, size, Sort.by("likeCount", "createdAt").descending());
default -> PageRequest.of(page, size, Sort.by("createdAt"));
};
PageResponse<GetPostSummaryResponse> response = postQueryService.getPostList(pageable);
return ResponseEntity.ok(response);
}

@Operation(summary = "인기 Post 조회 API", description = "특정 기간 내 인기 게시글 n개를 불러오는 API")
@GetMapping("/best")
public ResponseEntity<PageResponse<GetPostSummaryResponse>> getBestPostList(
@RequestParam(value = "size", defaultValue = "20") int size,
@RequestParam(value = "period", defaultValue = "1") int period
) {
LocalDateTime weeksAgo = LocalDateTime.now().minusWeeks(period);
Pageable pageable = PageRequest.of(0, size, Sort.by("likeCount", "createdAt").descending());
PageResponse<GetPostSummaryResponse> response = postQueryService.getBestPostList(weeksAgo, pageable);
return ResponseEntity.ok(response);
}

@Operation(summary = "Post 상세 조회 API", description = "Post와 Comments를 PostId로 불러오는 API")
@GetMapping("/{postId}")
public ResponseEntity<GetPostResponse> getPostById(@PathVariable("postId") Long postId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gdyd.gdydapi.request.mentoring.CreateHighSchoolStudentQuestionRequest;
import com.gdyd.gdydapi.request.mentoring.CreateUniversityStudentAnswerRequest;
import com.gdyd.gdydapi.request.report.ReportRequest;
import com.gdyd.gdydapi.response.board.GetPostSummaryResponse;
import com.gdyd.gdydapi.response.common.LikeListResponse;
import com.gdyd.gdydapi.response.common.PageResponse;
import com.gdyd.gdydapi.response.common.ScrapListResponse;
Expand All @@ -24,6 +25,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Tag(name = "Mentoring", description = "Mentoring 관련 API")
@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -55,21 +58,33 @@ public ResponseEntity<CreateUniversityStudentAnswerResponse> createUniversityStu
@Operation(summary = "고등학생 질문 목록 조회 API", description = "고등학생 질문 목록을 페이지네이션으로 조회하는 API")
@Parameter(name = "page", description = "페이지 번호")
@Parameter(name = "size", description = "페이지 크기")
@Parameter(name = "criteria", description = "정렬 기준 (createAt | likeCount)")
@Parameter(name = "criteria", description = "정렬 기준 (createdAt | likeCount)")
@GetMapping("/high-school-student-questions")
public ResponseEntity<PageResponse<HighSchoolStudentQuestionResponse>> getHighSchoolStudentQuestionList(
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "20") int size,
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria
) {
Pageable pageable = switch (criteria) {
case "likeCount" -> PageRequest.of(page, size, Sort.by(criteria).descending());
default -> PageRequest.of(page, size, Sort.by(criteria));
case "likeCount" -> PageRequest.of(page, size, Sort.by("likeCount", "createdAt").descending());
default -> PageRequest.of(page, size, Sort.by("createdAt"));
};
PageResponse<HighSchoolStudentQuestionResponse> response = mentoringQueryService.getHighSchoolStudentQuestions(pageable);
return ResponseEntity.ok(response);
}

@Operation(summary = "인기 질문글 조회 API", description = "특정 기간 내 인기 질문글 n개를 불러오는 API")
@GetMapping("/high-school-student-questions/best")
public ResponseEntity<PageResponse<HighSchoolStudentQuestionResponse>> getBestHighSchoolStudentQuestionList(
@RequestParam(value = "size", defaultValue = "20") int size,
@RequestParam(value = "period", defaultValue = "1") int period
) {
LocalDateTime weeksAgo = LocalDateTime.now().minusWeeks(period);
Pageable pageable = PageRequest.of(0, size, Sort.by("likeCount", "createdAt").descending());
PageResponse<HighSchoolStudentQuestionResponse> response = mentoringQueryService.getBestHighSchoolStudentQuestions(weeksAgo, pageable);
return ResponseEntity.ok(response);
}

@Operation(summary = "고등학생 질문 상세 조회 API", description = "고등학생 질문 상세 정보(대학생 답변 내용 등)를 조회하는 API")
@Parameter(name = "highSchoolStudentQuestionId", description = "고등학생 질문글 ID", required = true)
@GetMapping("/high-school-student-question/{highSchoolStudentQuestionId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -26,4 +29,9 @@ public GetPostResponse getPostAndCommentsByPostId(Long postId) {
Post post = postService.getPostById(postId);
return GetPostResponse.from(post);
}

public PageResponse<GetPostSummaryResponse> getBestPostList(LocalDateTime weeksAgo, Pageable pageable) {
List<Post> posts = postService.findAllByCreatedAtIsAfter(weeksAgo, pageable);
return PageResponse.of(posts.stream().map(GetPostSummaryResponse::from).toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
Expand All @@ -29,6 +30,11 @@ public PageResponse<HighSchoolStudentQuestionResponse> getHighSchoolStudentQuest
return PageResponse.of(pages.getContent().stream().map(HighSchoolStudentQuestionResponse::from).toList());
}

public PageResponse<HighSchoolStudentQuestionResponse> getBestHighSchoolStudentQuestions(LocalDateTime weeksAgo, Pageable pageable) {
List<HighSchoolStudentQuestion> questions = highSchoolStudentQuestionService.findAllByCreatedAtIsAfter(weeksAgo, pageable);
return PageResponse.of(questions.stream().map(HighSchoolStudentQuestionResponse::from).toList());
}

/**
* 고등학생 질문 상세 조회
* @param highSchoolStudentQuestionId 고등학생 질문 ID
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.gdyd.gdydcore.repository.board;

import com.gdyd.gdydcore.domain.board.Post;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface PostRepository extends JpaRepository<Post, Long> {
Optional<Post> findByIdAndMemberId(Long postId, Long memberId);

List<Post> findAllByCreatedAtIsAfter(LocalDateTime weeksAgo, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.gdyd.gdydcore.repository.mentoring;

import com.gdyd.gdydcore.domain.mentoring.HighSchoolStudentQuestion;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface HighSchoolStudentQuestionRepository extends JpaRepository<HighSchoolStudentQuestion, Long> {
import java.time.LocalDateTime;
import java.util.List;

public interface HighSchoolStudentQuestionRepository extends JpaRepository<HighSchoolStudentQuestion, Long> {
List<HighSchoolStudentQuestion> findAllByCreatedAtIsAfter(LocalDateTime weeksAgo, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -35,6 +38,10 @@ public Post getPostByIdAndMemberId(Long postId, Long memberId) {
.orElseThrow(() -> new BusinessException(ErrorCode.UNAUTHORIZED_MEMBER));
}

public List<Post> findAllByCreatedAtIsAfter(LocalDateTime weeksAgo, Pageable pageable) {
return postRepository.findAllByCreatedAtIsAfter(weeksAgo, pageable);
}

@Transactional
public void deletePost(Long postId) {
postRepository.deleteById(postId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -25,6 +28,10 @@ public Page<HighSchoolStudentQuestion> findHighSchoolStudentQuestionByPagination
return highSchoolStudentQuestionRepository.findAll(pageable);
}

public List<HighSchoolStudentQuestion> findAllByCreatedAtIsAfter(LocalDateTime weeksAgo, Pageable pageable) {
return highSchoolStudentQuestionRepository.findAllByCreatedAtIsAfter(weeksAgo, pageable);
}

@Transactional
public void save(HighSchoolStudentQuestion highSchoolStudentQuestion) {
highSchoolStudentQuestionRepository.save(highSchoolStudentQuestion);
Expand Down
Loading