Skip to content

Commit

Permalink
some unused library cleanup and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
cemore2048 committed Aug 5, 2018
1 parent faeb09f commit 59f59c9
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ build/
out/
.gradle/
data/

hikari.properties
4 changes: 2 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ 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 routing.UserRouting.getUsers
import routing.UserRouting.register
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import org.jetbrains.exposed.sql.statements.UpdateStatement
import org.joda.time.DateTime
import java.util.*

abstract class BaseStore<T : BaseTable>(val model: T){
abstract class BaseStore<T : BaseTable>(private val model: T) {
suspend fun create(callback: (Pair<InsertStatement<Number>, String>) -> Unit): String? {
val uuid = UUID.randomUUID().toString()
DatabaseFactory.dbQuery {
model.insert{
model.insert {
//TODO undo this Pair couldn't figure out a different way
callback(Pair(it, uuid))
it[id] = uuid
Expand All @@ -23,6 +23,7 @@ abstract class BaseStore<T : BaseTable>(val model: T){
}
return get(uuid)
}

suspend fun get(uuid: String?): String? {
return DatabaseFactory.dbQuery {
model.select {
Expand All @@ -31,16 +32,17 @@ abstract class BaseStore<T : BaseTable>(val model: T){
}
}

suspend fun <L>getAll(createJsonPojo: (items: ResultRow) -> L) : List <L>{
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){

suspend fun updateById(id: String, valueToUpdate: (x: UpdateStatement) -> UpdateStatement) {
return DatabaseFactory.dbQuery {
model.update({ model.id eq id!! })
model.update({ model.id eq id })
{
valueToUpdate(it)
}
Expand Down
29 changes: 14 additions & 15 deletions src/main/kotlin/Stores/ChannelStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.*
data class ChannelObj(val creatorId: String,
val teamId: String,
val type: String,
val displayName : String,
val displayName: String,
val name: String,
val header: String,
val purpose: String,
Expand Down Expand Up @@ -54,20 +54,19 @@ object ChannelStore {

suspend fun getAllChannels(): List<ChannelObj> {
return DatabaseFactory.dbQuery {
Channel.selectAll().map{
return@map ChannelObj(
it[Channel.creatorId],
it[Channel.teamId],
it[Channel.type],
it[Channel.displayName],
it[Channel.name],
it[Channel.header],
it[Channel.purpose],
it[Channel.id],
it[Channel.createdAt].toString(),
it[Channel.updateAt].toString(),
it[Channel.totalMsgCount])

Channel.selectAll().map {
return@map ChannelObj(
it[Channel.creatorId],
it[Channel.teamId],
it[Channel.type],
it[Channel.displayName],
it[Channel.name],
it[Channel.header],
it[Channel.purpose],
it[Channel.id],
it[Channel.createdAt].toString(),
it[Channel.updateAt].toString(),
it[Channel.totalMsgCount])
}
}
}
Expand Down
46 changes: 24 additions & 22 deletions src/main/kotlin/Stores/ChannelSubscriptionStore.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package Stores

import models.ChannelSubscription
import DatabaseFactory
import io.ktor.http.Parameters
import models.Channel
import models.ChannelSubscription
import models.User
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.joda.time.DateTime
import java.util.*

data class UserSmallObj(
val username: String,
val id: String)
Expand All @@ -19,13 +20,15 @@ data class ChannelUsers(
val id: String,
val User: UserSmallObj
)
data class ChannelSubscriptionObj (

data class ChannelSubscriptionObj(
var userId: String,
var channelId: String,
var id: String,
var createdAt: String,
var updateAt: String
)

object ChannelSubscriptionStore {
suspend fun createChannelSubscription(params: Parameters): String? {
val uuid = UUID.randomUUID().toString()
Expand All @@ -42,6 +45,7 @@ object ChannelSubscriptionStore {
}
return getChannelSubscription(uuid)
}

suspend private fun getChannelSubscription(uuid: String): String? {
return DatabaseFactory.dbQuery {
ChannelSubscription.select {
Expand All @@ -54,33 +58,31 @@ object ChannelSubscriptionStore {
return DatabaseFactory.dbQuery {
ChannelSubscription.selectAll().map {
return@map ChannelSubscriptionObj(
it[ChannelSubscription.userId],
it[ChannelSubscription.channelId],
it[ChannelSubscription.id],
it[ChannelSubscription.createdAt].toString(),
it[ChannelSubscription.updateAt].toString()
it[ChannelSubscription.userId],
it[ChannelSubscription.channelId],
it[ChannelSubscription.id],
it[ChannelSubscription.createdAt].toString(),
it[ChannelSubscription.updateAt].toString()
)
}
}
}

suspend fun getUsersInChannel(uuid: String): List<ChannelUsers>?{
suspend fun getUsersInChannel(uuid: String): List<ChannelUsers>? {
return DatabaseFactory.dbQuery {
(User innerJoin ChannelSubscription innerJoin Channel).slice(User.username, User.id,
(User innerJoin ChannelSubscription innerJoin Channel).slice(User.username, User.id,
ChannelSubscription.id, ChannelSubscription.userId, ChannelSubscription.channelId,
Channel.id, Channel.name, Channel.creatorId).
select {ChannelSubscription.channelId.eq(uuid!!) }.
map {
return@map ChannelUsers(
it[Channel.name],
it[Channel.id],
UserSmallObj(
it[User.username],
it[User.id]
)

Channel.id, Channel.name, Channel.creatorId).select { ChannelSubscription.channelId.eq(uuid!!) }.map {
return@map ChannelUsers(
it[Channel.name],
it[Channel.id],
UserSmallObj(
it[User.username],
it[User.id]
)
}

)
}
}
}
}
10 changes: 6 additions & 4 deletions src/main/kotlin/Stores/TeamStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import models.Team.id
import models.Team.name
import models.Team.updateAt

data class TeamObj (
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"]!!
return create {
it.first[name] = params["name"]!!
}

}
suspend fun getAll() : List <TeamObj>{

suspend fun getAll(): List<TeamObj> {
return getAll {
TeamObj(
it[id],
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/Stores/UserStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ object UserStore {
User.select { User.email.eq(email!!) }.takeIf { !it.empty() }
}
}

suspend fun getAllUsers(): List<UsersObj> {
return DatabaseFactory.dbQuery {
User.selectAll().map {
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions src/main/kotlin/models/Channel.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package models

import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.Date
import org.jetbrains.exposed.sql.Table
import org.joda.time.DateTime

object Channel : Table() {
// dynamic
Expand Down
3 changes: 0 additions & 3 deletions src/main/kotlin/models/ChannelSubscription.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package models

import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.Date
import org.jetbrains.exposed.sql.Table
import org.joda.time.DateTime

object ChannelSubscription : Table() {
// dynamic
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/models/Team.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package models

import org.jetbrains.exposed.sql.Table


object Team : BaseTable() {
val name = varchar("name", 30)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/models/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package models

import org.jetbrains.exposed.sql.Table

object User: Table() {
object User : Table() {
val id = varchar("id", 36).primaryKey()
val firstName = varchar("firstName",50)
val firstName = varchar("firstName", 50)
val lastName = varchar("lastName", 50)
val createdAt = datetime("createdAt")
val username = varchar("username", 64)
Expand Down
21 changes: 12 additions & 9 deletions src/main/kotlin/routing/ChannelRouting.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package routing

import Locations
import Stores.*
import com.sun.media.jfxmedia.logging.Logger
import io.ktor.application.call
Expand All @@ -24,7 +25,7 @@ object ChannelRouting {
val requiredParams = listOf("creatorId", "teamId", "type", "displayName", "name", "header", "purpose")
val missingFields: List<String> =
requiredParams.filter { param ->
params[param].isNullOrBlank()
params[param].isNullOrBlank()
}
if (missingFields.isNotEmpty()) {
val response = missingFields.joinToString(separator = ", ")
Expand All @@ -34,7 +35,7 @@ object ChannelRouting {
call.respond(CreateChannelResponse("success", "Successfully created a channel", channelId))
}
}
get<Locations.Channels>{
get<Locations.Channels> {
val channels: List<ChannelObj> = ChannelStore.getAllChannels()
call.respond(ListChannelResponse("success", "Successfully retrieved all channels", channels))
}
Expand All @@ -48,20 +49,22 @@ object ChannelRouting {
}
}

fun Route.getAllUsersForChannel(){
fun Route.getAllUsersForChannel() {
get<Locations.GetAllUsersForChannel> {
try { call.parameters["uuid"]!! } catch(e: NullPointerException){
try {
call.parameters["uuid"]!!
} catch (e: NullPointerException) {
call.respond(ListUsersChannelsResponse("failed", "Missing uuid of channel ", null))
}
call.parameters["uuid"]?.let{
call.parameters["uuid"]?.let {
val uuid = it
Logger.logMsg(Logger.INFO, "Starting getting all users for channels")
val channelUsers : List<ChannelUsers>? = ChannelSubscriptionStore.getUsersInChannel(uuid)
if(channelUsers != null){
val users :List<UserSmallObj>? = channelUsers.map{ it.User }
val channelUsers: List<ChannelUsers>? = ChannelSubscriptionStore.getUsersInChannel(uuid)
if (channelUsers != null) {
val users: List<UserSmallObj>? = channelUsers.map { it.User }
val respObj = ChannelMem(channelUsers[0].name, channelUsers[0].id, users)
call.respond(ListUsersChannelsResponse("success", "Successfully retrieved a channel", respObj))
}else{
} else {
call.respond(ListUsersChannelsResponse("success", "Successfully retrieved no channels", null))
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/routing/ChannelSubscriptionRouting.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package routing

import Locations
import Stores.ChannelSubscriptionObj
import Stores.ChannelSubscriptionStore
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

Expand All @@ -34,8 +33,9 @@ object ChannelSubscriptionRouting {
}

}
fun Route.getAllSubscriptions(){
get<Locations.GetAllSubscriptions>{

fun Route.getAllSubscriptions() {
get<Locations.GetAllSubscriptions> {
val subscriptions: List<ChannelSubscriptionObj> = ChannelSubscriptionStore.getAllChannelSubcriptions()
call.respond(ListChannelSubscription("success", "Successfully retrieved all the subscriptions", subscriptions))
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/kotlin/routing/TeamRouting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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{
object TeamRouting {
fun Route.createTeam() {
post<Locations.Teams> {
val params = call.receive<Parameters>()
Expand All @@ -33,13 +33,14 @@ object TeamRouting{
call.respond(CreateResponse("success", "Successfully created a Team", teamId))
}
}
get<Locations.Teams>{
val teams= TeamStore.getAll();
get<Locations.Teams> {
val teams = TeamStore.getAll();
call.respond(ListResponse<TeamObj>("success", "Successfully retrieved all Teams", teams))
}
}
fun Route.getTeam(){
get<Locations.Team>{

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))
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/routing/UserRouting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ object UserRouting {
}
}
}
fun Route.getUsers(){
get<Locations.GetAllUsers>{

fun Route.getUsers() {
get<Locations.GetAllUsers> {
val users: List<UsersObj> = UserStore.getAllUsers()
call.respond(ListUserResponse("success", "Successfully retrieved all users", users))
}
Expand Down
Loading

0 comments on commit 59f59c9

Please sign in to comment.