Skip to content

Commit

Permalink
Merge pull request #280 from Viva-con-Agua/feature/265
Browse files Browse the repository at this point in the history
Feature/265
  • Loading branch information
deinelieblings authored Nov 9, 2018
2 parents 7234cb1 + e79da13 commit 2b5b3c0
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 52 deletions.
77 changes: 34 additions & 43 deletions app/controllers/webapp/OAuthClientSocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,84 +54,75 @@ class OauthClientSocketController @Inject() (
def props(out: ActorRef) = Props(new OauthClientSocketActor(out))
}

class OauthClientSocketActor (out: ActorRef)extends Actor {
class OauthClientSocketActor (out: ActorRef)extends WebSocketActor(out) {

implicit val socketModel: String = "OauthClient"

def handleWebSocketEvent(msg: WebSocketEvent): WebSocketEvent = {
//lazy val responseTimestamp = currentTime
msg.operation match {
case "INSERT" => insert(msg)
case "UPDATE" => update(msg)
case "DELETE" => delete(msg)
case _ => WebSocketEvent("ERROR", None, Option(Messages("socket.error.ops", msg.operation)))
}
}

def receive = {
case request: WebSocketEvent =>
val response = handleWebSocketEvent(request)
out ! response
}

def insert(event: WebSocketEvent): WebSocketEvent = {
override def insert(event: WebSocketEvent): Future[WebSocketEvent] = {
event.query match {
//
case Some(query) => {
val firstElement = query.headOption.getOrElse(return WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel))))
val firstElement = query.headOption.getOrElse(return Future.successful(WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel)))))
val oauthJsonResult: JsResult[OauthClient] = firstElement.validate[OauthClient]
oauthJsonResult match {
case s: JsSuccess[OauthClient] => {
oauthClientService.save(s.get)
WebSocketEvent("SUCCESS", None, Option(Messages("socket.success.insert", s.get.id)))
oauthClientService.save(s.get).flatMap{
case (oauthClient) => Future.successful(WebSocketEvent("SUCCESS", Option(List(Json.toJson(oauthClient))), Option(Messages("socket.success.insert", s.get.id))))
}
}
case e: JsError => WebSocketEvent("ERROR", Option(List(JsError.toJson(e))), Option(Messages("socket.error.model", socketModel)))
case e: JsError => Future.successful(WebSocketEvent("ERROR", Option(List(JsError.toJson(e))), Option(Messages("socket.error.model", socketModel))))
}

}
case _ => WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel)))
case _ => Future.successful(WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel))))
}
}
//handle socket event for update crew
def update(event: WebSocketEvent): WebSocketEvent = {
override def update(event: WebSocketEvent): Future[WebSocketEvent] = {
//check if there is a query, else return WebSocketEvent with error
event.query match {
case Some(query) => {
// get first element of query. If the list is nil return WebSocketEvent error
val firstElement = query.headOption.getOrElse(return WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel))))
val firstElement = query.headOption.getOrElse(return Future.successful(WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel)))))
// validate Json as Crew. If there is no Crew return WebSocketEvent error.
val oauthJsonResult: JsResult[OauthClient] = firstElement.validate[OauthClient]
oauthJsonResult match {
case s: JsSuccess[OauthClient] => {
oauthClientService.update(s.get)
WebSocketEvent("SUCCESS", None, Option(Messages("socket.success.update", s.get.id)))
oauthClientService.update(s.get).flatMap{
case (oauthClient) => Future.successful(WebSocketEvent("SUCCESS", Option(List(Json.toJson(oauthClient))), Option(Messages("socket.success.update", s.get.id))))
}
}
case e: JsError => WebSocketEvent("ERROR", Option(List(JsError.toJson(e))), Option(Messages("socket.error.model", socketModel)))
case e: JsError => Future.successful(WebSocketEvent("ERROR", Option(List(JsError.toJson(e))), Option(Messages("socket.error.model", socketModel))))
}
}
case _ => WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel)))
case _ => Future.successful(WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel))))
}
}

def delete(event: WebSocketEvent): WebSocketEvent = {
override def delete(event: WebSocketEvent): Future[WebSocketEvent] = {
//check if there is a query, else return WebSocketEvent with error
event.query match {
case Some(query) => {
// get first element of query. If the list is nil return WebSocketEvent error
val firstElement = query.headOption.getOrElse(return WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel))))
// validate Json as Crew. If there is no Crew return WebSocketEvent error.
val crewJsonResult: JsResult[OauthClient] = firstElement.validate[OauthClient]
crewJsonResult match {
case s: JsSuccess[OauthClient] => {
oauthClientService.delete(s.get)
WebSocketEvent("SUCCESS", None, Option(Messages("socket.success.delete", s.get.id)))
event.query match {
case Some(query) => {
// get first element of query. If the list is nil return WebSocketEvent error
val firstElement = query.headOption.getOrElse(return Future.successful(WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel)))))
// validate Json as Crew. If there is no Crew return WebSocketEvent error.
val crewJsonResult: JsResult[OauthClient] = firstElement.validate[OauthClient]
crewJsonResult match {
case s: JsSuccess[OauthClient] => {
oauthClientService.delete(s.get).flatMap{
case true => Future.successful(WebSocketEvent("SUCCESS", None, Option(Messages("socket.success.delete", s.get.id))))
case false => Future.successful(WebSocketEvent("ERROR", None, Option(Messages("socket.error.notFound", s.get.id))))
}
}
case e: JsError => Future.successful(WebSocketEvent("ERROR", Option(List(JsError.toJson(e))), Option(Messages("socket.error.model", socketModel))))
}
case e: JsError => WebSocketEvent("ERROR", Option(List(JsError.toJson(e))), Option(Messages("socket.error.model", socketModel)))
}
case _ => Future.successful(WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel))))
}
case _ => WebSocketEvent("ERROR", None, Option(Messages("socket.error.query", socketModel)))
}
}

override def notMatch(event: WebSocketEvent): WebSocketEvent =
WebSocketEvent("ERROR", None, Option(Messages("socket.error.ops", event.operation)))
}
}

30 changes: 29 additions & 1 deletion app/daos/OauthClientDao.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import slick.driver.JdbcProfile
import slick.lifted.TableQuery
import slick.driver.MySQLDriver.api._
import play.api.db.slick.DatabaseConfigProvider

import slick.jdbc.{GetResult, PositionedParameters, SQLActionBuilder, SetParameter}
import models.converter.OauthClientConverter

/**
* Created by johann on 24.11.16.
Expand All @@ -29,6 +30,10 @@ trait OauthClientDao {
def find(id: String, secret: Option[String], grantType: String) : Future[Option[OauthClient]]
def save(client: OauthClient) : Future[OauthClient]
def validate(id: String, secret: Option[String], grantType: String) : Future[Boolean]
def update(client: OauthClient) : Future[OauthClient]
def delete(client: OauthClient) : Future[Boolean]
def list_with_statement(statement : SQLActionBuilder) : Future[List[OauthClient]]

}

class MongoOauthClientDao extends OauthClientDao {
Expand Down Expand Up @@ -63,12 +68,19 @@ class MongoOauthClientDao extends OauthClientDao {
case Some(client) => true
case _ => false
})

// It's necessary to define functions for the OauthClientDao trait in MongoOauthClientDao
def update(client: OauthClient): Future[OauthClient] = ???
def delete(client: OauthClient) : Future[Boolean] = ???
def list_with_statement(statement : SQLActionBuilder) : Future[List[OauthClient]] = ???
}

class MariadbOauthClientDao extends OauthClientDao {
val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
val oauthClients = TableQuery[OauthClientTableDef]

implicit val getOauthClient = GetResult(r => OauthClientDB(r.nextString, r.nextString, r.nextStringOption, r.nextString))

override def find(id: String) : Future[Option[OauthClient]] = {
dbConfig.db.run(oauthClients.filter(_.id === id).result).map(oauthClient => {
oauthClient.headOption.map(_.toOauthClient)
Expand Down Expand Up @@ -102,4 +114,20 @@ class MariadbOauthClientDao extends OauthClientDao {
else false
})
}

def update(client: OauthClient) : Future[OauthClient] = {
dbConfig.db.run(oauthClients.update(OauthClientDB(client))).flatMap(_ => find(client.id)).map(_.get)
}

def delete(client: OauthClient) : Future[Boolean] = {
dbConfig.db.run(oauthClients.filter(o => o.id === client.id).delete).flatMap {
case 0 => Future.successful(false)
case _ => Future.successful(true)
}
}

def list_with_statement(statement : SQLActionBuilder) : Future[List[OauthClient]] = {
var sql_action = statement.as[(OauthClientDB)]
dbConfig.db.run(sql_action).map(OauthClientConverter.buildListFromResult(_))
}
}
15 changes: 15 additions & 0 deletions app/models/converter/OauthClientConverter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package models.converter

import models.{OauthClient}
import models.database.{OauthClientDB}

object OauthClientConverter {

def buildListFromResult(result : Seq[(OauthClientDB)]) : List[OauthClient] = {
val oauthClientList = result.seq.foldLeft[List[OauthClient]](Nil)((oauthClientList, dbEntry) => {
val oauthClient = dbEntry
oauthClientList ++ List(oauthClient.toOauthClient)
})
oauthClientList
}
}
6 changes: 3 additions & 3 deletions app/services/OAuthService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import slick.jdbc.SQLActionBuilder

class OauthClientService @Inject() (oauthClientDao: OauthClientDao) {
def save(client: OauthClient): Future[OauthClient] = oauthClientDao.save(client)
def update(client: OauthClient): Future[OauthClient] = ???
def update(client: OauthClient): Future[OauthClient] = oauthClientDao.update(client)
def get(id: String): Future[Option[OauthClient]] = oauthClientDao.find(id)
def get(id: String, secret: String): Future[Option[OauthClient]] = oauthClientDao.find(id, secret)
def get(id: String, secret: Option[String], grandType: String): Future[Option[OauthClient]] = oauthClientDao.find(id, secret, grandType)
def delete(client: OauthClient): Future[Boolean] = ???
def list_with_statement(statement: SQLActionBuilder): Future[List[OauthClient]] = ???
def delete(client: OauthClient): Future[Boolean] = oauthClientDao.delete(client)
def list_with_statement(statement: SQLActionBuilder): Future[List[OauthClient]] = oauthClientDao.list_with_statement(statement)
}
8 changes: 4 additions & 4 deletions conf/evolutions/default/16.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ CREATE TABLE Crew(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
publicId BINARY(16),
name VARCHAR(255),
PRIMARY KEY (id),
UNIQUE KEY (name)
country VARCHAR(255),
PRIMARY KEY (id)
);

CREATE TABLE City(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
country VARCHAR(255),
crew_id BIGINT(20),
PRIMARY KEY (id),
FOREIGN KEY (crew_id) REFERENCES Crew(id) ON UPDATE CASCADE
Expand All @@ -27,4 +26,5 @@ ALTER TABLE Supporter
DROP TABLE Crew;
DROP TABLE City;
ALTER TABLE Supporter DROP FOREIGN KEY Supporter_ibfk_2;
ALTER TABLE Supporter DROP COLUMN crew_id;
ALTER TABLE Supporter
DROP COLUMN crew_id;
2 changes: 1 addition & 1 deletion conf/evolutions/default/23.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ CREATE VIEW Crews AS
SELECT Crew.id as Crew_id,
uuid_of(Crew.publicId) as Crew_publicId,
Crew.name as Crew_name,
Crew.country as Crew_country,
City.id as City_id,
City.name as City_name,
City.country as City_country,
City.crew_id as City_crew_id
from Crew
INNER JOIN City ON Crew.id = City.crew_id;
Expand Down
9 changes: 9 additions & 0 deletions conf/evolutions/default/31.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

# --- !Ups
ALTER TABLE Crew DROP COLUMN country;
ALTER TABLE City
ADD COLUMN country VARCHAR(255);

# --- !Downs
ALTER VIEW Crews DROP COLUMN City.country;
ALTER TABLE City DROP COLUMN country;

0 comments on commit 2b5b3c0

Please sign in to comment.