Skip to content
Snippets Groups Projects
Commit 5ad47d1c authored by Alexis Poyen's avatar Alexis Poyen
Browse files

Feat : delete deskRounds linked to a Desk on deletion

parent e5cd1be8
No related branches found
No related tags found
1 merge request!19Feat : delete deskRounds linked to a Desk on deletion
Pipeline #5674 passed
......@@ -110,10 +110,15 @@ func (d *DataHandler) putDesk(w http.ResponseWriter, r *http.Request, id int) {
func (d *DataHandler) deleteDesk(w http.ResponseWriter, r *http.Request, id int) {
if id != 0 {
var o Desk
if err := d.db.First(&o, id).Error; err != nil {
if err := d.db.Preload("DeskRounds").First(&o, id).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
for _, deskRound := range o.DeskRounds {
d.deleteDeskRound(deskRound)
}
d.db.Delete(&o)
} else {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
......
......@@ -79,6 +79,5 @@ func (d *DataHandler) createDeskRound(roundID uint, deskID uint) {
}
func (d *DataHandler) deleteDeskRound(deskRound DeskRound) {
d.db.Delete(&deskRound)
}
......@@ -98,6 +98,11 @@ func appTests(t *testing.T) {
do("GET", "/api/Round/1", xsrfHeader, ``, 200, `{"ID":1,"ElectionID":1,"Parameter":{"ID":0,"CountBlankAndNull":false,"ShowOnlyCompleted":false,"ShowMap":false},"Date":"2020-06-28","Round":1,"DeskRounds":[{"ID":1,"RoundID":1,"DeskID":1,"Capturers":null,"Completed":false,"DateCompletion":"0001-01-01T00:00:00Z","Validated":false,"Votes":null}],"CandidateLists":[]}`)
do("GET", "/api/Desk/1", xsrfHeader, ``, 200, `{"ID":1,"SectionID":1,"Name":"Desk 1","WitnessDesk":true,"Subscribed":9587,"DeskRounds":[{"ID":1,"RoundID":1,"DeskID":1,"Capturers":null,"Completed":false,"DateCompletion":"0001-01-01T00:00:00Z","Validated":false,"Votes":null}]}`)
// Verify that on Desk deletion deskRounds are deleted
do("GET", "/api/Desk/1", xsrfHeader, ``, 200, `{"ID":1,"SectionID":1,"Name":"Desk 1","WitnessDesk":true,"Subscribed":9587,"DeskRounds":[{"ID":1,"RoundID":1,"DeskID":1,"Capturers":null,"Completed":false,"DateCompletion":"0001-01-01T00:00:00Z","Validated":false,"Votes":null}]}`)
do("DELETE", "/api/Desk/1", xsrfHeader, ``, 200, ``)
do("GET", "/api/DeskRound/1", xsrfHeader, ``, 404, `id is missing`)
}
// Do an OAuth2 login with an known admin
do("GET", "/OAuth2Login", noH, "", 200, "<!DOCTYPE html>")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment