Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Event to import and export JSON database following the same for… #113

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions src/main/scala/model/TodoServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class TodoServer() {
new Database
}

var socketToTasks: Map[SocketIOClient, List[Int]] = Map()//maps socket to a list of task ids created by said socket

setNextId()

var usernameToSocket: Map[String, SocketIOClient] = Map()
Expand All @@ -29,22 +31,45 @@ class TodoServer() {
server.addConnectListener(new ConnectionListener(this))
server.addEventListener("add_task", classOf[String], new AddTaskListener(this))
server.addEventListener("complete_task", classOf[String], new CompleteTaskListener(this))
server.addEventListener("import_task", classOf[String], new importTaskListener(this))
server.addEventListener("export_task", classOf[Nothing], new exportTaskListener(this))

server.start()

def tasksJSON(): String = {
def tasksJSON(socket: SocketIOClient): String = {
val tasks: List[Task] = database.getTasks
val tasksJSON: List[JsValue] = tasks.map((entry: Task) => entry.asJsValue())
val pTasks: List[Task] = tasks.filter((value: Task) => socketToTasks.getOrElse(socket,List()).contains(value.id.toInt))
val tasksJSON: List[JsValue] = pTasks.map((entry: Task) => entry.asJsValue())
Json.stringify(Json.toJson(tasksJSON))
}

/*def tasksJSONALL(): String = {
val tasks: List[Task] = database.getTasks
val tasksJSON: List[JsValue] = tasks.map((entry: Task) => entry.asJsValue())
Json.stringify(Json.toJson(tasksJSON))
}*/

def setNextId(): Unit = {
val tasks = database.getTasks
if (tasks.nonEmpty) {
Task.nextId = tasks.map(_.id.toInt).max + 1
}
}

def importTasks(taskJson: String, socket: SocketIOClient): Unit = {
val tasks: List[JsValue] = Json.parse(taskJson).as[List[JsValue]]
for(task <- tasks){
database.addTask(makeTask(task))
socketToTasks += socket -> (database.getLastTaskId() :: socketToTasks.getOrElse(socket,List()))
}
}

def makeTask(task: JsValue): Task = {
val title: String = (task \ "title").as[String]
val description: String = (task \ "description").as[String]
Task(title, description)
}

}

object TodoServer {
Expand All @@ -57,21 +82,32 @@ object TodoServer {
class ConnectionListener(server: TodoServer) extends ConnectListener {

override def onConnect(socket: SocketIOClient): Unit = {
socket.sendEvent("all_tasks", server.tasksJSON())
socket.sendEvent("all_tasks", server.tasksJSON(socket))
}

}

class importTaskListener(server: TodoServer) extends DataListener[String]{//expects the client to emit a signal of "import_task" with a value of JSON string, will add the tasks into the current database
override def onData(socket: SocketIOClient, taskJSON: String, ackRequest: AckRequest): Unit = {
server.importTasks(taskJSON,socket)
socket.sendEvent("all_tasks", server.tasksJSON(socket))
}
}

class exportTaskListener(server: TodoServer) extends DataListener[Nothing]{//expects the client to emit a signal of "export_task", will convert all in the database into a JSON string and send it to the socket requesting
override def onData(socket: SocketIOClient, nothing: Nothing, ackRequest: AckRequest): Unit ={
socket.sendEvent("exported_task", server.tasksJSON(socket))
}
}

class AddTaskListener(server: TodoServer) extends DataListener[String] {

override def onData(socket: SocketIOClient, taskJSON: String, ackRequest: AckRequest): Unit = {
val task: JsValue = Json.parse(taskJSON)
val title: String = (task \ "title").as[String]
val description: String = (task \ "description").as[String]
server.database.addTask(server.makeTask(Json.parse(taskJSON)))
server.socketToTasks += socket -> (server.database.getLastTaskId() :: server.socketToTasks.getOrElse(socket,List()))
//server.server.getBroadcastOperations.sendEvent("all_tasks", server.tasksJSONALL())
socket.sendEvent("all_tasks", server.tasksJSON(socket))

server.database.addTask(Task(title, description))
server.server.getBroadcastOperations.sendEvent("all_tasks", server.tasksJSON())
}

}
Expand All @@ -80,8 +116,11 @@ class AddTaskListener(server: TodoServer) extends DataListener[String] {
class CompleteTaskListener(server: TodoServer) extends DataListener[String] {

override def onData(socket: SocketIOClient, taskId: String, ackRequest: AckRequest): Unit = {

server.database.completeTask(taskId)
server.server.getBroadcastOperations.sendEvent("all_tasks", server.tasksJSON())
server.socketToTasks += socket -> server.socketToTasks(socket).filter(_!=taskId.toInt)
socket.sendEvent("all_tasks", server.tasksJSON(socket))

}

}
Expand Down
5 changes: 5 additions & 0 deletions src/main/scala/model/database/Database.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class Database extends DatabaseAPI{
statement.execute()
}

override def getLastTaskId: Int = {
val statement = connection.prepareStatement("SELECT LAST() FROM tasks")
val result: ResultSet = statement.executeQuery()
result.getString("id").toInt
}

override def getTasks: List[Task] = {
val statement = connection.prepareStatement("SELECT * FROM tasks")
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/model/database/DatabaseAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ trait DatabaseAPI {

def addTask(task: Task): Unit
def completeTask(taskId: String): Unit
def getLastTaskId(): Int
def getTasks: List[Task]

}
10 changes: 10 additions & 0 deletions src/main/scala/model/database/TestingDatabase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class TestingDatabase extends DatabaseAPI {
data ::= task
}

override def getLastTaskId(): Int = {
var id = 0
for(task <- data){
if(task.id.toInt > id){
id = task.id.toInt
}
}
id
}


override def completeTask(taskId: String): Unit = {
data = data.filter(_.id != taskId)
Expand Down