Add abstract data source
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
2b3a79bd0f
commit
addc40f4bf
@ -0,0 +1,7 @@
|
||||
package allin.data
|
||||
|
||||
abstract class AllInDataSource {
|
||||
abstract val userDataSource: UserDataSource
|
||||
abstract val betDataSource: BetDataSource
|
||||
abstract val participationDataSource: ParticipationDataSource
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package allin.data
|
||||
|
||||
import allin.model.Bet
|
||||
import allin.model.UpdatedBetData
|
||||
|
||||
interface BetDataSource {
|
||||
fun getAllBets(): List<Bet>
|
||||
fun getBetById(id: String): Bet?
|
||||
fun getBetsNotFinished(): List<Bet>
|
||||
fun addBet(bet: Bet)
|
||||
fun removeBet(id: String): Boolean
|
||||
fun updateBet(data: UpdatedBetData): Boolean
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package allin.data
|
||||
|
||||
import allin.model.Participation
|
||||
|
||||
interface ParticipationDataSource {
|
||||
|
||||
fun addParticipation(participation: Participation)
|
||||
fun getParticipationFromBetId(betid: String): List<Participation>
|
||||
fun getParticipationFromUserId(username: String, betid: String): List<Participation>
|
||||
fun deleteParticipation(id: String): Boolean
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package allin.data
|
||||
|
||||
import allin.dto.UserDTO
|
||||
import allin.model.User
|
||||
|
||||
interface UserDataSource {
|
||||
fun getUserByUsername(username: String): Pair<UserDTO?, String?>
|
||||
fun addUser(user: User)
|
||||
fun deleteUser(username: String): Boolean
|
||||
fun modifyUserCoins(username: String, amount: Int)
|
||||
fun userExists(username: String, email: String): Boolean
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package allin.data.mock
|
||||
|
||||
import allin.data.BetDataSource
|
||||
import allin.model.Bet
|
||||
import allin.model.UpdatedBetData
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
class MockBetDataSource : BetDataSource {
|
||||
override fun getAllBets(): List<Bet> = bets
|
||||
override fun getBetById(id: String): Bet? =
|
||||
bets.find { it.id == id }
|
||||
|
||||
override fun removeBet(id: String): Boolean =
|
||||
bets.removeIf { it.id == id }
|
||||
|
||||
override fun updateBet(data: UpdatedBetData): Boolean {
|
||||
return bets.find { it.id == data.id }?.let {
|
||||
it.isPrivate = data.isPrivate
|
||||
} != null
|
||||
}
|
||||
|
||||
override fun getBetsNotFinished(): List<Bet> =
|
||||
bets.filter { it.endBet >= ZonedDateTime.now() }
|
||||
|
||||
override fun addBet(bet: Bet) {
|
||||
bets += bet
|
||||
}
|
||||
|
||||
private val bets by lazy { mutableListOf<Bet>() }
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package allin.data.mock
|
||||
|
||||
import allin.data.AllInDataSource
|
||||
import allin.data.BetDataSource
|
||||
import allin.data.ParticipationDataSource
|
||||
import allin.data.UserDataSource
|
||||
|
||||
class MockDataSource : AllInDataSource() {
|
||||
override val userDataSource: UserDataSource = MockUserDataSource()
|
||||
override val betDataSource: BetDataSource = MockBetDataSource()
|
||||
override val participationDataSource: ParticipationDataSource = MockParticipationDataSource()
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package allin.data.mock
|
||||
|
||||
import allin.data.ParticipationDataSource
|
||||
import allin.model.Participation
|
||||
|
||||
class MockParticipationDataSource : ParticipationDataSource {
|
||||
override fun addParticipation(participation: Participation) {
|
||||
participations += participations
|
||||
}
|
||||
|
||||
override fun getParticipationFromBetId(betid: String): List<Participation> =
|
||||
participations.filter { it.betId == betid }
|
||||
|
||||
override fun getParticipationFromUserId(username: String, betid: String): List<Participation> =
|
||||
participations.filter { it.betId == betid && it.username == username }
|
||||
|
||||
override fun deleteParticipation(id: String): Boolean =
|
||||
participations.removeIf { it.id == id }
|
||||
|
||||
private val participations by lazy { mutableListOf<Participation>() }
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package allin.data.mock
|
||||
|
||||
import allin.data.UserDataSource
|
||||
import allin.dto.UserDTO
|
||||
import allin.model.User
|
||||
|
||||
class MockUserDataSource : UserDataSource {
|
||||
override fun getUserByUsername(username: String): Pair<UserDTO?, String?> =
|
||||
users.find { it.username == username }?.let {
|
||||
Pair(
|
||||
UserDTO(
|
||||
id = it.id,
|
||||
username = it.username,
|
||||
email = it.email,
|
||||
nbCoins = it.nbCoins,
|
||||
token = it.token
|
||||
),
|
||||
it.password
|
||||
)
|
||||
} ?: Pair(null, null)
|
||||
|
||||
override fun addUser(user: User) {
|
||||
users += user
|
||||
}
|
||||
|
||||
override fun deleteUser(username: String): Boolean =
|
||||
users.removeIf { it.username == username }
|
||||
|
||||
override fun modifyUserCoins(username: String, amount: Int) {
|
||||
users.find { it.username == username }?.let {
|
||||
it.nbCoins += amount
|
||||
}
|
||||
}
|
||||
|
||||
override fun userExists(username: String, email: String): Boolean =
|
||||
users.any { it.username == username && it.email == email }
|
||||
|
||||
private val users by lazy {
|
||||
mutableListOf<User>()
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package allin.data.postgres
|
||||
|
||||
import allin.data.BetDataSource
|
||||
import allin.entities.BetsEntity
|
||||
import allin.model.Bet
|
||||
import allin.model.UpdatedBetData
|
||||
import allin.utils.Execute
|
||||
import org.ktorm.database.Database
|
||||
import org.ktorm.dsl.*
|
||||
import java.time.ZoneId
|
||||
import java.time.ZonedDateTime
|
||||
import java.util.*
|
||||
|
||||
class PostgresBetDataSource(private val database: Database) : BetDataSource {
|
||||
|
||||
private fun QueryRowSet.toBet() =
|
||||
Bet(
|
||||
id = this[BetsEntity.id].toString(),
|
||||
theme = this[BetsEntity.theme].toString(),
|
||||
sentenceBet = this[BetsEntity.sentenceBet].toString(),
|
||||
endRegistration = this[BetsEntity.endRegistration]!!.atZone(ZoneId.of("Europe/Paris")),
|
||||
endBet = this[BetsEntity.endBet]!!.atZone(ZoneId.of("Europe/Paris")),
|
||||
isPrivate = this[BetsEntity.isPrivate] ?: false,
|
||||
response = mutableListOf(), // ResponsesEntity.getResponse(UUID.fromString(this[BetsEntity.id].toString())),
|
||||
createdBy = this[BetsEntity.createdBy].toString()
|
||||
)
|
||||
|
||||
private fun Query.mapToBet() = this.map { it.toBet() }
|
||||
|
||||
override fun getAllBets(): List<Bet> =
|
||||
database.from(BetsEntity).select().mapToBet()
|
||||
|
||||
override fun getBetById(id: String): Bet? =
|
||||
database.from(BetsEntity).select().where {
|
||||
BetsEntity.id eq UUID.fromString(id)
|
||||
}.mapToBet().firstOrNull()
|
||||
|
||||
override fun getBetsNotFinished(): List<Bet> {
|
||||
val currentTime = ZonedDateTime.now(ZoneId.of("Europe/Paris"))
|
||||
return database.from(BetsEntity)
|
||||
.select()
|
||||
.where { BetsEntity.endBet greaterEq currentTime.toInstant() }
|
||||
.mapToBet()
|
||||
}
|
||||
|
||||
override fun addBet(bet: Bet) {
|
||||
database.insert(BetsEntity) {
|
||||
set(it.id, UUID.fromString(bet.id))
|
||||
set(it.endBet, bet.endBet.toInstant())
|
||||
set(it.endRegistration, bet.endRegistration.toInstant())
|
||||
set(it.sentenceBet, bet.sentenceBet)
|
||||
set(it.theme, bet.theme)
|
||||
set(it.isPrivate, bet.isPrivate)
|
||||
set(it.createdBy, bet.createdBy)
|
||||
}
|
||||
// ResponsesEntity.addResponse(bet.response, UUID.fromString(bet.id))
|
||||
}
|
||||
|
||||
override fun removeBet(id: String): Boolean {
|
||||
return database.delete(BetsEntity) { it.id eq UUID.fromString(id) } > 0
|
||||
}
|
||||
|
||||
override fun updateBet(data: UpdatedBetData): Boolean {
|
||||
return database.update(BetsEntity) {
|
||||
set(BetsEntity.isPrivate, data.isPrivate)
|
||||
where { BetsEntity.id eq UUID.fromString(data.id) }
|
||||
} > 0
|
||||
}
|
||||
|
||||
fun createBetsTable() {
|
||||
val request =
|
||||
"CREATE TABLE IF not exists bet ( id uuid PRIMARY KEY, theme VARCHAR(255), endregistration timestamp,endbet timestamp,sentencebet varchar(500),isprivate boolean, createdby varchar(250))"
|
||||
database.Execute(request)
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package allin.data.postgres
|
||||
|
||||
import allin.data.AllInDataSource
|
||||
import allin.data.BetDataSource
|
||||
import allin.data.ParticipationDataSource
|
||||
import allin.data.UserDataSource
|
||||
import org.ktorm.database.Database
|
||||
|
||||
class PostgresDataSource : AllInDataSource() {
|
||||
|
||||
private val database: Database
|
||||
|
||||
init {
|
||||
val dbDatabase = System.getenv()["POSTGRES_DB"]
|
||||
val dbUser = System.getenv()["POSTGRES_USER"]
|
||||
val dbPassword = System.getenv()["POSTGRES_PASSWORD"]
|
||||
val dbHost = System.getenv()["POSTGRES_HOST"]
|
||||
|
||||
database = Database.connect(
|
||||
url = "jdbc:postgresql://$dbHost/$dbDatabase",
|
||||
user = dbUser,
|
||||
password = dbPassword
|
||||
)
|
||||
}
|
||||
|
||||
override val userDataSource: UserDataSource = PostgresUserDataSource(database)
|
||||
.also { it.createUserTable() }
|
||||
|
||||
override val betDataSource: BetDataSource = PostgresBetDataSource(database)
|
||||
.also { it.createBetsTable() }
|
||||
|
||||
override val participationDataSource: ParticipationDataSource = PostgresParticipationDataSource(database)
|
||||
.also { it.createParticipationTable() }
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package allin.data.postgres
|
||||
|
||||
import allin.data.ParticipationDataSource
|
||||
import allin.entities.ParticipationsEntity
|
||||
import allin.model.Participation
|
||||
import allin.utils.Execute
|
||||
import org.ktorm.database.Database
|
||||
import org.ktorm.dsl.*
|
||||
import java.util.*
|
||||
|
||||
class PostgresParticipationDataSource(private val database: Database) : ParticipationDataSource {
|
||||
|
||||
private fun QueryRowSet.toParticipation() =
|
||||
Participation(
|
||||
id = this[ParticipationsEntity.id].toString(),
|
||||
betId = this[ParticipationsEntity.betId].toString(),
|
||||
username = this[ParticipationsEntity.username].toString(),
|
||||
answer = this[ParticipationsEntity.answer].toString(),
|
||||
stake = this[ParticipationsEntity.stake] ?: 0,
|
||||
)
|
||||
|
||||
private fun Query.mapToParticipation() = this.map { it.toParticipation() }
|
||||
|
||||
fun createParticipationTable() {
|
||||
val request =
|
||||
"CREATE TABLE IF NOT EXISTS participation (id uuid PRIMARY KEY,bet uuid,username varchar(250),answer varchar(250),stake int);"
|
||||
database.Execute(request)
|
||||
}
|
||||
|
||||
override fun addParticipation(participation: Participation) {
|
||||
database.insert(ParticipationsEntity) {
|
||||
set(it.id, UUID.fromString(participation.id))
|
||||
set(it.betId, UUID.fromString(participation.betId))
|
||||
set(it.username, participation.username)
|
||||
set(it.answer, participation.answer)
|
||||
set(it.stake, participation.stake)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getParticipationFromBetId(betid: String): List<Participation> {
|
||||
return database.from(ParticipationsEntity)
|
||||
.select()
|
||||
.where { ParticipationsEntity.betId eq UUID.fromString(betid) }
|
||||
.mapToParticipation()
|
||||
}
|
||||
|
||||
override fun getParticipationFromUserId(username: String, betid: String): List<Participation> {
|
||||
return database.from(ParticipationsEntity)
|
||||
.select()
|
||||
.where { (ParticipationsEntity.betId eq UUID.fromString(betid)) and (ParticipationsEntity.username eq username) }
|
||||
.mapToParticipation()
|
||||
}
|
||||
|
||||
fun getParticipationEntity(): List<Participation> {
|
||||
return database.from(ParticipationsEntity).select().mapToParticipation()
|
||||
}
|
||||
|
||||
override fun deleteParticipation(id: String): Boolean {
|
||||
return database.delete(ParticipationsEntity) {
|
||||
it.id eq UUID.fromString(id)
|
||||
} > 0
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package allin.data.postgres
|
||||
|
||||
import allin.data.UserDataSource
|
||||
import allin.dto.UserDTO
|
||||
import allin.entities.UsersEntity
|
||||
import allin.model.User
|
||||
import allin.utils.Execute
|
||||
import org.ktorm.database.Database
|
||||
import org.ktorm.dsl.*
|
||||
import java.util.*
|
||||
|
||||
class PostgresUserDataSource(private val database: Database) : UserDataSource {
|
||||
override fun getUserByUsername(username: String): Pair<UserDTO?, String?> =
|
||||
database.from(UsersEntity)
|
||||
.select()
|
||||
.where { UsersEntity.username eq username }
|
||||
.map { row ->
|
||||
Pair(
|
||||
UserDTO(
|
||||
row[UsersEntity.id].toString(),
|
||||
row[UsersEntity.username].toString(),
|
||||
row[UsersEntity.email].toString(),
|
||||
row[UsersEntity.nbCoins] ?: 0,
|
||||
null
|
||||
),
|
||||
row[UsersEntity.password].toString()
|
||||
)
|
||||
}
|
||||
.firstOrNull() ?: Pair(null, null)
|
||||
|
||||
override fun addUser(user: User) {
|
||||
database.insert(UsersEntity) {
|
||||
set(it.id, UUID.fromString(user.id))
|
||||
set(it.nbCoins, user.nbCoins)
|
||||
set(it.username, user.username)
|
||||
set(it.password, user.password)
|
||||
set(it.email, user.email)
|
||||
}
|
||||
}
|
||||
|
||||
override fun deleteUser(username: String): Boolean {
|
||||
val deletedCount = database.delete(UsersEntity) {
|
||||
it.username eq username
|
||||
}
|
||||
return deletedCount > 0
|
||||
}
|
||||
|
||||
override fun userExists(username: String, email: String): Boolean {
|
||||
return database.from(UsersEntity).select(UsersEntity.username, UsersEntity.email).where {
|
||||
(UsersEntity.username eq username) and (UsersEntity.email eq email)
|
||||
}.totalRecords > 0
|
||||
}
|
||||
|
||||
override fun modifyUserCoins(username: String, amount: Int) {
|
||||
val request = "UPDATE utilisateur SET coins = coins - $amount WHERE username = '$username';"
|
||||
database.Execute(request)
|
||||
}
|
||||
|
||||
fun createUserTable() {
|
||||
val request =
|
||||
"CREATE TABLE IF not exists utilisateur ( id uuid PRIMARY KEY, username VARCHAR(255), password VARCHAR(255),coins double precision,email VARCHAR(255))"
|
||||
database.Execute(request)
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue