diff --git a/election-api-scala/README.md b/election-api-scala/README.md index 8f3a830..f91d48d 100644 --- a/election-api-scala/README.md +++ b/election-api-scala/README.md @@ -21,7 +21,7 @@ The API has 3 endpoints: During your assessment we will ask you to work though the task in `tasks.md` with a pair. Please do not work on or complete these prior to the assessment. -:warning: If you make any changes to the code, please ensure you return it to it's initial (HEAD) state before your assessment. +__Warning:__ If you make any changes to the code, please ensure you return it to it's initial (HEAD) state before your assessment. ## Prerequisites - Java 11 @@ -36,6 +36,10 @@ During your assessment we will ask you to work though the task in `tasks.md` wit `sbt run` +You may get a port error when you try to run this. If so, run the following command: + +`export PLAY_HTTP_PORT=9001` + ### To Test `sbt test` diff --git a/election-api-scala/app/controllers/ResultsController.scala b/election-api-scala/app/controllers/ResultsController.scala index 0da79fe..9216329 100644 --- a/election-api-scala/app/controllers/ResultsController.scala +++ b/election-api-scala/app/controllers/ResultsController.scala @@ -45,7 +45,6 @@ class ResultsController @Inject()(val controllerComponents: ControllerComponents } def getScoreboard: Action[AnyContent] = Action { - - Ok(Json.toJson(Scoreboard(0))) + Ok(Json.toJson(None)) } } diff --git a/election-api-scala/app/model/PartyResult.scala b/election-api-scala/app/model/PartyResult.scala index 258fc28..00b2b59 100644 --- a/election-api-scala/app/model/PartyResult.scala +++ b/election-api-scala/app/model/PartyResult.scala @@ -5,5 +5,7 @@ case class ConstituencyResult(id: Int, name: String, seqNo: Int, partyResults: S case class ApiResponse (error: String, message: String) - -case class Scoreboard(declared:Int) \ No newline at end of file +// TODO: this class should hold: +// - the overall winner (if there is one) +// - the seats that each party wins in Parliament +case class Scoreboard(winner: String) \ No newline at end of file diff --git a/election-api-scala/tasks.md b/election-api-scala/tasks.md index 7da3e62..14252a0 100644 --- a/election-api-scala/tasks.md +++ b/election-api-scala/tasks.md @@ -1,11 +1,11 @@ ### Task + The product owner has asked that we implement the scoreboard endpoint. -First version: First Past the Post +#### First Past the Post -For each constituency a winner can be declared: -1. if we have votes for that constituency -2. The winning party is which ever party received a plurality of votes, e.g. whoever receives the most votes +For each constituency a winner can be declared if we have votes for at least one party in that constituency. +The winning party is which ever party received a plurality of votes, e.g. whoever receives the most votes. Someone wins in the UK if they receive half of the constituency seats in Parliament (325). So if a party has received 325 or more seats it can be declared the winner overall. @@ -16,16 +16,10 @@ The scoreboard should show: - The seats for each party - The overall winner (i.e. the party with 325 or more seats) if there is one -There is a case class [Scoreboard](app/model/PartyResult.scala) ready to be extended and an +There is a case class [Scoreboard](app/model/PartyResult.scala) which you should alter to show the above results and an endpoint controller to put your implementation: [ResultsController](app/controllers/ResultsController.scala). You will also need to complete the [tests](test/controllers/ResultsControllerSpec.scala) as part of your implementation. Bonus information for the scoreboard: - The total votes for each party - The total share of the vote for each party. So the percentage of votes for each party. - - -### Possible other implementations - -- Absolute majority required. Someone needs 50% + 1 votes or a run off is triggered (check the data that's probably all constituencies) -- Allocate the seats from the total declarations based on % of vote share \ No newline at end of file diff --git a/election-api-scala/test/controllers/ResultsControllerSpec.scala b/election-api-scala/test/controllers/ResultsControllerSpec.scala index 0e7b33c..fe01020 100644 --- a/election-api-scala/test/controllers/ResultsControllerSpec.scala +++ b/election-api-scala/test/controllers/ResultsControllerSpec.scala @@ -19,47 +19,37 @@ class ResultsControllerSpec extends PlaySpec with GuiceOneAppPerTest with Inject "Test first 5 results" in { val scoreboard = runXResults(5) - scoreboard match { - case Some(sc) => matchScoreboard(scoreboard = sc, LabSeats = 4, LDSeats = 1) - case None => fail("Didn't return a scoreboard") - } + scoreboard must not be empty + // LAB = 4 + // LD = 1 + // winner = noone } "First 100 results" in { val scoreboard = runXResults(100) - scoreboard match { - case Some(sc) => matchScoreboard(scoreboard = sc, LabSeats = 56, LDSeats = 12, ConSeats = 31) - case None => fail("Didn't return a scoreboard") - } + scoreboard must not be empty + // LD == 12 + // LAB == 56 + // CON == 31 + // winner = noone } "First 554 results" in { val scoreboard = runXResults(554) - scoreboard match { - case Some(sc) => matchScoreboard(scoreboard = sc, LabSeats = 325, LDSeats = 52, ConSeats = 167, winner = Some("LAB")) - case None => fail("Didn't return a scoreboard") - } + scoreboard must not be empty + // LD == 52 + // LAB = 325 + // CON = 167 + // winner = LAB } "All results" in { val scoreboard = runXResults(650) - scoreboard match { - case Some(sc) => matchScoreboard(scoreboard = sc, LabSeats = 349, LDSeats = 62, ConSeats = 210, winner = Some("LAB")) - case None => fail("Didn't return a scoreboard") - } - } - - def matchScoreboard(scoreboard: Scoreboard, LabSeats: Int = 0, - LDSeats: Int = 0, ConSeats: Int = 0, winner: Option[String] = None) { - var ld, lab, con = 0 - - //TODO: set the seats by party - - ld mustEqual LDSeats - lab mustEqual LabSeats - con mustEqual ConSeats -// something mustEqual winner - fail("Implement me") + scoreboard must not be empty + // LD == 62 + // LAB == 349 + // CON == 210 + // winner = LAB } def runXResults(number: Int): Option[Scoreboard] = {