Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Delete Multiple Elections #213

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/controllers/ElectionsApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ object ElectionsApi
registerElection(request, id)
}

/** deletes an election and its votes */
def delete(id: Long) = HActionAdmin("", "AuthEvent", id, "edit|delete").async { request =>
deleteElection(id)
}

/** updates an election's config */
def update(id: Long) = HActionAdmin("", "AuthEvent", id, "edit|update").async(BodyParsers.parse.json) { request =>
updateElection(id, request)
Expand Down Expand Up @@ -1502,6 +1507,12 @@ object ElectionsApi
promise.future
}

/** Future: deletes an election and its votes */
private def deleteElection(id: Long) = Future {
val result = DAL.elections.delete(id)
Ok(response("ok"))
}(slickExecutionContext)

/** Future: updates an election's config */
private def updateElection(id: Long, request: Request[JsValue]) = Future {

Expand Down
2 changes: 2 additions & 0 deletions app/models/DAL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ object DAL {
}

def delete(id: Long) = DB.withSession { implicit session =>
// remove votes, then election
Votes.deleteByElectionId(id)
val ret = Elections.delete(id)
Cache.remove(key(id))
ret
Expand Down
2 changes: 2 additions & 0 deletions app/models/Models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ object Votes {

def findByElectionId(electionId: Long)(implicit s: Session): List[Vote] = votes.filter(_.electionId === electionId).list

def deleteByElectionId(electionId: Long)(implicit s: Session) = votes.filter(_.electionId === electionId).delete

def findByElectionIdRange(electionId: Long, drop: Long, take: Long)(implicit s: Session): List[Vote] = {
votes.filter(_.electionId === electionId).sortBy(_.created.desc).drop(drop).take(take).list
}
Expand Down
1 change: 1 addition & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ POST /api/election/:id/unpublish-results controllers.Electi
GET /api/election/:id/results controllers.ElectionsApi.getResults(id: Long)
GET /api/election/:id/voters controllers.ElectionsApi.getElectionVoters(id: Long)
GET /api/election/:id/stats controllers.ElectionsApi.getElectionStats(id: Long)
POST /api/election/:id/delete controllers.ElectionsApi.delete(id: Long)
GET /api/authorities controllers.ElectionsApi.getAuthorities
GET /api/authorities-state controllers.ElectionsApi.getAuthoritiesState

Expand Down
Loading