-
Notifications
You must be signed in to change notification settings - Fork 4
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 #169 from santa-close/feat/kotlin-jdsl-3
kotlin jdsl 3
- Loading branch information
Showing
20 changed files
with
262 additions
and
190 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
61 changes: 32 additions & 29 deletions
61
.../src/main/kotlin/com/santaclose/app/mountain/repository/MountainAppQueryRepositoryImpl.kt
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,54 +1,57 @@ | ||
package com.santaclose.app.mountain.repository | ||
|
||
import arrow.core.Either | ||
import arrow.core.recover | ||
import com.linecorp.kotlinjdsl.querydsl.expression.col | ||
import com.linecorp.kotlinjdsl.querydsl.from.fetch | ||
import com.linecorp.kotlinjdsl.querydsl.from.join | ||
import com.linecorp.kotlinjdsl.spring.data.SpringDataQueryFactory | ||
import com.linecorp.kotlinjdsl.spring.data.listQuery | ||
import com.linecorp.kotlinjdsl.spring.data.singleQuery | ||
import com.linecorp.kotlinjdsl.dsl.jpql.jpql | ||
import com.linecorp.kotlinjdsl.render.jpql.JpqlRenderContext | ||
import com.linecorp.kotlinjdsl.support.spring.data.jpa.extension.createQuery | ||
import com.santaclose.app.mountain.repository.dto.MountainLocationDto | ||
import com.santaclose.lib.entity.location.Location | ||
import com.santaclose.lib.entity.mountain.Mountain | ||
import com.santaclose.lib.entity.mountainRestaurant.MountainRestaurant | ||
import com.santaclose.lib.entity.restaurant.Restaurant | ||
import com.santaclose.lib.web.exception.DomainError.DBFailure | ||
import com.santaclose.lib.web.exception.catchDB | ||
import jakarta.persistence.NoResultException | ||
import jakarta.persistence.criteria.JoinType | ||
import jakarta.persistence.EntityManager | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class MountainAppQueryRepositoryImpl( | ||
private val springDataQueryFactory: SpringDataQueryFactory, | ||
private val entityManager: EntityManager, | ||
private val jpqlRenderContext: JpqlRenderContext, | ||
) : MountainAppQueryRepository { | ||
|
||
override fun findOneWithLocation(id: Long): Either<DBFailure, Mountain?> = | ||
Either.catch { | ||
springDataQueryFactory.singleQuery { | ||
select(entity(Mountain::class)) | ||
from(Mountain::class) | ||
fetch(Mountain::location, JoinType.INNER) | ||
where(col(Mountain::id).equal(id)) | ||
} | ||
}.recover { | ||
if (it is NoResultException) { | ||
null | ||
} else { | ||
raise(DBFailure(it)) | ||
Either.catchDB { | ||
val query = jpql { | ||
select( | ||
entity(Mountain::class), | ||
).from( | ||
entity(Mountain::class), | ||
innerFetchJoin(Mountain::location), | ||
).where( | ||
path(Mountain::id).eq(id), | ||
) | ||
} | ||
|
||
entityManager.createQuery(query, jpqlRenderContext).resultList.firstOrNull() | ||
} | ||
|
||
override fun findLocationByRestaurant(restaurantId: Long): Either<DBFailure, List<MountainLocationDto>> = | ||
Either.catchDB { | ||
springDataQueryFactory.listQuery { | ||
selectMulti(col(Mountain::id), col(Location::point)) | ||
from(Mountain::class) | ||
join(Mountain::mountainRestaurant, JoinType.INNER) | ||
join(MountainRestaurant::restaurant, JoinType.INNER) | ||
join(Mountain::location, JoinType.INNER) | ||
where(col(Restaurant::id).equal(restaurantId)) | ||
val query = jpql { | ||
selectNew<MountainLocationDto>( | ||
path(Mountain::id), | ||
path(Location::point), | ||
).from( | ||
entity(Mountain::class), | ||
innerJoin(Mountain::mountainRestaurant), | ||
innerJoin(MountainRestaurant::restaurant), | ||
innerJoin(Mountain::location), | ||
).where( | ||
path(Restaurant::id).eq(restaurantId), | ||
) | ||
} | ||
|
||
entityManager.createQuery(query, jpqlRenderContext).resultList | ||
} | ||
} |
64 changes: 42 additions & 22 deletions
64
.../santaclose/app/mountainRestaurant/repository/MountainRestaurantAppQueryRepositoryImpl.kt
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,47 +1,67 @@ | ||
package com.santaclose.app.mountainRestaurant.repository | ||
|
||
import arrow.core.Either | ||
import com.linecorp.kotlinjdsl.querydsl.expression.col | ||
import com.linecorp.kotlinjdsl.querydsl.from.join | ||
import com.linecorp.kotlinjdsl.spring.data.SpringDataQueryFactory | ||
import com.linecorp.kotlinjdsl.spring.data.listQuery | ||
import com.linecorp.kotlinjdsl.dsl.jpql.jpql | ||
import com.linecorp.kotlinjdsl.render.jpql.JpqlRenderContext | ||
import com.linecorp.kotlinjdsl.support.spring.data.jpa.extension.createQuery | ||
import com.santaclose.app.mountainRestaurant.repository.dto.LatestMountainDto | ||
import com.santaclose.app.mountainRestaurant.repository.dto.LatestRestaurantDto | ||
import com.santaclose.lib.entity.mountain.Mountain | ||
import com.santaclose.lib.entity.mountainRestaurant.MountainRestaurant | ||
import com.santaclose.lib.entity.restaurant.Restaurant | ||
import com.santaclose.lib.web.exception.DomainError.DBFailure | ||
import com.santaclose.lib.web.exception.catchDB | ||
import jakarta.persistence.criteria.JoinType | ||
import jakarta.persistence.EntityManager | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class MountainRestaurantAppQueryRepositoryImpl( | ||
private val springDataQueryFactory: SpringDataQueryFactory, | ||
private val entityManager: EntityManager, | ||
private val jpqlRenderContext: JpqlRenderContext, | ||
) : MountainRestaurantAppQueryRepository { | ||
override fun findMountainByRestaurant(id: Long, limit: Int): Either<DBFailure, List<LatestMountainDto>> = | ||
Either.catchDB { | ||
springDataQueryFactory.listQuery { | ||
selectMulti(col(Mountain::id), col(Mountain::name)) | ||
from(MountainRestaurant::class) | ||
join(MountainRestaurant::mountain, JoinType.INNER) | ||
join(MountainRestaurant::restaurant, JoinType.INNER) | ||
where(col(Restaurant::id).equal(id)) | ||
orderBy(col(Mountain::id).desc()) | ||
limit(limit) | ||
val query = jpql { | ||
selectNew<LatestMountainDto>( | ||
path(Mountain::id), | ||
path(Mountain::name), | ||
).from( | ||
entity(Mountain::class), | ||
innerJoin(Mountain::mountainRestaurant), | ||
innerJoin(MountainRestaurant::restaurant), | ||
).where( | ||
path(Restaurant::id).eq(id), | ||
).orderBy( | ||
path(Mountain::id).desc(), | ||
) | ||
} | ||
|
||
entityManager | ||
.createQuery(query, jpqlRenderContext) | ||
.apply { maxResults = limit } | ||
.resultList | ||
} | ||
|
||
override fun findRestaurantByMountain(id: Long, limit: Int): Either<DBFailure, List<LatestRestaurantDto>> = | ||
Either.catchDB { | ||
springDataQueryFactory.listQuery { | ||
selectMulti(col(Restaurant::id), col(Restaurant::name)) | ||
from(MountainRestaurant::class) | ||
join(MountainRestaurant::mountain, JoinType.INNER) | ||
join(MountainRestaurant::restaurant, JoinType.INNER) | ||
where(col(Mountain::id).equal(id)) | ||
orderBy(col(Restaurant::id).desc()) | ||
limit(limit) | ||
val query = jpql { | ||
selectNew<LatestRestaurantDto>( | ||
path(Restaurant::id), | ||
path(Restaurant::name), | ||
).from( | ||
entity(MountainRestaurant::class), | ||
innerJoin(MountainRestaurant::mountain), | ||
innerJoin(MountainRestaurant::restaurant), | ||
).where( | ||
path(Mountain::id).eq(id), | ||
).orderBy( | ||
path(Restaurant::id).desc(), | ||
) | ||
} | ||
|
||
entityManager | ||
.createQuery(query, jpqlRenderContext) | ||
.apply { maxResults = limit } | ||
.resultList | ||
} | ||
} |
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
Oops, something went wrong.