-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
9 changed files
with
162 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |