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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ target/
.settings
.springBeans
.sts4-cache
.env

### IntelliJ IDEA ###
.idea
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import java.util.Set;

@Entity
@Table(name = "course")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@Table(name = "course")
public class Course extends AbstractAuditEntity {

@Id
Expand Down Expand Up @@ -53,7 +53,6 @@ public class Course extends AbstractAuditEntity {

private boolean free;


@Enumerated(EnumType.STRING)
private CourseStatus status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,23 @@ public ResponseEntity<List<CourseListGetVM>> getPageableCourse (
}


@GetMapping("/courses/search")
public ResponseEntity<PageableData<CourseListGetVM>> getCoursesByMultiQueryWithPageable (
@RequestParam(value = "pageNum", defaultValue = Constants.PageableConstant.DEFAULT_PAGE_NUMBER, required = false) int pageNum,
@RequestParam(value = "pageSize", defaultValue = Constants.PageableConstant.DEFAULT_PAGE_SIZE, required = false) int pageSize,
@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "ratingStar", required = false) Float rating,
@RequestParam(value = "level", required = false) List<String> level,
@RequestParam(value = "free", required = false) List<Boolean> free,
@RequestParam(value = "categoryName", required = false) String categoryName,
@RequestParam(value = "topicId", required = false) Integer topicId
) {
PageableData<CourseListGetVM> pageableCourses = courseService.getCoursesByMultiQuery(pageNum, pageSize, keyword, rating, level, free, categoryName, topicId);
return ResponseEntity.ok().body(pageableCourses);
}

// @GetMapping("/courses/search")
// public ResponseEntity<PageableData<CourseListGetVM>> getCoursesByMultiQueryWithPageable (
// public ResponseEntity<List<CourseListGetVM>> getCoursesByMultiQuery (
// @RequestParam(value = "pageNum", defaultValue = Constants.PageableConstant.DEFAULT_PAGE_NUMBER, required = false) int pageNum,
// @RequestParam(value = "pageSize", defaultValue = Constants.PageableConstant.DEFAULT_PAGE_SIZE, required = false) int pageSize,
// @RequestParam(value = "keyword", required = false) String keyword,
Expand All @@ -57,27 +72,11 @@ public ResponseEntity<List<CourseListGetVM>> getPageableCourse (
// @RequestParam(value = "free", required = false) Boolean[] free,
// @RequestParam(value = "categoryName", required = false) String categoryName,
// @RequestParam(value = "topicId", required = false) Integer topicId
// ) {
// PageableData<CourseListGetVM> pageableCourses = courseService.getCoursesByMultiQuery(pageNum, pageSize, keyword, rating, level, free, categoryName, topicId);
// return ResponseEntity.ok().body(pageableCourses);
// ) {
// List<CourseListGetVM> coursesByMultiQueryReturnList = courseService.getCoursesByMultiQueryReturnList(pageNum, pageSize, keyword, rating, level, free, categoryName, topicId);
// return ResponseEntity.ok().body(coursesByMultiQueryReturnList);
// }

// this
@GetMapping("/courses/search")
public ResponseEntity<List<CourseListGetVM>> getCoursesByMultiQuery (
@RequestParam(value = "pageNum", defaultValue = Constants.PageableConstant.DEFAULT_PAGE_NUMBER, required = false) int pageNum,
@RequestParam(value = "pageSize", defaultValue = Constants.PageableConstant.DEFAULT_PAGE_SIZE, required = false) int pageSize,
@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "ratingStar", required = false) Float rating,
@RequestParam(value = "level", required = false) String[] level,
@RequestParam(value = "free", required = false) Boolean[] free,
@RequestParam(value = "categoryName", required = false) String categoryName,
@RequestParam(value = "topicId", required = false) Integer topicId
) {
List<CourseListGetVM> coursesByMultiQueryReturnList = courseService.getCoursesByMultiQueryReturnList(pageNum, pageSize, keyword, rating, level, free, categoryName, topicId);
return ResponseEntity.ok().body(coursesByMultiQueryReturnList);
}

@PostMapping("/admin/courses")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Created", content =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.backend.elearning.domain.section.Section;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

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

Expand Down Expand Up @@ -222,37 +224,95 @@ List<Course> findByMultiQuery(
@Param("categoryName") String categoryName,
@Param("topicId") Integer topicId
);
@Query(value = """
select c
from Course c
join fetch c.category cat
left join fetch cat.parent p
join fetch c.topic t
join fetch c.user u
left join fetch c.reviews
where LOWER(c.title) LIKE LOWER(CONCAT('%', :title, '%'))
and (:level IS NULL or c.level in :level)
and (:free IS NULL or c.free in :free)
and (:categoryName IS NULL or cat.name = :categoryName or p.name = :categoryName)
and (:topicId IS NULL or t.id = :topicId)
and (:ratingStar IS NULL or
(select avg(r.ratingStar)
from Review r
join r.course rc
where rc.id = c.id
group by rc.id) >= :ratingStar)
and c.status = 'PUBLISHED'
""")
Page<Course> findByMultiQueryWithKeyword(Pageable pageable,
@Param("title") String title,
@Param("ratingStar") Float ratingStar,
@Param("level") String[] level,
@Param("free") Boolean[] free,
@Param("categoryName") String categoryName,
@Param("topicId") Integer topicId


// @Query(value = """
// select c
// from Course c
// join fetch c.category cat
// left join fetch cat.parent p
// join fetch c.topic t
// join fetch c.user u
// left join fetch c.reviews
// where LOWER(c.title) LIKE LOWER(CONCAT('%', :title, '%'))
// and (:level IS NULL or c.level in :level)
// and (:free IS NULL or c.free in :free)
// and (:categoryName IS NULL or cat.name = :categoryName or p.name = :categoryName)
// and (:topicId IS NULL or t.id = :topicId)
// and (:ratingStar IS NULL or
// (select avg(r.ratingStar)
// from Review r
// join r.course rc
// where rc.id = c.id
// group by rc.id) >= :ratingStar)
// and c.status = 'PUBLISHED'
// """)
// Page<Course> findByMultiQueryWithKeyword(Pageable pageable,
// @Param("title") String title,
// @Param("ratingStar") Float ratingStar,
// @Param("level") String[] level,
// @Param("free") Boolean[] free,
// @Param("categoryName") String categoryName,
// @Param("topicId") Integer topicId
// );

@EntityGraph(attributePaths = {"category","category.parent","topic","user"})
@Query(
value = """
SELECT c
FROM Course c
JOIN c.category cat
LEFT JOIN cat.parent p
JOIN c.topic t
JOIN c.user u
WHERE
(:title IS NULL OR :title = '' OR LOWER(c.title) LIKE LOWER(CONCAT('%', :title, '%')))
AND (:levels IS NULL OR c.level IN :levels)
AND (:freeFlags IS NULL OR c.free IN :freeFlags)
AND (:categoryName IS NULL OR cat.name = :categoryName OR p.name = :categoryName)
AND (:topicId IS NULL OR t.id = :topicId)
AND c.status = 'PUBLISHED'
AND (
:ratingStar IS NULL OR
(SELECT COALESCE(AVG(r.ratingStar), 0)
FROM Review r
WHERE r.course = c) >= :ratingStar
)
ORDER BY c.createdAt DESC, c.id DESC
""",
countQuery = """
SELECT COUNT(c)
FROM Course c
JOIN c.category cat
LEFT JOIN cat.parent p
JOIN c.topic t
WHERE
(:title IS NULL OR :title = '' OR LOWER(c.title) LIKE LOWER(CONCAT('%', :title, '%')))
AND (:levels IS NULL OR c.level IN :levels)
AND (:freeFlags IS NULL OR c.free IN :freeFlags)
AND (:categoryName IS NULL OR cat.name = :categoryName OR p.name = :categoryName)
AND (:topicId IS NULL OR t.id = :topicId)
AND c.status = 'PUBLISHED'
AND (
:ratingStar IS NULL OR
(SELECT COALESCE(AVG(r.ratingStar), 0)
FROM Review r
WHERE r.course = c) >= :ratingStar
)
"""
)
Page<Course> findByMultiQueryWithKeyword(
Pageable pageable,
@Param("title") String title,
@Param("ratingStar") Float ratingStar,
@Param("levels") Collection<String> levels,
@Param("freeFlags") Collection<Boolean> freeFlags,
@Param("categoryName") String categoryName,
@Param("topicId") Integer topicId
);



@Query(value = """
select c
from Course c
Expand Down Expand Up @@ -296,4 +356,10 @@ WHERE c.id NOT IN (
""", nativeQuery = true)
List<Course> findCoursesNotInPromotionToday(@Param("promotionId") Long promotionId);



Optional<Course> findByTitle(String title);



}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ PageableData<CourseListGetVM> getCoursesByMultiQuery(int pageNum,
int pageSize,
String title,
Float rating,
String[] level,
Boolean[] free,
List<String> level,
List<Boolean> free,
String categoryName,
Integer topicId
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,15 @@ public PageableData<CourseListGetVM> getCoursesByMultiQuery(int pageNum,
int pageSize,
String title,
Float rating,
String[] level,
Boolean[] free,
List<String> level,
List<Boolean> free,
String categoryName, Integer topicId
) {

log.info("received pageNum: {}, pageSize: {}, title: {}, rating: {}, level: {}, free: {}, categoryName: {}, " +
"topicId: {}", pageNum, pageSize, title, rating, level, free, categoryName, topicId);
Pageable pageable = PageRequest.of(pageNum, pageSize);
Page<Course> coursePage = courseRepositoryCustom.findByMultiFilter(title, rating, level, free, categoryName, topicId, pageable);
Page<Course> coursePage = courseRepository.findByMultiQueryWithKeyword(pageable, title, rating, level, free, categoryName, topicId) ;
List<Course> courses = coursePage.getContent();
List<CourseListGetVM> courseListGetVMS = courses.stream().map(course -> {
course = courseRepository.findByIdWithPromotions(course).orElseThrow(() -> new NotFoundException(Constants.ERROR_CODE.COURSE_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import java.time.LocalDateTime;

@Entity
@Table(name = "review")
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table(name = "review")
public class Review extends AbstractAuditEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import java.util.Set;

@Entity
@Table(name = "topic")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@Table(name = "topic")
public class Topic extends AbstractAuditEntity {

@Id
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spring:
format_sql: 'true'
hibernate:
ddl-auto: update
show-sql: 'true'
show-sql: true
cloudinary:
api:
secret: u3aoCSJzt31lcqOJBsgykwTq81o
Expand Down
Loading