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

[FEAT] 데이트 코스 필터링 조회(메인) API 구현 - #79 #83

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -37,6 +37,12 @@ public ResponseEntity<CourseGetAllRes> getAllCourse(
return ResponseEntity.ok(courseAll);
}

@GetMapping("/sort")
public ResponseEntity<CourseGetAllRes> getSortedCourses(final @RequestParam String sortBy) {
CourseGetAllRes courseSortedRes = courseService.getSortedCourses(sortBy);
return ResponseEntity.ok(courseSortedRes);
}

@GetMapping("/date-access")
public ResponseEntity<DateAccessGetAllRes> getAllDataAccesCourse(
final @UserId Long userId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.dateroad.date.repository.CourseRepository;
import org.dateroad.dateAccess.domain.DateAccess;
import org.dateroad.dateAccess.repository.DateAccessRepository;
import org.dateroad.exception.DateRoadException;
import org.dateroad.exception.EntityNotFoundException;
import org.dateroad.image.domain.Image;
import org.dateroad.image.repository.ImageRepository;
Expand All @@ -35,6 +34,8 @@
import org.dateroad.tag.repository.CourseTagRepository;
import org.dateroad.user.domain.User;
import org.dateroad.user.repository.UserRepository;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -62,6 +63,18 @@ public CourseGetAllRes getAllCourses(final CourseGetAllReq courseGetAllReq) {
return CourseGetAllRes.of(courseDtoGetResList);
}

public CourseGetAllRes getSortedCourses(String sortBy) {
List<Course> courses;
if (sortBy.equalsIgnoreCase("POPULAR")) {
courses = getCoursesSortedByLikes();
} else if (sortBy.equalsIgnoreCase("LATEST")) {
courses = getCoursesSortedByLatest();
} else {
throw new EntityNotFoundException(FailureCode.SORT_TYPE_NOT_FOUND);
}
List<CourseDtoGetRes> courseDtoGetResList = convertToDtoList(courses, Function.identity());
return CourseGetAllRes.of(courseDtoGetResList);
}

@Transactional
public void createCourseLike(final Long userId, final Long courseId) {
Expand All @@ -79,6 +92,16 @@ public void deleteCourseLike(Long userId, Long courseId) {
likeRepository.delete(findLike);
}

public List<Course> getCoursesSortedByLikes() {
Pageable pageable = PageRequest.of(0, 5);
return courseRepository.findTopCoursesByLikes(pageable);
}

public List<Course> getCoursesSortedByLatest() {
Pageable pageable = PageRequest.of(0, 3);
return courseRepository.findTopCoursesByCreatedAt(pageable);
}

private <T> List<CourseDtoGetRes> convertToDtoList(final List<T> entities, final Function<T, Course> converter) {
return entities.stream()
.map(converter)
Expand Down Expand Up @@ -173,7 +196,7 @@ private CoursePaymentType validateUserFreeOrPoint(final User user, final int req
if (user.getFree() > 0) {
return CoursePaymentType.FREE; // User가 free를 갖고 있으면 true를 반환
} else if (user.getTotalPoint() < requiredPoints) {
throw new DateRoadException(FailureCode.INSUFFICIENT_USER_POINTS);
throw new EntityNotFoundException(FailureCode.INSUFFICIENT_USER_POINTS);
}
return CoursePaymentType.POINT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public enum FailureCode {
COURSE_TAG_NOT_FOUND(HttpStatus.NOT_FOUND, "e40410", "데이트 태그를 찾을 수 없습니다."),
NEAREST_DATE_NOT_FOUND(HttpStatus.NOT_FOUND, "e40411", "다가오는 데이트를 찾을 수 없습니다."),
LIKE_NOT_FOUND(HttpStatus.NOT_FOUND, "e40412", "해당 데이트 코스에 좋아요를 찾을 수 없습니다."),
INSUFFICIENT_USER_POINTS(HttpStatus.NOT_FOUND, "e40413", "유저의 포인트가 부족합니다."),
SORT_TYPE_NOT_FOUND(HttpStatus.UNAUTHORIZED, "e4029", "해당 순서 타입을 찾을 수 없습니다."),

INSUFFICIENT_USER_POINTS(HttpStatus.NOT_FOUND, "e4048", "유저의 포인트가 부족합니다."),
/**
* 405 Method Not Allowed
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package org.dateroad.date.repository;

import java.util.List;
import java.util.Optional;

import org.dateroad.date.domain.Course;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

// CourseRepository.java
@Repository
public interface CourseRepository extends JpaRepository<Course, Long> , JpaSpecificationExecutor<Course> {
boolean existsByUserId(Long userId);

@Query("SELECT c FROM Course c LEFT JOIN Like l ON c.id = l.course.id GROUP BY c.id ORDER BY COUNT(l) DESC")
List<Course> findTopCoursesByLikes(Pageable pageable);
@Query("SELECT c FROM Course c ORDER BY c.createdAt DESC")
List<Course> findTopCoursesByCreatedAt(Pageable pageable);
}