From e9910fe827bb361309f0b37ab78893b7d8f91fb1 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Thu, 21 Dec 2017 21:25:23 +0100 Subject: [PATCH] admin: can delete team history item --- admin/api/team.go | 19 ++++++++++++++++ admin/static/js/app.js | 13 ++++++++++- admin/static/views/team-edit.html | 9 +++++++- libfic/team_history.go | 38 +++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/admin/api/team.go b/admin/api/team.go index 3dd1c017..02f64d42 100644 --- a/admin/api/team.go +++ b/admin/api/team.go @@ -5,6 +5,7 @@ import ( "fmt" "strconv" "strings" + "time" "srs.epita.fr/fic-server/libfic" @@ -63,6 +64,7 @@ func init() { return fic.GetTeamsStats(nil) } }))) + router.DELETE("/api/teams/:tid/history.json", apiHandler(teamPublicHandler(delHistory))) router.GET("/api/teams/:tid/tries", apiHandler(teamPublicHandler( func(team *fic.Team, _ []byte) (interface{}, error) { return fic.GetTries(team, nil) }))) @@ -211,3 +213,20 @@ func dispMemberTeamName(ps httprouter.Params, body []byte) (interface{}, error) return team.InitialName, nil } } + + +type uploadedHistory struct { + Kind string + Time time.Time + Primary *int64 + Secondary *int64 +} + +func delHistory(team *fic.Team, body []byte) (interface{}, error) { + var uh uploadedHistory + if err := json.Unmarshal(body, &uh); err != nil { + return nil, err + } + + return team.DelHistoryItem(uh.Kind, uh.Time, uh.Primary, uh.Secondary) +} diff --git a/admin/static/js/app.js b/admin/static/js/app.js index ac299eed..bb7b9b89 100644 --- a/admin/static/js/app.js +++ b/admin/static/js/app.js @@ -891,8 +891,19 @@ angular.module("FICApp") $location.url("/teams/" + $scope.team.id + "/stats"); } }) - .controller("TeamHistoryController", function($scope, TeamHistory, $routeParams) { + .controller("TeamHistoryController", function($scope, TeamHistory, $routeParams, $http, $rootScope) { $scope.history = TeamHistory.query({ teamId: $routeParams.teamId }); + $scope.delHistory = function(row) { + $http({ + url: "/api/teams/" + $routeParams.teamId + "/history.json", + method: "DELETE", + data: row + }).then(function(response) { + $scope.history = TeamHistory.query({ teamId: $routeParams.teamId }); + }, function(response) { + $rootScope.newBox('danger', 'An error occurs when removing history item: ', response.data); + }); + } }) .controller("TeamStatsController", function($scope, TeamStats, $routeParams) { $scope.teamstats = TeamStats.get({ teamId: $routeParams.teamId }); diff --git a/admin/static/views/team-edit.html b/admin/static/views/team-edit.html index 43b31daa..3f971bee 100644 --- a/admin/static/views/team-edit.html +++ b/admin/static/views/team-edit.html @@ -7,6 +7,7 @@ +
@@ -90,7 +91,9 @@ - + +
{{ row.time | date:"mediumTime" }}
{{ row.kind }}
+ {{ row.time | date:"mediumTime" }}
{{ row.kind }} +
{{ row.primary_title }} @@ -102,7 +105,11 @@ (coeff x{{ row.secondary }}) : {{ row.secondary }} + +
+
diff --git a/libfic/team_history.go b/libfic/team_history.go index 6a629a70..4bfb3d5f 100644 --- a/libfic/team_history.go +++ b/libfic/team_history.go @@ -44,3 +44,41 @@ func (t Team) GetHistory() ([]map[string]interface{}, error) { return hist, nil } + +func (t Team) DelHistoryItem(kind string, h time.Time, primary *int64, secondary *int64) (interface{}, error) { + if kind == "tries" && primary != nil { + if res, err := DBExec("DELETE FROM exercice_tries WHERE id_team = ? AND time = ? AND id_exercice = ?", t.Id, h, *primary); err != nil { + return 0, err + } else if nb, err := res.RowsAffected(); err != nil { + return 0, err + } else { + return nb, err + } + } else if kind == "hint" && primary != nil { + if res, err := DBExec("DELETE FROM team_hints WHERE id_team = ? AND time = ? AND id_exercice = ?", t.Id, h, *primary); err != nil { + return 0, err + } else if nb, err := res.RowsAffected(); err != nil { + return 0, err + } else { + return nb, err + } + } else if kind == "key_found" && secondary != nil { + if res, err := DBExec("DELETE FROM key_found WHERE id_team = ? AND time = ? AND id_key = ?", t.Id, h, *secondary); err != nil { + return 0, err + } else if nb, err := res.RowsAffected(); err != nil { + return 0, err + } else { + return nb, err + } + } else if kind == "solved" && primary != nil { + if res, err := DBExec("DELETE FROM exercice_solved WHERE id_team = ? AND time = ? AND id_exercice = ?", t.Id, h, *primary); err != nil { + return 0, err + } else if nb, err := res.RowsAffected(); err != nil { + return 0, err + } else { + return nb, err + } + } else { + return nil, nil + } +}