Skip to content

Commit

Permalink
[BSVR-56] QueryDSL 설정 추가 (#13)
Browse files Browse the repository at this point in the history
* fix : 윈도우에서 makeGitHooksExecutable 에러 fix

* build(queryDSL): queryDSL 의존성 추가

* feat: queryDSl configuration 추가

* feat: QueryDSL 샘플 코드 추가

* docs: swagger 수정

* docs: editorconfig 설정에서 indent 4space로 조정

---------

Co-authored-by: wjdwnsdnjs13 <67488973+wjdwnsdnjs13@users.noreply.github.com>
  • Loading branch information
EunjiShin and wjdwnsdnjs13 authored Jul 4, 2024
1 parent f088444 commit 4dd7bfb
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_size = 4
indent_style = space
insert_final_newline = false
max_line_length = 100
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package org.depromeet.spot.application.member.controller;

import java.util.List;

import org.depromeet.spot.application.member.dto.request.MemberRequest;
import org.depromeet.spot.application.member.dto.response.MemberResponse;
import org.depromeet.spot.usecase.port.in.MemberUsecase;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.val;
Expand All @@ -31,4 +36,14 @@ public MemberResponse create(@RequestBody MemberRequest request) {
val member = memberUsecase.create(request.name());
return MemberResponse.from(member);
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "이름으로 Member 조회하는 API")
public List<MemberResponse> findByName(
@RequestParam("name") @Parameter(name = "name", description = "사용자 이름", required = true)
final String name) {
val memberList = memberUsecase.findByName(name);
return memberList.stream().map(MemberResponse::from).toList();
}
}
7 changes: 6 additions & 1 deletion infrastructure/jpa/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ dependencies {
// spring
implementation("org.springframework.boot:spring-boot-starter-data-jpa:_")

// queryDSL
implementation("com.querydsl:querydsl-jpa:_:jakarta")
annotationProcessor("com.querydsl:querydsl-apt:_:jakarta")
annotationProcessor("jakarta.annotation:jakarta.annotation-api")
annotationProcessor("jakarta.persistence:jakarta.persistence-api")

// p6spy
implementation("com.github.gavlyukovskiy:p6spy-spring-boot-starter:_")

// h2 - DB (또는 도커) 세팅 후 사라질 예정,,
runtimeOnly("com.h2database:h2")

}

tasks.bootJar { enabled = false }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.depromeet.spot.jpa.config;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.querydsl.jpa.impl.JPAQueryFactory;

@Configuration
public class QueryDslConfig {

@PersistenceContext private EntityManager entityManager;

@Bean
public JPAQueryFactory queryFactory() {
return new JPAQueryFactory(entityManager);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.depromeet.spot.jpa.member.repository;

import static org.depromeet.spot.jpa.member.entity.QMemberEntity.memberEntity;

import java.util.List;

import org.depromeet.spot.jpa.member.entity.MemberEntity;
import org.springframework.stereotype.Repository;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
public class MemberCustomRepository {

private final JPAQueryFactory queryFactory;

public List<MemberEntity> findByName(final String name) {
return queryFactory.selectFrom(memberEntity).where(eqMemberName(name)).fetch();
}

private BooleanExpression eqMemberName(final String name) {
if (name == null) {
return null;
}
return memberEntity.name.eq(name);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.depromeet.spot.jpa.member.repository;

import java.util.List;

import org.depromeet.spot.domain.member.Member;
import org.depromeet.spot.jpa.member.entity.MemberEntity;
import org.depromeet.spot.usecase.port.out.MemberRepository;
Expand All @@ -13,10 +15,17 @@
public class MemberRepositoryImpl implements MemberRepository {

private final MemberJpaRepository memberJpaRepository;
private final MemberCustomRepository memberCustomRepository;

@Override
public Member save(Member member) {
val memberEntity = memberJpaRepository.save(MemberEntity.from(member));
return memberEntity.toDomain();
}

@Override
public List<Member> findByName(String name) {
val memberEntities = memberCustomRepository.findByName(name);
return memberEntities.stream().map(MemberEntity::toDomain).toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.depromeet.spot.usecase.port.in;

import java.util.List;

import org.depromeet.spot.domain.member.Member;

public interface MemberUsecase {

Member create(String name);

List<Member> findByName(String name);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.depromeet.spot.usecase.port.out;

import java.util.List;

import org.depromeet.spot.domain.member.Member;

public interface MemberRepository {

Member save(Member member);

List<Member> findByName(String name);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.depromeet.spot.usecase.service;

import java.util.List;

import org.depromeet.spot.domain.member.Member;
import org.depromeet.spot.usecase.port.in.MemberUsecase;
import org.depromeet.spot.usecase.port.out.MemberRepository;
Expand All @@ -19,4 +21,9 @@ public Member create(final String name) {
val member = new Member(null, name);
return memberRepository.save(member);
}

@Override
public List<Member> findByName(final String name) {
return memberRepository.findByName(name);
}
}
4 changes: 4 additions & 0 deletions versions.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ version.org.springdoc..springdoc-openapi-starter-webmvc-ui=2.5.0

version.com.github.gavlyukovskiy..p6spy-spring-boot-starter=1.9.0

version.com.querydsl..querydsl-apt=5.0.0

version.com.querydsl..querydsl-jpa=5.0.0

0 comments on commit 4dd7bfb

Please sign in to comment.