diff --git a/src/main/kotlin/DatabaseFactory.kt b/src/main/kotlin/DatabaseFactory.kt index 2c64bc6..58fa00e 100644 --- a/src/main/kotlin/DatabaseFactory.kt +++ b/src/main/kotlin/DatabaseFactory.kt @@ -2,10 +2,7 @@ import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource import kotlinx.coroutines.experimental.newFixedThreadPoolContext import kotlinx.coroutines.experimental.withContext -import models.Channel -import models.ChannelSubscription -import models.Message -import models.User +import models.* import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.SchemaUtils.create import org.jetbrains.exposed.sql.transactions.transaction @@ -23,7 +20,7 @@ object DatabaseFactory { fun init() { Database.connect(hikari()) transaction { - create(User, Channel, Message, ChannelSubscription) + create(User, Channel, Message, ChannelSubscription, Team) } } diff --git a/src/main/kotlin/Locations.kt b/src/main/kotlin/Locations.kt index 2f273f6..46be1f1 100644 --- a/src/main/kotlin/Locations.kt +++ b/src/main/kotlin/Locations.kt @@ -27,4 +27,10 @@ class Locations { @Location("/subscriptions") data class GetAllSubscriptions(val test: String = "") + + @Location("/teams") + data class Teams(val name: String = "") + + @Location("/teams/{uuid}") + data class Team(val uuid: String = "") } \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 8b73b4f..98b028c 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -12,7 +12,6 @@ import io.ktor.locations.Location import io.ktor.locations.Locations import io.ktor.locations.location import io.ktor.response.respond -import io.ktor.response.respondText import io.ktor.routing.get import io.ktor.routing.routing import io.ktor.server.engine.embeddedServer @@ -21,14 +20,15 @@ import io.ktor.sessions.SessionTransportTransformerMessageAuthentication import io.ktor.sessions.Sessions import io.ktor.sessions.cookie import io.ktor.util.hex -import routing.CreateUserResponse -import routing.UserRouting.register import routing.ChannelRouting.createChannel -import routing.ChannelRouting.getChannel import routing.ChannelRouting.getAllUsersForChannel +import routing.ChannelRouting.getChannel import routing.ChannelSubscriptionRouting.createChannelSubscription import routing.ChannelSubscriptionRouting.getAllSubscriptions import routing.UserRouting.getUsers +import routing.UserRouting.register +import routing.TeamRouting.createTeam +import routing.TeamRouting.getTeam import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec @@ -74,6 +74,8 @@ fun Application.mainModule() { getAllSubscriptions() createChannelSubscription() getAllUsersForChannel() + createTeam() + getTeam() location { authenticate("kchatAuth1") { get { diff --git a/src/main/kotlin/Stores/TeamStore.kt b/src/main/kotlin/Stores/TeamStore.kt new file mode 100644 index 0000000..7e08852 --- /dev/null +++ b/src/main/kotlin/Stores/TeamStore.kt @@ -0,0 +1,33 @@ +package Stores + +import io.ktor.http.Parameters +import models.Team +import models.Team.createdAt +import models.Team.id +import models.Team.name +import models.Team.updateAt + +data class TeamObj ( + var id: String, + var name: String, + var createdAt: String, + var updateAt: String +) +object TeamStore : BaseStore(Team) { + suspend fun createTeam(params: Parameters): String? { + return create { + it.first[name] = params["name"]!! + } + + } + suspend fun getAll() : List { + return getAll { + TeamObj( + it[id], + it[name], + it[createdAt].toString(), + it[updateAt].toString() + ) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/Stores/baseStore.kt b/src/main/kotlin/Stores/baseStore.kt new file mode 100644 index 0000000..35460e5 --- /dev/null +++ b/src/main/kotlin/Stores/baseStore.kt @@ -0,0 +1,49 @@ +package Stores + +import DatabaseFactory +import models.BaseTable +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.statements.InsertStatement +import org.jetbrains.exposed.sql.statements.UpdateStatement +import org.joda.time.DateTime +import java.util.* + +abstract class BaseStore(val model: T){ + suspend fun create(callback: (Pair, String>) -> Unit): String? { + val uuid = UUID.randomUUID().toString() + DatabaseFactory.dbQuery { + model.insert{ + //TODO undo this Pair couldn't figure out a different way + callback(Pair(it, uuid)) + it[id] = uuid + it[createdAt] = DateTime.now() + it[updateAt] = DateTime.now() + + } + } + return get(uuid) + } + suspend fun get(uuid: String?): String? { + return DatabaseFactory.dbQuery { + model.select { + model.id.eq(uuid!!) + }.takeIf { !it.empty() }?.map { it[model.id] }?.get(0) + } + } + + suspend fun getAll(createJsonPojo: (items: ResultRow) -> L) : List { + return DatabaseFactory.dbQuery { + model.selectAll().map { + return@map createJsonPojo(it) + } + } + } + suspend fun updateById(id : String , valueToUpdate: (x : UpdateStatement) -> UpdateStatement){ + return DatabaseFactory.dbQuery { + model.update({ model.id eq id!! }) + { + valueToUpdate(it) + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/models/Channel.kt b/src/main/kotlin/models/Channel.kt index a9a69a9..aee2bb5 100644 --- a/src/main/kotlin/models/Channel.kt +++ b/src/main/kotlin/models/Channel.kt @@ -18,7 +18,7 @@ object Channel : Table() { // payload defined val creatorId = varchar("creatorId", 36) - val teamId = varchar("teamId", 36) + val teamId = (varchar("teamId", 36) references Team.id) val type = varchar("type", 10) // public, private, dm val displayName = varchar("displayName", 30) val name = varchar("name", 30) diff --git a/src/main/kotlin/models/Team.kt b/src/main/kotlin/models/Team.kt new file mode 100644 index 0000000..29ccb95 --- /dev/null +++ b/src/main/kotlin/models/Team.kt @@ -0,0 +1,8 @@ +package models + +import org.jetbrains.exposed.sql.Table + + +object Team : BaseTable() { + val name = varchar("name", 30) +} \ No newline at end of file diff --git a/src/main/kotlin/models/baseTable.kt b/src/main/kotlin/models/baseTable.kt new file mode 100644 index 0000000..cf8c3a7 --- /dev/null +++ b/src/main/kotlin/models/baseTable.kt @@ -0,0 +1,9 @@ +package models + +import org.jetbrains.exposed.sql.Table + +abstract class BaseTable : Table() { + val id = varchar("id", 36).primaryKey() + val createdAt = date("createdAt") + val updateAt = date("updateAt") +} \ No newline at end of file diff --git a/src/main/kotlin/routing/TeamRouting.kt b/src/main/kotlin/routing/TeamRouting.kt new file mode 100644 index 0000000..d809e7c --- /dev/null +++ b/src/main/kotlin/routing/TeamRouting.kt @@ -0,0 +1,48 @@ +package routing + +import Locations +import Stores.TeamObj +import Stores.TeamStore +import com.sun.media.jfxmedia.logging.Logger +import io.ktor.application.call +import io.ktor.http.Parameters +import io.ktor.locations.get +import io.ktor.locations.post +import io.ktor.request.receive +import io.ktor.response.respond +import io.ktor.routing.Route + +data class ListResponse(val status: String, val reason: String, val data: List?) +data class CreateResponse(val status: String, val reason: String, val id: String?) + +object TeamRouting{ + fun Route.createTeam() { + post { + val params = call.receive() + Logger.logMsg(Logger.INFO, "Create a Team ") + val requiredParams = listOf("name") + val missingFields: List = + requiredParams.filter { + params[it].isNullOrBlank() + } + if (missingFields.isNotEmpty()) { + val response = missingFields.joinToString(separator = ", ") + call.respond(CreateResponse("failed", "Missing Fields: $response", null)) + } else { + val teamId: String? = TeamStore.createTeam(params) + call.respond(CreateResponse("success", "Successfully created a Team", teamId)) + } + } + get{ + val teams= TeamStore.getAll(); + call.respond(ListResponse("success", "Successfully retrieved all Teams", teams)) + } + } + fun Route.getTeam(){ + get{ + val uuid = call.parameters["uuid"] + val team = TeamStore.get(uuid) + call.respond(CreateResponse("success", "Successfully retrieved a channel", team)) + } + } +} \ No newline at end of file