From bd5ac4cc99e2578f0b392a43bd95fd0e8dbf7f51 Mon Sep 17 00:00:00 2001 From: Findeton Date: Sat, 23 Nov 2024 06:15:26 +0100 Subject: [PATCH 1/6] wip --- app/controllers/ElectionsApi.scala | 37 ++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/app/controllers/ElectionsApi.scala b/app/controllers/ElectionsApi.scala index 3a131cb..9832942 100644 --- a/app/controllers/ElectionsApi.scala +++ b/app/controllers/ElectionsApi.scala @@ -1509,8 +1509,41 @@ object ElectionsApi /** Future: deletes an election and its votes */ private def deleteElection(id: Long) = Future { - val result = DAL.elections.delete(id) - Ok(response("ok")) + getElection(id) + .flatMap { + election => { + val configJson = Json.parse(election.configuration) + val config = configJson.validate[ElectionConfig].get + val url = eoUrl(config.director, "public_api/delete") + WS.url(url).post( + Json.obj( + "election_id" -> id, + ) + ).map { resp => + if(resp.status == HTTP.OK) { + setTrusteeKeysState(election, deleteRequest.authority_id, TrusteeKeysStates.DELETED) + Ok(response(resp.body)) + } + else { + BadRequest(error(s"EO returned status ${resp.status} with body ${resp.body}", ErrorCodes.EO_ERROR)) + } + } + } + }.flatMap { + _ => { + DAL.elections.delete(id) + }.flatMap { + _ => { + Ok(response("ok")) + } + }.recover { + case e: NoSuchElementException => BadRequest(error(s"Election $id not found", ErrorCodes.NO_ELECTION)) + case t: Throwable => { + t.printStackTrace() + Logger.warn(s"Exception caught when checking share: $t") + BadRequest(error(t.getMessage)) + } + } }(slickExecutionContext) /** Future: updates an election's config */ From 82d6febd3926f18fd7feeb1563a1150103d542ce Mon Sep 17 00:00:00 2001 From: Findeton Date: Sat, 23 Nov 2024 06:22:35 +0100 Subject: [PATCH 2/6] wip --- app/controllers/ElectionsApi.scala | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/ElectionsApi.scala b/app/controllers/ElectionsApi.scala index 9832942..00657d9 100644 --- a/app/controllers/ElectionsApi.scala +++ b/app/controllers/ElectionsApi.scala @@ -1530,12 +1530,11 @@ object ElectionsApi } } }.flatMap { - _ => { + _ => DAL.elections.delete(id) }.flatMap { - _ => { - Ok(response("ok")) - } + _ => + Ok(response("ok")) }.recover { case e: NoSuchElementException => BadRequest(error(s"Election $id not found", ErrorCodes.NO_ELECTION)) case t: Throwable => { From 8a5b7b8098f8e84cde210774e194346c168f3a59 Mon Sep 17 00:00:00 2001 From: Findeton Date: Sat, 23 Nov 2024 06:26:19 +0100 Subject: [PATCH 3/6] wip --- app/controllers/ElectionsApi.scala | 48 ++++++++++++++---------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/app/controllers/ElectionsApi.scala b/app/controllers/ElectionsApi.scala index 00657d9..f644b97 100644 --- a/app/controllers/ElectionsApi.scala +++ b/app/controllers/ElectionsApi.scala @@ -1510,41 +1510,37 @@ object ElectionsApi /** Future: deletes an election and its votes */ private def deleteElection(id: Long) = Future { getElection(id) - .flatMap { - election => { - val configJson = Json.parse(election.configuration) - val config = configJson.validate[ElectionConfig].get - val url = eoUrl(config.director, "public_api/delete") - WS.url(url).post( - Json.obj( - "election_id" -> id, - ) - ).map { resp => - if(resp.status == HTTP.OK) { - setTrusteeKeysState(election, deleteRequest.authority_id, TrusteeKeysStates.DELETED) - Ok(response(resp.body)) - } - else { - BadRequest(error(s"EO returned status ${resp.status} with body ${resp.body}", ErrorCodes.EO_ERROR)) - } + .flatMap { election => + val configJson = Json.parse(election.configuration) + val config = configJson.validate[ElectionConfig].get + val url = eoUrl(config.director, "public_api/delete") + WS.url(url).post( + Json.obj( + "election_id" -> id + ) + ).map { resp => + if (resp.status == HTTP.OK) { + setTrusteeKeysState(election, deleteRequest.authority_id, TrusteeKeysStates.DELETED) + Ok(response(resp.body)) + } else { + BadRequest(error(s"EO returned status ${resp.status} with body ${resp.body}", ErrorCodes.EO_ERROR)) } } - }.flatMap { - _ => - DAL.elections.delete(id) - }.flatMap { - _ => - Ok(response("ok")) + }.flatMap { _ => + DAL.elections.delete(id) + }.map { _ => + Ok(response("ok")) }.recover { - case e: NoSuchElementException => BadRequest(error(s"Election $id not found", ErrorCodes.NO_ELECTION)) - case t: Throwable => { + case e: NoSuchElementException => + BadRequest(error(s"Election $id not found", ErrorCodes.NO_ELECTION)) + case t: Throwable => t.printStackTrace() Logger.warn(s"Exception caught when checking share: $t") BadRequest(error(t.getMessage)) - } } }(slickExecutionContext) + /** Future: updates an election's config */ private def updateElection(id: Long, request: Request[JsValue]) = Future { From 89be281322cd5f34a43b0b7c88f3ac9bfa6a4085 Mon Sep 17 00:00:00 2001 From: Findeton Date: Sat, 23 Nov 2024 06:27:19 +0100 Subject: [PATCH 4/6] wip --- app/controllers/ElectionsApi.scala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/controllers/ElectionsApi.scala b/app/controllers/ElectionsApi.scala index f644b97..9e7d2b2 100644 --- a/app/controllers/ElectionsApi.scala +++ b/app/controllers/ElectionsApi.scala @@ -1520,7 +1520,6 @@ object ElectionsApi ) ).map { resp => if (resp.status == HTTP.OK) { - setTrusteeKeysState(election, deleteRequest.authority_id, TrusteeKeysStates.DELETED) Ok(response(resp.body)) } else { BadRequest(error(s"EO returned status ${resp.status} with body ${resp.body}", ErrorCodes.EO_ERROR)) @@ -1535,7 +1534,7 @@ object ElectionsApi BadRequest(error(s"Election $id not found", ErrorCodes.NO_ELECTION)) case t: Throwable => t.printStackTrace() - Logger.warn(s"Exception caught when checking share: $t") + Logger.warn(s"Exception caught when deleting election: $t") BadRequest(error(t.getMessage)) } }(slickExecutionContext) From c8f17e6162c9dc2d89c72c1a5e4129e28c18f1dd Mon Sep 17 00:00:00 2001 From: Findeton Date: Sat, 23 Nov 2024 06:30:24 +0100 Subject: [PATCH 5/6] wip --- app/controllers/ElectionsApi.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/ElectionsApi.scala b/app/controllers/ElectionsApi.scala index 9e7d2b2..80f4286 100644 --- a/app/controllers/ElectionsApi.scala +++ b/app/controllers/ElectionsApi.scala @@ -1526,7 +1526,7 @@ object ElectionsApi } } }.flatMap { _ => - DAL.elections.delete(id) + Future.successful(DAL.elections.delete(id)) }.map { _ => Ok(response("ok")) }.recover { From 80a766c40088b454b9944f841cae6cd576ae8d7c Mon Sep 17 00:00:00 2001 From: Findeton Date: Sat, 23 Nov 2024 06:37:39 +0100 Subject: [PATCH 6/6] wip --- app/controllers/ElectionsApi.scala | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/ElectionsApi.scala b/app/controllers/ElectionsApi.scala index 80f4286..b556200 100644 --- a/app/controllers/ElectionsApi.scala +++ b/app/controllers/ElectionsApi.scala @@ -1508,7 +1508,7 @@ object ElectionsApi } /** Future: deletes an election and its votes */ - private def deleteElection(id: Long) = Future { + private def deleteElection(id: Long): Future[Result] = { getElection(id) .flatMap { election => val configJson = Json.parse(election.configuration) @@ -1525,9 +1525,8 @@ object ElectionsApi BadRequest(error(s"EO returned status ${resp.status} with body ${resp.body}", ErrorCodes.EO_ERROR)) } } - }.flatMap { _ => - Future.successful(DAL.elections.delete(id)) }.map { _ => + DAL.elections.delete(id) Ok(response("ok")) }.recover { case e: NoSuchElementException => @@ -1537,7 +1536,7 @@ object ElectionsApi Logger.warn(s"Exception caught when deleting election: $t") BadRequest(error(t.getMessage)) } - }(slickExecutionContext) + } /** Future: updates an election's config */