Skip to content

Commit

Permalink
Merge pull request #45 from agoravoting/go-v8
Browse files Browse the repository at this point in the history
Go v8
  • Loading branch information
Findeton committed Nov 6, 2017
2 parents 0a8a8e8 + 0070f7c commit 1475826
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
29 changes: 26 additions & 3 deletions app/controllers/ElectionsApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,16 @@ object ElectionsApi
if (!body.as[JsObject].keys.contains("logo_url")) {
body = body.as[JsObject] + ("logo_url" -> Json.toJson(""))
}
if (body.as[JsObject].keys.contains("start_date") &&
(0 == (body.as[JsObject] \ "start_date").toString.length ||
"\"\"" == (body.as[JsObject] \ "start_date").toString)) {
body = body.as[JsObject] - "start_date"
}
if (body.as[JsObject].keys.contains("end_date") &&
(0 == (body.as[JsObject] \ "end_date").toString.length ||
"\"\"" == (body.as[JsObject] \ "end_date").toString)) {
body = body.as[JsObject] - "end_date"
}

val electionConfig = body.validate[ElectionConfig]

Expand All @@ -523,8 +533,7 @@ object ElectionsApi
config =>
{
try {
val now = new java.sql.Timestamp(new Date().getTime)
val validated = config.validate(authorities, id).copy(start_date=now, end_date=now)
val validated = config.validate(authorities, id).copy(start_date=None, end_date=None)
DB.withSession
{
implicit session =>
Expand Down Expand Up @@ -891,10 +900,24 @@ object ElectionsApi
val authData = getAuthData(auths)
// add the callback and auth data fields to the original config
val jsObject = configJson.as[JsObject]

// add start date if missing
val withStartDate = if (!jsObject.keys.contains("start_date")) {
jsObject + ("start_date" -> JsString("2000-01-01T00:00:00.001"))
} else {
jsObject
}
// add end date if missing
val withEndDate = if (!withStartDate.keys.contains("end_date")) {
withStartDate + ("end_date" -> JsString("2000-01-01T00:00:00.001"))
} else {
withStartDate
}

val callback = "callback_url" -> JsString(apiSslUrl(routes.ElectionsApi.keydone(election.id).url))
Logger.info("create callback is " + callback)

val withCallback = (jsObject + callback)
val withCallback = (withEndDate + callback)
val withAuthorities = withCallback - "authorities" + ("authorities" -> authData)

Logger.info(s"creating election with\n$withAuthorities")
Expand Down
2 changes: 1 addition & 1 deletion app/models/DAL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ object DAL {
Elections.updateResults(id, results)
}

def updateConfig(id: Long, config: String, start: Timestamp, end: Timestamp) = DB.withSession { implicit session =>
def updateConfig(id: Long, config: String, start: Option[Timestamp], end: Option[Timestamp]) = DB.withSession { implicit session =>
Cache.remove(key(id))
Elections.updateConfig(id, config, start, end)
}
Expand Down
31 changes: 20 additions & 11 deletions app/models/Models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ case class Election(
id: Long,
configuration: String,
state: String,
startDate: Timestamp,
endDate: Timestamp,
startDate: Option[Timestamp],
endDate: Option[Timestamp],
pks: Option[String],
resultsConfig: Option[String],
results: Option[String],
Expand Down Expand Up @@ -160,8 +160,8 @@ class Elections(tag: Tag)
def id = column[Long]("id", O.PrimaryKey)
def configuration = column[String]("configuration", O.NotNull, O.DBType("text"))
def state = column[String]("state", O.NotNull)
def startDate = column[Timestamp]("start_date", O.NotNull)
def endDate = column[Timestamp]("end_date", O.NotNull)
def startDate = column[Timestamp]("start_date", O.Nullable)
def endDate = column[Timestamp]("end_date", O.Nullable)
def pks = column[String]("pks", O.Nullable, O.DBType("text"))
def resultsConfig = column[String]("results_config", O.Nullable, O.DBType("text"))
def results = column[String]("results", O.Nullable, O.DBType("text"))
Expand All @@ -174,8 +174,8 @@ class Elections(tag: Tag)
id,
configuration,
state,
startDate,
endDate,
startDate.?,
endDate.?,
pks.?,
resultsConfig.?,
results.?,
Expand Down Expand Up @@ -227,8 +227,17 @@ object Elections {
.update(Elections.RESULTS_OK, results, new Timestamp(new Date().getTime))
}

def updateConfig(id: Long, config: String, start: Timestamp, end: Timestamp)(implicit s: Session) = {
elections.filter(_.id === id).map(e => (e.configuration, e.startDate, e.endDate)).update(config, start, end)
def updateConfig(id: Long, config: String, start: Option[Timestamp], end: Option[Timestamp])(implicit s: Session) = {
if (start.isEmpty && end.isEmpty) {
elections.filter(_.id === id).map(e => (e.configuration)).update(config)
} else if (start.isDefined && end.isEmpty) {
elections.filter(_.id === id).map(e => (e.configuration, e.startDate)).update(config, start.get)
} else if (start.isEmpty && end.isDefined) {
elections.filter(_.id === id).map(e => (e.configuration, e.endDate)).update(config, end.get)
} else {
elections.filter(_.id === id).map(e => (e.configuration, e.startDate, e.endDate)).update(config, start.get, end.get)
}

}

def setPublicKeys(id: Long, pks: String)(implicit s: Session) = {
Expand All @@ -250,8 +259,8 @@ case class ElectionDTO(
id: Long,
configuration: ElectionConfig,
state: String,
startDate: Timestamp,
endDate: Timestamp,
startDate: Option[Timestamp],
endDate: Option[Timestamp],
pks: Option[String],
resultsConfig: Option[String],
results: Option[String],
Expand All @@ -263,7 +272,7 @@ case class ElectionDTO(

/** an election configuration defines an election */
case class ElectionConfig(id: Long, layout: String, director: String, authorities: Array[String], title: String, description: String,
questions: Array[Question], start_date: Timestamp, end_date: Timestamp, presentation: ElectionPresentation, real: Boolean, extra_data: Option[String], resultsConfig: Option[String], virtual: Boolean, virtualSubelections: Option[Array[Long]], logo_url: Option[String])
questions: Array[Question], start_date: Option[Timestamp], end_date: Option[Timestamp], presentation: ElectionPresentation, real: Boolean, extra_data: Option[String], resultsConfig: Option[String], virtual: Boolean, virtualSubelections: Option[Array[Long]], logo_url: Option[String])
{

/**
Expand Down
20 changes: 20 additions & 0 deletions conf/evolutions/default/7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# --- !Ups

ALTER TABLE election ALTER COLUMN start_date DROP DEFAULT;
ALTER TABLE election ALTER COLUMN start_date DROP NOT NULL;
UPDATE election SET start_date=NULL where start_date='2000-01-01 00:00:00.001';

ALTER TABLE election ALTER COLUMN end_date DROP DEFAULT;
ALTER TABLE election ALTER COLUMN end_date DROP NOT NULL;
UPDATE election SET end_date=NULL where end_date='2000-01-01 00:00:00.001';


# --- !Downs

ALTER TABLE election ALTER COLUMN start_date SET DEFAULT '2000-01-01 00:00:00.001';
UPDATE election SET start_date='2000-01-01 00:00:00.001' where start_date IS NULL;
ALTER TABLE election ALTER COLUMN start_date SET NOT NULL;

ALTER TABLE election ALTER COLUMN end_date SET DEFAULT '2000-01-01 00:00:00.001';
UPDATE election SET end_date='2000-01-01 00:00:00.001' where end_date IS NULL;
ALTER TABLE election ALTER COLUMN end_date SET NOT NULL;

0 comments on commit 1475826

Please sign in to comment.