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] 코스 하나보기 기능 #42

Merged
merged 1 commit into from
Apr 7, 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 @@ -9,6 +9,7 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import sungdong29.backend.domain.course.dto.request.CourseCreateRequestDTO;
import sungdong29.backend.domain.course.dto.response.CourseResponseDTO;
import sungdong29.backend.domain.course.dto.response.SimpleCourseResponseDTO;
import sungdong29.backend.domain.course.service.CourseService;
import sungdong29.backend.domain.course.domain.Category;
Expand All @@ -35,7 +36,6 @@ public ResponseEntity<List<SimpleCourseResponseDTO>> getMyCourse(
return ResponseEntity.ok(simpleCourseResponseDTO);
}

//카테고리 별 코스 조회
@Operation(summary = "카테고리 별 코스 조회")
@GetMapping("/category/{category}")
public ResponseEntity<List<SimpleCourseResponseDTO>> getCourseByCategory(
Expand All @@ -46,6 +46,16 @@ public ResponseEntity<List<SimpleCourseResponseDTO>> getCourseByCategory(
return ResponseEntity.ok(simpleCourseResponseDTO);
}

@Operation(summary = "코스 하나 조회")
@GetMapping("/{courseId}")
public ResponseEntity<CourseResponseDTO> getCourse(
@PathVariable Long courseId
) {
log.info("코스 하나 조회");
CourseResponseDTO courseResponseDTO = courseService.getCourse(courseId);
return ResponseEntity.ok(courseResponseDTO);
}

//코스 검색
@Operation(summary = "코스 검색")
@GetMapping("/search")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package sungdong29.backend.domain.course.dto.response;

import lombok.Builder;
import lombok.Getter;
import sungdong29.backend.domain.course.domain.Course;
import sungdong29.backend.domain.course.domain.Category;
import sungdong29.backend.domain.place.dto.response.SimplePlaceResponseDTO;

import java.util.List;

@Getter
public class CourseResponseDTO {

private Long id;

private String name;

private String description;

private String image;

private Long likeCount;

private Category category1;

private Category category2;

private Category category3;

private Boolean isSungDongSelected;

private Boolean isSungDongRecommended;

private List<SimplePlaceResponseDTO> placeList;

@Builder
private CourseResponseDTO(Long id, String name, String description, String image, Long likeCount, Category category1, Category category2, Category category3, Boolean isSungDongSelected, Boolean isSungDongRecommended, List<SimplePlaceResponseDTO> placeList) {
this.id = id;
this.name = name;
this.description = description;
this.image = image;
this.likeCount = likeCount;
this.category1 = category1;
this.category2 = category2;
this.category3 = category3;
this.isSungDongSelected = isSungDongSelected;
this.isSungDongRecommended = isSungDongRecommended;
this.placeList = placeList;
}

public static CourseResponseDTO of(Course course, Long likeCount, List<SimplePlaceResponseDTO> placeList) {
return CourseResponseDTO.builder()
.id(course.getId())
.name(course.getName())
.description(course.getDescription())
.image(course.getImage())
.likeCount(likeCount)
.category1(course.getCategory1())
.category2(course.getCategory2())
.category3(course.getCategory3())
.isSungDongSelected(course.getIsSungDongSelected())
.isSungDongRecommended(course.getIsSungDongRecommended())
.placeList(placeList)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ public interface CoursePlaceRepository extends JpaRepository<CoursePlace, Long>
Long countDistinctByPlace(Place place);
@Query("SELECT DISTINCT cp.course FROM CoursePlace cp WHERE cp.place = :place")
List<Course> findDistinctCourseByPlace(Place place);

@Query("SELECT DISTINCT cp.place FROM CoursePlace cp WHERE cp.course = :course")
List<Place> findPlaceByCourse(Course course);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import sungdong29.backend.domain.course.domain.CourseLike;
import sungdong29.backend.domain.course.domain.CoursePlace;
import sungdong29.backend.domain.course.dto.request.CourseCreateRequestDTO;
import sungdong29.backend.domain.course.dto.response.CourseResponseDTO;
import sungdong29.backend.domain.course.dto.response.SimpleCourseResponseDTO;
import sungdong29.backend.domain.course.exception.CourseNotFound;
import sungdong29.backend.domain.course.repository.CourseLikeRepository;
import sungdong29.backend.domain.course.repository.CoursePlaceRepository;
import sungdong29.backend.domain.course.repository.CourseRepository;
import sungdong29.backend.domain.course.domain.Category;
import sungdong29.backend.domain.place.domain.Place;
import sungdong29.backend.domain.place.dto.response.SimplePlaceResponseDTO;
import sungdong29.backend.domain.place.exception.PlaceNotFound;
import sungdong29.backend.domain.place.repository.PlaceLikeRepository;
import sungdong29.backend.domain.place.repository.PlaceRepository;
import sungdong29.backend.domain.user.domain.User;
import sungdong29.backend.global.config.user.UserDetails;
Expand All @@ -28,6 +31,7 @@ public class CourseService {
private final PlaceRepository placeRepository;
private final CoursePlaceRepository coursePlaceRepository;
private final CourseLikeRepository courseLikeRepository;
private final PlaceLikeRepository placeLikeRepository;

// 내 코스 조회
public List<SimpleCourseResponseDTO> getMyCourse(UserDetails userDetails) {
Expand All @@ -49,6 +53,20 @@ public List<SimpleCourseResponseDTO> getCourseByCategory(Category category) {
.toList();
}

// 코스 하나 조회
public CourseResponseDTO getCourse(Long courseId) {
Course course = courseRepository.findById(courseId)
.orElseThrow(() -> CourseNotFound.EXCEPTION);

Long likeCount = courseLikeRepository.countByCourse(course);
List<SimplePlaceResponseDTO> placeList = coursePlaceRepository.findPlaceByCourse(course)
.stream()
.map(place -> SimplePlaceResponseDTO.of(place, placeLikeRepository.countByPlace(place), null))
.toList();

return CourseResponseDTO.of(course, likeCount, placeList);
}

// 코스 검색
public List<SimpleCourseResponseDTO> searchCourse(String keyword) {
List<Course> courseList = courseRepository.findAllByNameContaining(keyword);
Expand Down
Loading