-
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.
Move kotlintest to kotest Add KRepository (with suspend functions)
- Loading branch information
Alex Mihailov
committed
Nov 3, 2021
1 parent
5572e5e
commit 8257d70
Showing
19 changed files
with
556 additions
and
73 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
142 changes: 142 additions & 0 deletions
142
api/src/main/kotlin/org/taymyr/play/repository/domain/KRepository.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 |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package org.taymyr.play.repository.domain | ||
|
||
import akka.Done | ||
|
||
/** | ||
* DDD repository for identified aggregate (use coroutines). | ||
*/ | ||
interface KRepository<Aggregate, Identity> { | ||
|
||
/** | ||
* Generate a new identifier. | ||
*/ | ||
fun nextIdentity(): Identity | ||
|
||
/** | ||
* Get aggregate by the identifier. | ||
* @param id Identifier. | ||
* @return aggregate or null if aggregate not exist. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun get(id: Identity): Aggregate? | ||
|
||
/** | ||
* Get all aggregates from the repository. | ||
* @return List of aggregates or an empty list if the repository is empty. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun getAll(): List<Aggregate> | ||
|
||
/** | ||
* Finding aggregates on the repository by their identifiers. | ||
* @param ids List of identifiers. | ||
* @return List of aggregates or an empty list if aggregates not found. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun findByIds(ids: List<Identity>): List<Aggregate> | ||
|
||
/** | ||
* Finding aggregates on the repository by their identifiers. | ||
* @param ids List of identifiers. | ||
* @return List of aggregates or an empty list if aggregates not found. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun findByIds(vararg ids: Identity): List<Aggregate> = findByIds(ids.asList()) | ||
|
||
/** | ||
* Removing aggregate from the repository. | ||
* @param aggregate Aggregate. | ||
* @return [Done] if removing successfully. Otherwise will throw an exception. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun remove(aggregate: Aggregate): Done | ||
|
||
/** | ||
* Removing aggregates from the repository. | ||
* @param aggregates List of aggregates. | ||
* @return [Done] if removing successfully. Otherwise will throw an exception. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun removeAll(aggregates: Collection<Aggregate>): Done | ||
|
||
/** | ||
* Create aggregate on the repository. | ||
* @param aggregate Aggregate. | ||
* @return [Done] if creation successfully. Otherwise will throw an exception. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun create(aggregate: Aggregate): Done | ||
|
||
/** | ||
* Create aggregates on the repository. | ||
* @param aggregates Aggregates. | ||
* @return [Done] if creation successfully. Otherwise will throw an exception. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun createAll(aggregates: Collection<Aggregate>): Done | ||
|
||
/** | ||
* Saving aggregate on the repository. | ||
* @param aggregate Aggregate. | ||
* @return [Done] if saving successfully. Otherwise will throw an exception. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun save(aggregate: Aggregate): Done | ||
|
||
/** | ||
* Saving aggregates on the repository. | ||
* @param aggregates Aggregates. | ||
* @return [Done] if saving successfully. Otherwise will throw an exception. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun saveAll(aggregates: Collection<Aggregate>): Done | ||
|
||
/** | ||
* Finding aggregates on the repository by jpaQuery. | ||
* @param jpaQuery Jpa query. | ||
* @param parameters Map of parameters name and value in jpa query. | ||
* @return List of aggregates or an empty list if aggregates not found. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun findAggregates(jpaQuery: String, parameters: Map<String, Any>): List<Aggregate> | ||
|
||
/** | ||
* Finding aggregates on the repository by jpaQuery and using pagination. | ||
* @param jpaQuery Jpa query. | ||
* @param parameters Map of parameters name and value in jpa query. | ||
* @param offset Offset from the beginning of the list | ||
* @param limit The number of elements in the sample. | ||
* @return List of aggregates or an empty list if aggregates not found. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun findAggregates(jpaQuery: String, parameters: Map<String, Any>, offset: Int, limit: Int): List<Aggregate> | ||
|
||
/** | ||
* Finding aggregate on the repository by jpaQuery. | ||
* @param jpaQuery Jpa query. | ||
* @param parameters Map of parameters name and value in jpa query. | ||
* @return aggregate or null if aggregate not exist. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun findAggregate(jpaQuery: String, parameters: Map<String, Any>): Aggregate? | ||
|
||
/** | ||
* Finding specific values on the repository by jpaQuery. | ||
* @param jpaQuery Jpa query. | ||
* @param parameters Map of parameters name and value in jpa query. | ||
* @param clazz Class of the find value. | ||
* @return List of aggregates or an empty list if aggregates not found. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun <E> findValues(jpaQuery: String, parameters: Map<String, Any>, clazz: Class<E>): List<E> | ||
|
||
/** | ||
* Find specific value on the repository by jpaQuery. | ||
* @param jpaQuery Jpa query. | ||
* @param parameters Map of parameters name and value in jpa query. | ||
* @param clazz Class of the find value. | ||
* @return Specific value or null. | ||
* @throws Exception Any exceptions while execute a query on the database will wrapped. | ||
*/ | ||
suspend fun <E> findValue(jpaQuery: String, parameters: Map<String, Any>, clazz: Class<E>): E? | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
plugins { | ||
plugins { | ||
`kotlin-dsl` | ||
} | ||
|
||
|
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
16 changes: 16 additions & 0 deletions
16
...rc/main/kotlin/org/taymyr/play/repository/infrastructure/persistence/JPABaseRepository.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.taymyr.play.repository.infrastructure.persistence | ||
|
||
import play.db.jpa.JPAApi | ||
import java.io.Serializable | ||
import javax.persistence.EntityManager | ||
|
||
abstract class JPABaseRepository<Aggregate : Any, Identity : Serializable> @JvmOverloads constructor( | ||
protected val jpaApi: JPAApi, | ||
protected val executionContext: DatabaseExecutionContext, | ||
protected val clazz: Class<out Aggregate>, | ||
protected val persistenceUnitName: String = "default" | ||
) { | ||
protected fun <E> transaction(function: (EntityManager) -> E): E = jpaApi.withTransaction(persistenceUnitName, function) | ||
|
||
protected fun <E> readOnly(function: (EntityManager) -> E): E = jpaApi.withTransaction(persistenceUnitName, true, function) | ||
} |
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.