Skip to content

Commit

Permalink
Merge pull request #4 from UB-CSE/testing-database
Browse files Browse the repository at this point in the history
add developer mode
  • Loading branch information
jessehartloff authored Apr 23, 2020
2 parents 7dc263d + 51be62d commit 09c433e
Showing 6 changed files with 64 additions and 12 deletions.
2 changes: 1 addition & 1 deletion nginx/public/officeHours.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const socket = io.connect({transports: ['websocket']});
const socket = io.connect("http://localhost:8080", {transports: ['websocket']});

socket.on('queue', displayQueue);
socket.on('message', displayMessage);
7 changes: 7 additions & 0 deletions src/main/scala/model/Configuration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

object Configuration {

val DEV_MODE = true

}
15 changes: 11 additions & 4 deletions src/main/scala/model/OfficeHoursServer.scala
Original file line number Diff line number Diff line change
@@ -2,11 +2,18 @@ package model

import com.corundumstudio.socketio.listener.{DataListener, DisconnectListener}
import com.corundumstudio.socketio.{AckRequest, Configuration, SocketIOClient, SocketIOServer}
import model.database.{Database, DatabaseAPI, TestingDatabase}
import play.api.libs.json.{JsValue, Json}


class OfficeHoursServer() {

val database: DatabaseAPI = if(Configuration.DEV_MODE){
new TestingDatabase
}else{
new Database
}

var usernameToSocket: Map[String, SocketIOClient] = Map()
var socketToUsername: Map[SocketIOClient, String] = Map()

@@ -24,7 +31,7 @@ class OfficeHoursServer() {
server.start()

def queueJSON(): String = {
val queue: List[StudentInQueue] = Database.getQueue()
val queue: List[StudentInQueue] = database.getQueue
val queueJSON: List[JsValue] = queue.map((entry: StudentInQueue) => entry.asJsValue())
Json.stringify(Json.toJson(queueJSON))
}
@@ -53,7 +60,7 @@ class DisconnectionListener(server: OfficeHoursServer) extends DisconnectListene

class EnterQueueListener(server: OfficeHoursServer) extends DataListener[String] {
override def onData(socket: SocketIOClient, username: String, ackRequest: AckRequest): Unit = {
Database.addStudentToQueue(StudentInQueue(username, System.nanoTime()))
server.database.addStudentToQueue(StudentInQueue(username, System.nanoTime()))
server.socketToUsername += (socket -> username)
server.usernameToSocket += (username -> socket)
server.server.getBroadcastOperations.sendEvent("queue", server.queueJSON())
@@ -63,10 +70,10 @@ class EnterQueueListener(server: OfficeHoursServer) extends DataListener[String]

class ReadyForStudentListener(server: OfficeHoursServer) extends DataListener[Nothing] {
override def onData(socket: SocketIOClient, dirtyMessage: Nothing, ackRequest: AckRequest): Unit = {
val queue = Database.getQueue().sortBy(_.timestamp)
val queue = server.database.getQueue.sortBy(_.timestamp)
if(queue.nonEmpty){
val studentToHelp = queue.head
Database.removeStudentFromQueue(studentToHelp.username)
server.database.removeStudentFromQueue(studentToHelp.username)
socket.sendEvent("message", "You are now helping " + studentToHelp.username)
if(server.usernameToSocket.contains(studentToHelp.username)){
server.usernameToSocket(studentToHelp.username).sendEvent("message", "A TA is ready to help you")
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package model
package model.database

import java.sql.{Connection, DriverManager, ResultSet}

object Database {
import model.StudentInQueue

val url = "jdbc:mysql://mysql/officehours"

class Database extends DatabaseAPI{

val url = "jdbc:mysql://mysql/officehours?autoReconnect=true"
val username: String = sys.env("DB_USERNAME")
val password: String = sys.env("DB_PASSWORD")

@@ -18,7 +21,7 @@ object Database {
}


def addStudentToQueue(student: StudentInQueue): Unit = {
override def addStudentToQueue(student: StudentInQueue): Unit = {
val statement = connection.prepareStatement("INSERT INTO queue VALUE (?, ?)")

statement.setString(1, student.username)
@@ -28,7 +31,7 @@ object Database {
}


def removeStudentFromQueue(username: String): Unit = {
override def removeStudentFromQueue(username: String): Unit = {
val statement = connection.prepareStatement("DELETE FROM queue WHERE username=?")

statement.setString(1, username)
@@ -37,7 +40,7 @@ object Database {
}


def getQueue(): List[StudentInQueue] = {
override def getQueue: List[StudentInQueue] = {
val statement = connection.prepareStatement("SELECT * FROM queue")
val result: ResultSet = statement.executeQuery()

@@ -49,7 +52,7 @@ object Database {
queue = new StudentInQueue(username, timestamp) :: queue
}

queue
queue.reverse
}

}
11 changes: 11 additions & 0 deletions src/main/scala/model/database/DatabaseAPI.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model.database

import model.StudentInQueue

trait DatabaseAPI {

def addStudentToQueue(student: StudentInQueue): Unit
def removeStudentFromQueue(username: String): Unit
def getQueue: List[StudentInQueue]

}
24 changes: 24 additions & 0 deletions src/main/scala/model/database/TestingDatabase.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package model.database

import model.StudentInQueue

class TestingDatabase extends DatabaseAPI {

var data: List[StudentInQueue] = List()


override def addStudentToQueue(student: StudentInQueue): Unit = {
data ::= student
}


override def removeStudentFromQueue(username: String): Unit = {
data = data.filter(_.username != username)
}


override def getQueue: List[StudentInQueue] = {
data.reverse
}

}

0 comments on commit 09c433e

Please sign in to comment.