diff --git a/Sources/src/main/kotlin/allin/data/BetDataSource.kt b/Sources/src/main/kotlin/allin/data/BetDataSource.kt index f0c1e03..d29c5ba 100644 --- a/Sources/src/main/kotlin/allin/data/BetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/BetDataSource.kt @@ -19,4 +19,5 @@ interface BetDataSource { fun getCurrent(username: String): List fun getMostPopularBet(): Bet? fun updatePopularityScore(betId: String) + fun addPrivateBet(bet: Bet) } \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt b/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt index 1f757f0..753d3eb 100644 --- a/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt @@ -233,4 +233,8 @@ class MockBetDataSource(private val mockData: MockDataSource.MockData) : BetData bet.popularityscore = score } + override fun addPrivateBet(bet: Bet) { + TODO() + } + } diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt index 58a553a..87554bc 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt @@ -253,5 +253,13 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { } } } - + override fun addPrivateBet(bet: Bet) { + addBet(bet) + bet.userInvited?.forEach{ + database.privatebets.add(PrivateBetEntity{ + betId=bet.id + userId=it + }) + } + } } \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt index 3f3e52a..14cfc49 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt @@ -144,6 +144,17 @@ class PostgresDataSource : AllInDataSource() { ) """.trimIndent() ) + + database.execute( + """ + CREATE TABLE IF NOT EXISTS privatebet + ( + betid VARCHAR(255), + userid VARCHAR(255), + CONSTRAINT pk_privatebet PRIMARY KEY (betid,userid) + ) + """.trimIndent() + ) } override val userDataSource: UserDataSource by lazy { PostgresUserDataSource(database) } diff --git a/Sources/src/main/kotlin/allin/data/postgres/entities/PrivateBetEntity.kt b/Sources/src/main/kotlin/allin/data/postgres/entities/PrivateBetEntity.kt new file mode 100644 index 0000000..3417b6f --- /dev/null +++ b/Sources/src/main/kotlin/allin/data/postgres/entities/PrivateBetEntity.kt @@ -0,0 +1,21 @@ +package allin.data.postgres.entities + +import org.ktorm.database.Database +import org.ktorm.entity.Entity +import org.ktorm.entity.sequenceOf +import org.ktorm.schema.Table +import org.ktorm.schema.varchar + +interface PrivateBetEntity : Entity { + companion object : Entity.Factory() + + var betId: String + var userId: String +} + +object PrivateBetsEntity : Table("privatebet") { + val betid = varchar("betid").bindTo { it.betId } + val userId = varchar("userid").bindTo { it.userId } +} + +val Database.privatebets get() = this.sequenceOf(PrivateBetsEntity) \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/model/Bet.kt b/Sources/src/main/kotlin/allin/model/Bet.kt index e48a6d6..cd9353b 100644 --- a/Sources/src/main/kotlin/allin/model/Bet.kt +++ b/Sources/src/main/kotlin/allin/model/Bet.kt @@ -22,7 +22,8 @@ data class Bet( val createdBy: String = "", var popularityscore: Int = 0, val totalStakes: Int = 0, - val totalParticipants: Int = 0 + val totalParticipants: Int = 0, + val userInvited: List? = null ) @Serializable diff --git a/Sources/src/main/kotlin/allin/routing/betRouter.kt b/Sources/src/main/kotlin/allin/routing/betRouter.kt index a3e4a72..01ff733 100644 --- a/Sources/src/main/kotlin/allin/routing/betRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/betRouter.kt @@ -57,7 +57,10 @@ fun Application.betRouter() { call.respond(HttpStatusCode.Conflict, ApiMessage.BET_ALREADY_EXIST) } ?: run { val betWithId = bet.copy(id = id, createdBy = user.first?.username.toString()) - betDataSource.addBet(betWithId) + + if(bet.isPrivate && bet.userInvited?.isNotEmpty() == true){ + betDataSource.addPrivateBet(betWithId) + } else betDataSource.addBet(betWithId) logManager.log("Routing","CREATED /bets/add\t${betWithId}") call.respond(HttpStatusCode.Created, betWithId) } @@ -330,7 +333,7 @@ fun Application.betRouter() { } } }) { - logManager.log("Routing","GET /bets/confirm/{id}") + logManager.log("Routing", "GET /bets/confirm/{id}") hasToken { principal -> verifyUserFromToken(userDataSource, principal) { user, _ -> val betId = call.parameters["id"] ?: "" @@ -338,10 +341,10 @@ fun Application.betRouter() { if (betDataSource.getBetById(betId)?.createdBy == user.username) { betDataSource.confirmBet(betId, result) - logManager.log("Routing","ACCEPTED /bets/confirm/{id}") + logManager.log("Routing", "ACCEPTED /bets/confirm/{id}") call.respond(HttpStatusCode.OK) } else { - logManager.log("Routing","UNAUTHORIZED /bets/confirm/{id}") + logManager.log("Routing", "UNAUTHORIZED /bets/confirm/{id}") call.respond(HttpStatusCode.Unauthorized) }