diff --git a/internal/models/election.go b/internal/models/election.go index 970cfd4330c6d942d71bc9049401ce18f2d66f7c..e62a10a21aca9e384dfc6d248c268e4f1b3e3244 100644 --- a/internal/models/election.go +++ b/internal/models/election.go @@ -74,6 +74,16 @@ func (d *DataHandler) postElection(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + + var elections []Election + d.db.Find(&elections) + for _, val := range elections { + if o.Name == val.Name { + http.Error(w, "Name Already exist.", 409) + return + } + } + d.db.Create(&o) d.db.Last(&o) json.NewEncoder(w).Encode(o) diff --git a/web/components/management/election.js b/web/components/management/election.js index a7b7f3e0a533eeeede5a4bdf29b26e4f4f26be14..ffbcb2a0bec474d37b4075e2efb316668af71c15 100644 --- a/web/components/management/election.js +++ b/web/components/management/election.js @@ -256,6 +256,12 @@ class Election { }), } ); + if (response.status == 409) { + throw new Error( + `The name of the election already exist (status ${response.status})` + + ); + } if (response.status !== 200) { throw new Error( `Election could not be updated or created (status ${response.status})` @@ -266,6 +272,7 @@ class Election { } catch (e) { Messages.Show("is-warning", e.message); console.error(e); + return } Common.toggleModal("election-modal", "election-modal-card"); this.activateElection(election);