Skip to content

Commit

Permalink
mission: submit week6 mission
Browse files Browse the repository at this point in the history
  • Loading branch information
ahtop00 committed Nov 7, 2024
1 parent 12d820b commit 65a8f89
Show file tree
Hide file tree
Showing 16 changed files with 332 additions and 6 deletions.
34 changes: 28 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id 'io.spring.dependency-management' version '1.1.6'
}

group = 'java-lab'
group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
Expand All @@ -17,17 +17,39 @@ repositories {
mavenCentral()
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-validation'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// QueryDsl
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"

}

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
public class Umc7thMissionApplication {

public static void main(String[] args) {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/javalab/umc7th_mission/config/QueryDSLConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package javalab.umc7th_mission.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class QueryDSLConfig {
private final EntityManager em;
@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(em);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package javalab.umc7th_mission.repository.MemberMissionRepository;

import javalab.umc7th_mission.domain.mapping.MemberMission;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberMissionRepository extends JpaRepository<MemberMission, Long>, MemberMissionRepositoryCustom {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package javalab.umc7th_mission.repository.MemberMissionRepository;


import javalab.umc7th_mission.domain.enums.MissionStatus;
import javalab.umc7th_mission.domain.mapping.MemberMission;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface MemberMissionRepositoryCustom {
List<MemberMission> findChallengingMissionByMember(Long memberId, Pageable pageable);
List<MemberMission> findCompletedMissionByMember(Long memberId, Pageable pageable);
Long countCompletedMissionByMember(Long memberId, String regionName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package javalab.umc7th_mission.repository.MemberMissionRepository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import ext.javalab.umc7th_mission.domain.QMember;
import ext.javalab.umc7th_mission.domain.QMission;
import ext.javalab.umc7th_mission.domain.QRegion;
import ext.javalab.umc7th_mission.domain.QStore;
import ext.javalab.umc7th_mission.domain.mapping.QMemberAddress;
import ext.javalab.umc7th_mission.domain.mapping.QMemberMission;
import ext.javalab.umc7th_mission.domain.mapping.QStoreAddress;
import javalab.umc7th_mission.domain.enums.MissionStatus;
import javalab.umc7th_mission.domain.mapping.MemberMission;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class MemberMissionRepositoryImpl implements MemberMissionRepositoryCustom {
private final JPAQueryFactory queryFactory;

//멤버 id를 통해 현재 진행중인 미션을 보여주는 QueryDSL
@Override
public List<MemberMission> findChallengingMissionByMember(Long memberId, Pageable pageable) {
QMemberMission memberMission = QMemberMission.memberMission;

return queryFactory
.selectFrom(memberMission)
.where(
memberMission.member.id.eq(memberId),
memberMission.status.eq(MissionStatus.CHALLENGING)
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
}

//멤버 id를 통해 완료된 미션을 보여주는 QueryDSL
@Override
public List<MemberMission> findCompletedMissionByMember(Long memberId, Pageable pageable) {
QMemberMission memberMission = QMemberMission.memberMission;

return queryFactory
.selectFrom(memberMission)
.where(
memberMission.member.id.eq(memberId),
memberMission.status.eq(MissionStatus.COMPLETE)
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
}


//해당 지역의 완료된 미션의 개수를 보여주는 QueryDSL
@Override
public Long countCompletedMissionByMember(Long memberId, String regionName) {
QMemberMission memberMission = QMemberMission.memberMission;
QStoreAddress storeAddress = QStoreAddress.storeAddress;
QMission mission = QMission.mission;
QStore store = QStore.store;
QRegion region = QRegion.region;

Long regionId = queryFactory
.select(region.id)
.from(region)
.where(region.name.eq(regionName))
.fetchOne();

if (regionId == null) {
return Long.valueOf(0);
}

return queryFactory
.select(memberMission.count())
.from(memberMission)
.join(memberMission.mission, mission)
.join(mission.store, store)
.join(store.addressList, storeAddress)
.where(
memberMission.member.id.eq(memberId),
memberMission.status.eq(MissionStatus.COMPLETE),
storeAddress.region.id.eq(regionId)
)
.fetchOne();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package javalab.umc7th_mission.repository.MemberRepository;

import javalab.umc7th_mission.domain.Member;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberRepository extends JpaRepository<Member, Integer> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package javalab.umc7th_mission.repository.MemberRepository;

import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.web.dto.MemberInfoDto;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MemberRepositoryCustom {
MemberInfoDto findMemberInfoById(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package javalab.umc7th_mission.repository.MemberRepository;


import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import ext.javalab.umc7th_mission.domain.QMember;
import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.web.dto.MemberInfoDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class MemberRepositoryImpl implements MemberRepositoryCustom {
private final JPAQueryFactory queryFactory;

//멤버의 정보를 Dto객체로 보여주는 QueryDSL
@Override
public MemberInfoDto findMemberInfoById(Long memberId) {
QMember member = QMember.member;

return queryFactory
.select(Projections.constructor(MemberInfoDto.class,
member.nickname,
member.email,
member.phoneNumber,
member.point))
.from(member)
.where(member.id.eq(memberId))
.fetchOne();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package javalab.umc7th_mission.repository.ReviewRepository;

import jakarta.transaction.Transactional;
import javalab.umc7th_mission.domain.Review;
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 java.math.BigDecimal;

public interface ReviewRepository extends JpaRepository<Review, Integer> {

@Modifying
@Transactional
@Query(value = "INSERT INTO Review (member_id, store_id, content, rating) VALUES (:memberId, :storeId, :content, :rating)", nativeQuery = true)
void insertReview(@Param("memberId") Long memberId,
@Param("storeId") Long storeId,
@Param("content") String content,
@Param("rating") BigDecimal rating
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package javalab.umc7th_mission.repository.ReviewRepository;

import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.domain.Store;

import java.math.BigDecimal;

public interface ReviewRepositoryCustom {
public Long save(Member member, Store store, String content, BigDecimal rating);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package javalab.umc7th_mission.repository.ReviewRepository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import ext.javalab.umc7th_mission.domain.QReview;
import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.domain.Review;
import javalab.umc7th_mission.domain.Store;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

@Repository
@RequiredArgsConstructor
public class ReviewRepositoryImpl implements ReviewRepositoryCustom {
private final JPAQueryFactory queryFactory;
private final QReview review = QReview.review;

//리뷰 작성 및 저장
@Override
public Long save(Member member, Store store, String content, BigDecimal rating) {
return queryFactory
.insert(review)
.columns(review.member, review.store, review.content, review.rating)
.values(member, store, content, rating)
.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package javalab.umc7th_mission.repository.StoreRepository;

import javalab.umc7th_mission.domain.Store;
import org.springframework.data.jpa.repository.JpaRepository;

public interface StoreRepository extends JpaRepository<Store, Long>, StoreRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package javalab.umc7th_mission.repository.StoreRepository;

import javalab.umc7th_mission.domain.Store;

import java.util.List;

public interface StoreRepositoryCustom {
List<Store> dynamicQueryWithBooleanBuilder(String name, Float score);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package javalab.umc7th_mission.repository.StoreRepository;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import ext.javalab.umc7th_mission.domain.QStore;
import javalab.umc7th_mission.domain.Store;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class StoreRepositoryImpl implements StoreRepositoryCustom {
private final JPAQueryFactory jpaQueryFactory;
private final QStore store = QStore.store;

//예시 QueryDSL?
@Override
public List<Store> dynamicQueryWithBooleanBuilder(String name, Float score) {
BooleanBuilder predicate = new BooleanBuilder();

if (name != null) {
predicate.and(store.name.eq(name));
}

return jpaQueryFactory
.selectFrom(store)
.where(predicate)
.fetch();
}
}
13 changes: 13 additions & 0 deletions src/main/java/javalab/umc7th_mission/web/dto/MemberInfoDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package javalab.umc7th_mission.web.dto;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class MemberInfoDto {
private String nickname;
private String email;
private Integer points;
private String phoneNumber;
}

0 comments on commit 65a8f89

Please sign in to comment.