Skip to content
Snippets Groups Projects
Commit 1bc65d74 authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

Add Project List Route

parent e26c9bff
No related branches found
No related tags found
No related merge requests found
......@@ -60,7 +60,8 @@ object PlayPadServer extends App {
webSocket("/project", new ProjectSyncHandler(sessionDao, databaseConnection))
// Project
get("/projects", new ProjectGet(databaseConnection, sessionDao), new JsonTransformer)
get("/projects/:id", new ProjectGet(databaseConnection, sessionDao), new JsonTransformer)
get("/projects", new ProjectList(databaseConnection, sessionDao), new JsonTransformer)
post("/projects", new ProjectPost(databaseConnection, sessionDao), new JsonTransformer)
delete("/projects", new ProjectDelete(databaseConnection, sessionDao), new JsonTransformer)
......
......@@ -13,6 +13,5 @@ class Pad() {
var position: Int = _
var pageIndex: Int = _
var design: Design = _
var page: Page = _
}
package de.tobias.playpad.server.project
import java.util.UUID
/**
* Created by tobias on 19.02.17.
*/
class ProjectReference {
var id: UUID = UUID.randomUUID()
var name: String = _
}
......@@ -3,7 +3,7 @@ package de.tobias.playpad.server.project.loader.sql
import java.sql.Connection
import java.util.UUID
import de.tobias.playpad.server.project.Project
import de.tobias.playpad.server.project.{Project, ProjectReference}
import de.tobias.playpad.server.project.utils.SqlDef._
/**
......@@ -36,6 +36,28 @@ class ProjectLoader(val connection: Connection) {
projects
}
def list(accountId: Int): List[ProjectReference] = {
val sql = s"SELECT * FROM $PROJECT WHERE $PROJECT_ACCOUNT_ID = ?"
val preparedStatement = connection.prepareStatement(sql)
preparedStatement.setInt(1, accountId)
val result = preparedStatement.executeQuery()
var projects: List[ProjectReference] = List()
while (result.next()) {
val project = new ProjectReference()
project.id = UUID.fromString(result.getString(PROJECT_ID))
project.name = result.getString(PROJECT_NAME)
projects = project :: projects
}
result.close()
preparedStatement.close()
projects
}
def getAccountId(id: UUID): Int = {
val sql = s"SELECT account_id FROM $PROJECT WHERE $PROJECT_ID = ?"
......
package de.tobias.playpad.server.server.project
import java.sql.Connection
import java.util.UUID
import com.google.gson.{JsonArray, JsonObject}
import com.j256.ormlite.dao.Dao
import de.tobias.playpad.server.account.Session
import de.tobias.playpad.server.project.loader.sql.ProjectLoader
import de.tobias.playpad.server.project.saver.json.ProjectSaver
import de.tobias.playpad.server.server.{Result, Status}
import spark.{Request, Response, Route}
/**
* Created by tobias on 17.02.17.
*/
class ProjectList(connection: Connection, sessionDao: Dao[Session, Int]) extends Route {
override def handle(request: Request, response: Response): AnyRef = {
val sessionKey = request.queryParams("session")
val session = Session.getSession(sessionKey, sessionDao)
session match {
case Some(s) =>
val projectLoader = new ProjectLoader(connection)
val projects = projectLoader.list(s.getAccount.id)
val array = new JsonArray()
projects.foreach(project => {
val json = new JsonObject()
json.addProperty("uuid", project.id.toString)
json.addProperty("name", project.name)
array.add(json)
})
array
case None =>
new Result(Status.ERROR, "Session invalid")
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment