Skip to content

Commit

Permalink
Nick feature/cr team (#16)
Browse files Browse the repository at this point in the history
* basics for team #12

* basics for team creation

* fix date issue for team table and add default empty param

* implement get all teams

* single retrieval of team

* update relationship to channel

* re-write get for abstract class and implement base table
and basic updatebyId for abstract class

* update based on review

* renamed some stuff and remove a space
  • Loading branch information
bote795 authored Jun 21, 2018
1 parent 824bab6 commit faeb09f
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 10 deletions.
7 changes: 2 additions & 5 deletions src/main/kotlin/DatabaseFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +20,7 @@ object DatabaseFactory {
fun init() {
Database.connect(hikari())
transaction {
create(User, Channel, Message, ChannelSubscription)
create(User, Channel, Message, ChannelSubscription, Team)
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/Locations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "")
}
10 changes: 6 additions & 4 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -74,6 +74,8 @@ fun Application.mainModule() {
getAllSubscriptions()
createChannelSubscription()
getAllUsersForChannel()
createTeam()
getTeam()
location<Manual> {
authenticate("kchatAuth1") {
get {
Expand Down
33 changes: 33 additions & 0 deletions src/main/kotlin/Stores/TeamStore.kt
Original file line number Diff line number Diff line change
@@ -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>(Team) {
suspend fun createTeam(params: Parameters): String? {
return create {
it.first[name] = params["name"]!!
}

}
suspend fun getAll() : List <TeamObj>{
return getAll {
TeamObj(
it[id],
it[name],
it[createdAt].toString(),
it[updateAt].toString()
)
}
}
}
49 changes: 49 additions & 0 deletions src/main/kotlin/Stores/baseStore.kt
Original file line number Diff line number Diff line change
@@ -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<T : BaseTable>(val model: T){
suspend fun create(callback: (Pair<InsertStatement<Number>, 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 <L>getAll(createJsonPojo: (items: ResultRow) -> L) : List <L>{
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)
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/models/Channel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/models/Team.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package models

import org.jetbrains.exposed.sql.Table


object Team : BaseTable() {
val name = varchar("name", 30)
}
9 changes: 9 additions & 0 deletions src/main/kotlin/models/baseTable.kt
Original file line number Diff line number Diff line change
@@ -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")
}
48 changes: 48 additions & 0 deletions src/main/kotlin/routing/TeamRouting.kt
Original file line number Diff line number Diff line change
@@ -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<T>(val status: String, val reason: String, val data: List<T>?)
data class CreateResponse(val status: String, val reason: String, val id: String?)

object TeamRouting{
fun Route.createTeam() {
post<Locations.Teams> {
val params = call.receive<Parameters>()
Logger.logMsg(Logger.INFO, "Create a Team ")
val requiredParams = listOf("name")
val missingFields: List<String> =
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<Locations.Teams>{
val teams= TeamStore.getAll();
call.respond(ListResponse<TeamObj>("success", "Successfully retrieved all Teams", teams))
}
}
fun Route.getTeam(){
get<Locations.Team>{
val uuid = call.parameters["uuid"]
val team = TeamStore.get(uuid)
call.respond(CreateResponse("success", "Successfully retrieved a channel", team))
}
}
}

0 comments on commit faeb09f

Please sign in to comment.