admin: can delete team history item

This commit is contained in:
nemunaire 2017-12-21 21:25:23 +01:00 committed by Pierre-Olivier Mercier
parent a0737d91b9
commit e9910fe827
4 changed files with 77 additions and 2 deletions

View File

@ -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)
}

View File

@ -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 });

View File

@ -7,6 +7,7 @@
</button>
</h1>
<div class="row">
<div class="col-lg-8">
<form ng-submit="saveTeam()">
@ -90,7 +91,9 @@
<table ng-controller="TeamHistoryController" class="table table-hover table-striped table-bordered">
<tbody>
<tr ng-repeat="row in history" ng-class="{success: row.kind == 'solved', info: row.kind == 'hint', warning: row.kind == 'key_found'}">
<td><nobr>{{ row.time | date:"mediumTime" }}</nobr><br>{{ row.kind }}</td>
<td>
<nobr>{{ row.time | date:"mediumTime" }}</nobr><br>{{ row.kind }}
</td>
<td>
<span ng-if="row.primary_title">
<a href="exercices/{{ row.primary }}">{{ row.primary_title }}</a>
@ -102,7 +105,11 @@
<span ng-if="!row.secondary_title && row.secondary && row.kind == 'solved'">(coeff x{{ row.secondary }})</span>
<span ng-if="!row.secondary_title && row.secondary && row.kind != 'solved'">: {{ row.secondary }}</span>
</td>
<td style="vertical-align: middle; padding: 0">
<button ng-click="delHistory(row)" class="float-right btn btn-sm btn-danger"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
</td>
</tr>
</tbody>
</table>
</div>
</div>

View File

@ -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
}
}