Skip to content

Commit

Permalink
Merge pull request #34 from Central-MakeUs/14-필터를-조회한다
Browse files Browse the repository at this point in the history
Feat(#33): 필터 조회 tag 조건 추가
  • Loading branch information
tmddus2 authored Aug 10, 2024
2 parents b96966a + bfbefd0 commit bae6c9f
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 106 deletions.
26 changes: 26 additions & 0 deletions purithm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ dependencies {
implementation 'io.awspring.cloud:spring-cloud-starter-aws:2.4.4'
implementation 'io.awspring.cloud:spring-cloud-starter-aws-secrets-manager-config:2.4.4'
implementation("software.amazon.awssdk:s3")

implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"

runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.6'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.6'
compileOnly 'org.projectlombok:lombok'
Expand All @@ -45,6 +51,26 @@ dependencies {
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

sourceSets {
main {
java {
srcDirs = ['src/main/java', 'src/main/generated']
}
}
}

tasks.withType(JavaCompile) {
options.annotationProcessorPath = configurations.annotationProcessor
options.generatedSourceOutputDirectory = file('src/main/generated')
}

clean {
delete file('src/main/generated')
}

tasks.named('test') {
useJUnitPlatform()
}



Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.purithm.domain.filter.dto.response.FilterReviewDto;
import com.example.purithm.domain.filter.dto.response.filterDetailValue.IOSFilterDetailDto;
import com.example.purithm.domain.filter.entity.OS;
import com.example.purithm.domain.filter.entity.Tag;
import com.example.purithm.domain.filter.service.FilterService;
import com.example.purithm.global.response.SuccessResponse;

Expand All @@ -25,7 +26,7 @@ public class FilterController implements FilterControllerDocs {
private final FilterService filterService;

@GetMapping
public SuccessResponse<FilterListDto> getFilters(Long id, OS os, String tag, String sortedBy, int page, int size
public SuccessResponse<FilterListDto> getFilters(Long id, OS os, Tag tag, String sortedBy, int page, int size
) {
return SuccessResponse.of(filterService.getFilters(id, page, size, os, tag, sortedBy));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.purithm.domain.filter.dto.response.FilterReviewDto;
import com.example.purithm.domain.filter.dto.response.filterDetailValue.IOSFilterDetailDto;
import com.example.purithm.domain.filter.entity.OS;
import com.example.purithm.domain.filter.entity.Tag;
import com.example.purithm.global.auth.annotation.LoginInfo;
import com.example.purithm.global.response.SuccessResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -22,7 +23,7 @@ public SuccessResponse<FilterListDto> getFilters(
@LoginInfo Long id,
@RequestParam(value = "os", required = true) @Parameter(description = "휴대폰 os",
examples = {@ExampleObject(value = "AOS"), @ExampleObject(value = "iOS")}) OS os,
@RequestParam(value = "tag", required = false) String tag,
@RequestParam(value = "tag", required = false) Tag tag,
@RequestParam(value = "sortedBy", required = false) @Parameter(description = "정렬순",
examples =
{@ExampleObject(name = "최신순", summary = "최신순 정렬", value = "latest"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ public class Filter {
private Membership membership;

private OS os;

private Tag tag;
}
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);
}
}
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);
}
}
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);
}
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);
}
}
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 {
}
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);
}
}

This file was deleted.

Loading

0 comments on commit bae6c9f

Please sign in to comment.