admin: add button and route to reset some parts
This commit is contained in:
parent
d4110c70c8
commit
a43c5fbd55
4 changed files with 65 additions and 0 deletions
|
@ -2,6 +2,7 @@ package api
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
|
@ -16,6 +17,8 @@ var TeamsDir string
|
|||
func init() {
|
||||
router.GET("/api/settings.json", apiHandler(getSettings))
|
||||
router.PUT("/api/settings.json", apiHandler(saveSettings))
|
||||
|
||||
router.POST("/api/reset", apiHandler(reset))
|
||||
}
|
||||
|
||||
func getSettings(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
|
@ -38,3 +41,22 @@ func saveSettings(_ httprouter.Params, body []byte) (interface{}, error) {
|
|||
return config, err
|
||||
}
|
||||
}
|
||||
|
||||
func reset(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
var m map[string]string
|
||||
if err := json.Unmarshal(body, &m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if t, ok := m["type"]; !ok {
|
||||
return nil, errors.New("Field type not found")
|
||||
} else if t == "teams" {
|
||||
return true, fic.ResetTeams()
|
||||
} else if t == "challenges" {
|
||||
return true, fic.ResetExercices()
|
||||
} else if t == "game" {
|
||||
return true, fic.ResetGame()
|
||||
} else {
|
||||
return nil, errors.New("Unknown reset type")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -280,6 +280,13 @@ angular.module("FICApp")
|
|||
var f = new Date(ts + 120000 + this.duration * 60000);
|
||||
this.config.end = f.toISOString();
|
||||
}
|
||||
$scope.reset = function(type) {
|
||||
if (confirm("Êtes-vous sûr ?")) {
|
||||
$http.post("/api/reset", {"type": type}).success(function(time) {
|
||||
$location.url("/");
|
||||
});
|
||||
}
|
||||
};
|
||||
})
|
||||
|
||||
.controller("PublicController", function($scope, Scene, Theme, Teams, Exercice) {
|
||||
|
|
|
@ -102,3 +102,9 @@
|
|||
<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-save" aria-hidden="true"></span> Propager ces paramètres</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div class="well">
|
||||
<a ng-click="reset('challenges')" class="btn btn-warning" role="button"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Effacer les challenges et les thèmes</a>
|
||||
<a ng-click="reset('teams');" class="btn btn-warning" role="button"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Effacer les équipes</a>
|
||||
<a ng-click="reset('game');" class="btn btn-warning" role="button"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Effacer la partie (tentatives, indices, ...)</a>
|
||||
</div>
|
||||
|
|
30
libfic/reset.go
Normal file
30
libfic/reset.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package fic
|
||||
|
||||
import ()
|
||||
|
||||
func truncateTable(tables ...string) (error) {
|
||||
if _, err := DBExec("SET FOREIGN_KEY_CHECKS = 0;"); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, table := range tables {
|
||||
if _, err := DBExec("TRUNCATE TABLE " + table + ";"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := DBExec("SET FOREIGN_KEY_CHECKS = 1;"); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ResetGame() (error) {
|
||||
return truncateTable("team_hints", "key_found", "exercice_solved", "exercice_tries")
|
||||
}
|
||||
|
||||
func ResetExercices() (error) {
|
||||
return truncateTable("team_hints", "exercice_files", "key_found", "exercice_keys", "exercice_solved", "exercice_tries", "exercice_hints", "exercices", "themes")
|
||||
}
|
||||
|
||||
func ResetTeams() (error) {
|
||||
return truncateTable("team_hints", "key_found", "exercice_solved", "exercice_tries", "team_members", "teams")
|
||||
}
|
Reference in a new issue