-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from Central-MakeUs/14-필터를-조회한다
Feat(#33): 필터 조회 tag 조건 추가
- Loading branch information
Showing
14 changed files
with
256 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,4 +52,6 @@ public class Filter { | |
private Membership membership; | ||
|
||
private OS os; | ||
|
||
private Tag tag; | ||
} |
13 changes: 13 additions & 0 deletions
13
purithm/src/main/java/com/example/purithm/domain/filter/entity/StringToTagConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.purithm.domain.filter.entity; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.stereotype.Component; | ||
|
||
|
||
@Component | ||
public class StringToTagConverter implements Converter<String, Tag> { | ||
@Override | ||
public Tag convert(String source) { | ||
return Tag.fromValue(source); | ||
} | ||
} |
35 changes: 20 additions & 15 deletions
35
purithm/src/main/java/com/example/purithm/domain/filter/entity/Tag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
package com.example.purithm.domain.filter.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.AllArgsConstructor; | ||
|
||
@Entity | ||
public class Tag { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@AllArgsConstructor | ||
public enum Tag { | ||
SPRING("봄"), | ||
SUMMER("여름"), | ||
FALL("가을"), | ||
WINTER("겨울"), | ||
BACKLIGHT("역광에서"), | ||
NIGHT("night"), | ||
DAILY("daily"); | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "filter_id") | ||
private Filter filter; | ||
private final String value; | ||
|
||
private String tag; | ||
public static Tag fromValue(String value) { | ||
for (Tag tag : Tag.values()) { | ||
if (tag.value.equals(value)) { | ||
return tag; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException("Unknown Tag value: " + value); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...hm/src/main/java/com/example/purithm/domain/filter/repository/CustomFilterRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.purithm.domain.filter.repository; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import com.example.purithm.domain.filter.entity.Filter; | ||
import com.example.purithm.domain.filter.entity.OS; | ||
import com.example.purithm.domain.filter.entity.Tag; | ||
|
||
public interface CustomFilterRepository { | ||
Page<Filter> findAllByOs(OS os, Tag tag, Pageable pageable); | ||
Page<Object[]> findAllWithLikeSorting(OS os, Tag tag, Pageable pageable); | ||
Page<Object[]> findAllWithReviewSorting(OS os, Tag tag, Pageable pageable); | ||
} |
119 changes: 119 additions & 0 deletions
119
...rc/main/java/com/example/purithm/domain/filter/repository/CustomFilterRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.example.purithm.domain.filter.repository; | ||
|
||
import static com.example.purithm.domain.filter.entity.QFilter.*; | ||
import static com.example.purithm.domain.filter.entity.QFilterLike.*; | ||
import static com.example.purithm.domain.review.entity.QReview.*; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.example.purithm.domain.filter.entity.Filter; | ||
import com.example.purithm.domain.filter.entity.OS; | ||
import com.example.purithm.domain.filter.entity.Tag; | ||
import com.querydsl.core.BooleanBuilder; | ||
import com.querydsl.core.Tuple; | ||
import com.querydsl.core.types.dsl.Expressions; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class CustomFilterRepositoryImpl implements CustomFilterRepository { | ||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
@Override | ||
public Page<Filter> findAllByOs(OS os, Tag tag, Pageable pageable) { | ||
BooleanBuilder builder = new BooleanBuilder(); | ||
builder.and(filter.os.eq(os)); | ||
if (tag != null) { | ||
builder.and(filter.tag.eq(tag)); | ||
} | ||
|
||
List<Filter> results = jpaQueryFactory | ||
.selectFrom(filter) | ||
.where(builder) | ||
.orderBy(filter.createdAt.desc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
|
||
Long total = jpaQueryFactory | ||
.select(filter.count()) | ||
.from(filter) | ||
.where(filter.os.eq(os)).fetchOne(); | ||
|
||
return new PageImpl<>(results, pageable, total); | ||
} | ||
|
||
@Override | ||
public Page<Object[]> findAllWithLikeSorting(OS os, Tag tag, Pageable pageable) { | ||
BooleanBuilder builder = new BooleanBuilder(); | ||
builder.and(filter.os.eq(os)); | ||
if (tag != null) { | ||
builder.and(filter.tag.eq(tag)); | ||
} | ||
|
||
List<Tuple> tuples = jpaQueryFactory | ||
.select(filter, filterLike.count()) | ||
.from(filter) | ||
.leftJoin(filterLike).on(filter.id.eq(filterLike.filter.id)) | ||
.where(builder) | ||
.groupBy(filter.id) | ||
.orderBy(filterLike.count().desc(), filter.id.asc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
|
||
long total = jpaQueryFactory | ||
.select(filter.count()) | ||
.from(filter) | ||
.leftJoin(filterLike).on(filter.id.eq(filterLike.filter.id)) | ||
.where(builder) | ||
.fetchOne(); | ||
|
||
List<Object[]> results = tuples.stream() | ||
.map(tuple -> new Object[]{tuple.get(filter), tuple.get(filterLike.count())}) | ||
.collect(Collectors.toList()); | ||
|
||
return new PageImpl<>(results, pageable, total); | ||
} | ||
|
||
@Override | ||
public Page<Object[]> findAllWithReviewSorting(OS os, Tag tag, Pageable pageable) { | ||
BooleanBuilder builder = new BooleanBuilder(); | ||
builder.and(filter.os.eq(os)); | ||
if (tag != null) { | ||
builder.and(filter.tag.eq(tag)); | ||
} | ||
|
||
List<Tuple> tuples = jpaQueryFactory | ||
.select(filter, review.pureDegree.avg().as("avg")) | ||
.from(filter) | ||
.leftJoin(review).on(filter.id.eq(review.filter.id)) | ||
.where(builder) | ||
.groupBy(filter.id) | ||
.orderBy(review.pureDegree.avg().desc(), filter.id.asc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
|
||
long total = jpaQueryFactory | ||
.select(filter.count()) | ||
.from(filter) | ||
.leftJoin(review).on(filter.id.eq(review.filter.id)) | ||
.where(builder) | ||
.fetchOne(); | ||
|
||
List<Object[]> results = tuples.stream() | ||
.map(tuple -> new Object[]{tuple.get(filter), tuple.get(Expressions.stringPath("avg"))}) | ||
.collect(Collectors.toList()); | ||
|
||
return new PageImpl<>(results, pageable, total); | ||
} | ||
} |
24 changes: 1 addition & 23 deletions
24
purithm/src/main/java/com/example/purithm/domain/filter/repository/FilterRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,10 @@ | ||
package com.example.purithm.domain.filter.repository; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.example.purithm.domain.filter.entity.Filter; | ||
import com.example.purithm.domain.filter.entity.OS; | ||
|
||
@Repository | ||
public interface FilterRepository extends JpaRepository<Filter, Long> { | ||
@Query("SELECT f FROM Filter f WHERE f.os=:os ORDER BY f.createdAt DESC") | ||
Page<Filter> findAllByOs(OS os, Pageable pageable); | ||
@Query("SELECT f, COUNT(fl.id) AS like_count " + | ||
"FROM Filter f " + | ||
"LEFT JOIN FilterLike fl ON f.id = fl.filter.id " + | ||
"WHERE f.os = :os " + | ||
"GROUP BY f.id " + | ||
"ORDER BY like_count DESC, f.id ASC") | ||
Page<Object[]> findAllWithLikeSorting(OS os, Pageable pageable); | ||
|
||
@Query("SELECT f, AVG(r.pureDegree) AS avg " + | ||
"FROM Filter f " + | ||
"LEFT JOIN Review r ON f.id = r.filter.id " + | ||
"WHERE f.os=:os " + | ||
"GROUP BY f.id " + | ||
"ORDER BY avg DESC, f.id ASC") | ||
Page<Object[]> findAllWithReviewSorting(OS os, Pageable pageable); | ||
|
||
public interface FilterRepository extends JpaRepository<Filter, Long>, CustomFilterRepository { | ||
} |
21 changes: 21 additions & 0 deletions
21
purithm/src/main/java/com/example/purithm/domain/filter/repository/QuerydslConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.example.purithm.domain.filter.repository; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
|
||
@Configuration | ||
public class QuerydslConfig { | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(entityManager); | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
purithm/src/main/java/com/example/purithm/domain/filter/repository/TagRepository.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.