-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[#23] 내 방문기록 조회 API 추가
- Loading branch information
Showing
16 changed files
with
366 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.dnd.dndtravel.config; | ||
|
||
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 JpaConfig { | ||
|
||
@PersistenceContext | ||
private EntityManager em; | ||
|
||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(em); | ||
} | ||
} |
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
10 changes: 7 additions & 3 deletions
10
src/main/java/com/dnd/dndtravel/map/repository/MemberAttractionRepository.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,11 +1,15 @@ | ||
package com.dnd.dndtravel.map.repository; | ||
|
||
import java.util.Optional; | ||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.dnd.dndtravel.map.domain.MemberAttraction; | ||
import com.dnd.dndtravel.map.repository.custom.MemberAttractionRepositoryCustom; | ||
|
||
public interface MemberAttractionRepository extends JpaRepository<MemberAttraction, Long> { | ||
MemberAttraction findByAttractionIdAndMemberId(Long attractionId, Long memberId); | ||
public interface MemberAttractionRepository extends JpaRepository<MemberAttraction, Long>, | ||
MemberAttractionRepositoryCustom { | ||
|
||
List<MemberAttraction> findByMemberId(long memberId); | ||
} | ||
|
7 changes: 6 additions & 1 deletion
7
src/main/java/com/dnd/dndtravel/map/repository/PhotoRepository.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,9 +1,14 @@ | ||
package com.dnd.dndtravel.map.repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.dnd.dndtravel.map.domain.Photo; | ||
import com.dnd.dndtravel.map.repository.custom.PhotoRepositoryCustom; | ||
|
||
public interface PhotoRepository extends JpaRepository<Photo, Long> { | ||
public interface PhotoRepository extends JpaRepository<Photo, Long>, PhotoRepositoryCustom { | ||
|
||
List<Photo> findByMemberAttractionId(Long id); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/dnd/dndtravel/map/repository/custom/MemberAttractionRepositoryCustom.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,12 @@ | ||
package com.dnd.dndtravel.map.repository.custom; | ||
|
||
import java.util.List; | ||
|
||
import com.dnd.dndtravel.map.repository.dto.projection.AttractionPhotoProjection; | ||
import com.dnd.dndtravel.map.repository.dto.projection.RecordProjection; | ||
|
||
public interface MemberAttractionRepositoryCustom { | ||
Long maxCursor(long memberId); | ||
|
||
List<RecordProjection> findAttractionRecords(long memberId, long cursorNo, int displayPerPage); | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/dnd/dndtravel/map/repository/custom/MemberAttractionRepositoryImpl.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,67 @@ | ||
package com.dnd.dndtravel.map.repository.custom; | ||
|
||
import static com.dnd.dndtravel.map.domain.QAttraction.attraction; | ||
import static com.dnd.dndtravel.map.domain.QMemberAttraction.memberAttraction; | ||
import static com.dnd.dndtravel.member.domain.QMember.member; | ||
|
||
import java.util.List; | ||
|
||
import com.dnd.dndtravel.map.repository.dto.projection.RecordProjection; | ||
import com.querydsl.core.types.ExpressionUtils; | ||
import com.querydsl.core.types.Projections; | ||
import com.querydsl.core.types.dsl.Expressions; | ||
import com.querydsl.core.types.dsl.NumberPath; | ||
import com.querydsl.jpa.JPAExpressions; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class MemberAttractionRepositoryImpl implements MemberAttractionRepositoryCustom { | ||
|
||
private final JPAQueryFactory jpaQueryFactory; | ||
private final NumberPath<Long> entireRecordCount = Expressions.numberPath(Long.class, "entireRecordCount"); | ||
|
||
/** | ||
* select max(ma.id) | ||
* from memberAttraction ma | ||
*/ | ||
@Override | ||
public Long maxCursor(long memberId) { | ||
return jpaQueryFactory.select(memberAttraction.id.max()) | ||
.from(memberAttraction) | ||
.fetchOne(); | ||
} | ||
|
||
/** | ||
* select * | ||
* from memberAttraction ma | ||
* join member m on ma.memberId = m.id | ||
* where {cursor.no} < ma.id | ||
* order by ma.id desc | ||
* limit {displayPerPage} | ||
*/ | ||
|
||
@Override | ||
public List<RecordProjection> findAttractionRecords(long memberId, long cursorNo, int displayPerPage) { | ||
return jpaQueryFactory.select(Projections.constructor(RecordProjection.class, | ||
memberAttraction.id, | ||
ExpressionUtils.as(JPAExpressions | ||
.select(memberAttraction.id.count()) | ||
.from(memberAttraction) | ||
.where(memberAttraction.member.id.eq(member.id)), entireRecordCount | ||
), | ||
memberAttraction.memo, | ||
memberAttraction.localDate, | ||
memberAttraction.region, | ||
attraction | ||
)) | ||
.from(memberAttraction) | ||
.join(member).on(memberAttraction.member.id.eq(member.id)) | ||
.join(attraction).on(memberAttraction.attraction.id.eq(attraction.id)) | ||
.where(memberAttraction.id.lt(cursorNo)) | ||
.orderBy(memberAttraction.id.desc()) | ||
.limit(displayPerPage) | ||
.fetch(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/dnd/dndtravel/map/repository/custom/PhotoRepositoryCustom.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,11 @@ | ||
package com.dnd.dndtravel.map.repository.custom; | ||
|
||
import java.util.List; | ||
|
||
import com.dnd.dndtravel.map.repository.dto.projection.AttractionPhotoProjection; | ||
import com.dnd.dndtravel.map.repository.dto.projection.RecordProjection; | ||
|
||
public interface PhotoRepositoryCustom { | ||
|
||
List<AttractionPhotoProjection> findByRecordDtos(List<RecordProjection> dtos); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/dnd/dndtravel/map/repository/custom/PhotoRepositoryImpl.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,35 @@ | ||
package com.dnd.dndtravel.map.repository.custom; | ||
|
||
import static com.dnd.dndtravel.map.domain.QMemberAttraction.memberAttraction; | ||
import static com.dnd.dndtravel.map.domain.QPhoto.photo; | ||
|
||
import java.util.List; | ||
|
||
import com.dnd.dndtravel.map.repository.dto.projection.AttractionPhotoProjection; | ||
import com.dnd.dndtravel.map.repository.dto.projection.RecordProjection; | ||
import com.querydsl.core.types.Projections; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class PhotoRepositoryImpl implements PhotoRepositoryCustom { | ||
|
||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
@Override | ||
public List<AttractionPhotoProjection> findByRecordDtos(List<RecordProjection> recordDtos) { | ||
List<Long> memberAttractionIds = recordDtos.stream() | ||
.map(RecordProjection::getMemberAttractionId) | ||
.toList(); | ||
|
||
return jpaQueryFactory.select( | ||
Projections.constructor(AttractionPhotoProjection.class, | ||
memberAttraction.id, | ||
photo.url)) | ||
.from(photo) | ||
.join(photo.memberAttraction, memberAttraction) | ||
.where(photo.memberAttraction.id.in(memberAttractionIds)) | ||
.fetch(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/dnd/dndtravel/map/repository/dto/projection/AttractionPhotoProjection.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,7 @@ | ||
package com.dnd.dndtravel.map.repository.dto.projection; | ||
|
||
public record AttractionPhotoProjection( | ||
long memberAttractionId, | ||
String photoUrl | ||
) { | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/dnd/dndtravel/map/repository/dto/projection/RecordProjection.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,47 @@ | ||
package com.dnd.dndtravel.map.repository.dto.projection; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import com.dnd.dndtravel.map.domain.Attraction; | ||
|
||
import lombok.Getter; | ||
|
||
/** | ||
* record로 설계하려 했으나, photoUrls를 뒤늦게 주입시켜줘야하는 상황이므로 class로 설계 | ||
*/ | ||
@Getter | ||
public class RecordProjection { | ||
private final long memberAttractionId; | ||
private final long entireRecordCount; | ||
private String attractionName; | ||
private final String memo; | ||
private final LocalDate visitDate; | ||
private final String region; | ||
private final Attraction attraction; | ||
private List<String> photoUrls; | ||
|
||
public RecordProjection(long memberAttractionId, long entireRecordCount, String memo, | ||
LocalDate visitDate, String region, Attraction attraction) { | ||
this.memberAttractionId = memberAttractionId; | ||
this.entireRecordCount = entireRecordCount; | ||
this.memo = memo; | ||
this.visitDate = visitDate; | ||
this.region = region; | ||
this.attraction = attraction; | ||
} | ||
|
||
public void inputPhotoUrls(List<AttractionPhotoProjection> attractionPhotoProjections) { | ||
if (attractionPhotoProjections != null) { | ||
this.photoUrls = attractionPhotoProjections.stream() | ||
.map(AttractionPhotoProjection::photoUrl) | ||
.filter(Objects::nonNull) | ||
.toList(); | ||
} | ||
} | ||
|
||
public void inputAttractionNames() { | ||
this.attractionName = this.attraction.getName(); | ||
} | ||
} |
Oops, something went wrong.