admin: can perform mass editing on exercices

This commit is contained in:
nemunaire 2018-11-21 01:19:13 +01:00 committed by Pierre-Olivier Mercier
parent 1166a925fe
commit 6b54704d59
4 changed files with 97 additions and 9 deletions

View File

@ -17,6 +17,7 @@ func init() {
router.GET("/api/exercices/:eid", apiHandler(exerciceHandler(showExercice)))
router.PUT("/api/exercices/:eid", apiHandler(exerciceHandler(updateExercice)))
router.PATCH("/api/exercices/:eid", apiHandler(exerciceHandler(partUpdateExercice)))
router.DELETE("/api/exercices/:eid", apiHandler(exerciceHandler(deleteExercice)))
router.GET("/api/exercices/:eid/files", apiHandler(exerciceHandler(listExerciceFiles)))
@ -116,6 +117,51 @@ func updateExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
return ue, nil
}
func partUpdateExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
var ue fic.Exercice
if err := json.Unmarshal(body, &ue); err != nil {
return nil, err
}
if len(ue.Title) > 0 {
exercice.Title = ue.Title
}
if len(ue.URLId) > 0 {
exercice.URLId = ue.URLId
}
if len(ue.Statement) > 0 {
exercice.Statement = ue.Statement
}
if len(ue.Overview) > 0 {
exercice.Overview = ue.Overview
}
if ue.Depend != nil {
exercice.Depend = ue.Depend
}
if ue.Gain != 0 {
exercice.Gain = ue.Gain
}
if ue.Coefficient != 0 {
exercice.Coefficient = ue.Coefficient
}
if len(ue.VideoURI) > 0 {
exercice.VideoURI = ue.VideoURI
}
if _, err := exercice.Update(); err != nil {
return nil, err
}
return exercice, nil
}
func createExercice(theme fic.Theme, body []byte) (interface{}, error) {
// Create a new exercice
var ue fic.Exercice

View File

@ -205,7 +205,8 @@ angular.module("FICApp")
})
.factory("Exercice", function($resource) {
return $resource("/api/exercices/:exerciceId", { exerciceId: '@id' }, {
update: {method: 'PUT'}
update: {method: 'PUT'},
patch: {method: 'PATCH'}
})
})
.factory("ExerciceTags", function($resource) {
@ -962,10 +963,27 @@ angular.module("FICApp")
};
})
.controller("AllExercicesListController", function($scope, Exercice, $routeParams, $location, $rootScope, $http) {
.controller("AllExercicesListController", function($scope, Exercice, $routeParams, $location, $rootScope, $http, $filter) {
$scope.exercices = Exercice.query();
$scope.exercice = {}; // Array used to save fields to updates in selected exercices
$scope.fields = ["title", "overview"];
$scope.toggleSelectAll = function() {
angular.forEach($filter('filter')($scope.exercices, $scope.query), function(ex) {
ex.selected = !$scope.selectall
})
}
$scope.updateExercices = function() {
angular.forEach($scope.exercices, function(ex) {
if (ex.selected) {
Exercice.patch({exerciceId: ex.id}, $scope.exercice);
}
})
$scope.exercice = {};
$rootScope.newBox('success', 'Édition de masse terminée avec succès');
}
$scope.show = function(id) {
$location.url("/exercices/" + id);
};

View File

@ -24,17 +24,41 @@
<table class="table table-hover table-bordered table-striped table-sm">
<thead class="thead-dark">
<tr>
<th>
<input type="checkbox" ng-click="toggleSelectAll()" ng-model="selectall">
</th>
<th ng-repeat="field in fields">
{{ field }}
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="exercice in exercices | filter: query" ng-click="show(exercice.id)">
<td ng-repeat="field in fields">
<tr ng-repeat="exercice in exercices | filter: query">
<td>
<input type="checkbox" ng-model="exercice.selected">
</td>
<td ng-repeat="field in fields" ng-click="show(exercice.id)">
{{ exercice[field] }}
</td>
</tr>
</tbody>
</table>
</div>
<form ng-submit="updateExercices()">
<fieldset>
<legend class="text-dark">Édition de masse <button type="submit" class="float-right btn btn-sm btn-success"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button></legend>
<div class="form-group row" ng-repeat="field in ['gain','coefficient']">
<label for="{{ field }}" class="col-sm-1 col-form-label-sm">{{ field | capitalize }}</label>
<div class="col-sm-11">
<input type="text" class="form-control form-control-sm" id="{{ field }}" ng-model="exercice[field]" ng-if="field != 'statement' && field != 'overview' && field != 'depend' && field != 'gain' && field != 'coefficient'">
<input type="text" class="form-control form-control-sm" id="{{ field }}" ng-model="exercice[field]" ng-if="field == 'gain'" integer>
<input type="text" class="form-control form-control-sm" id="{{ field }}" ng-model="exercice[field]" ng-if="field == 'coefficient'" float>
<textarea class="form-control form-control-sm" id="{{field}}" ng-model="exercice[field]" ng-if="field == 'statement' || field == 'overview'"></textarea>
<select class="form-control form-control-sm" id="{{field}}" ng-model="exercice[field]" ng-options="ex.id as ex.title group by ex.path.split('/')[0] for ex in exercices" ng-if="field == 'depend'">
<option value="">Aucune</option>
</select>
</div>
</div>
</fieldset>
</form>

View File

@ -6,11 +6,11 @@
<div class="form-group row" ng-repeat="field in fields">
<label for="{{ field }}" class="col-sm-1 col-form-label-sm">{{ field | capitalize }}</label>
<div class="col-sm-11">
<input type="text" class="form-control form-control-sm" id="{{ field }}" ng-model="exercice[field]" ng-show="field != 'statement' && field != 'overview' && field != 'depend' && field != 'gain' && field != 'coefficient'">
<input type="text" class="form-control form-control-sm" id="{{ field }}" ng-model="exercice[field]" ng-show="field == 'gain'" integer>
<input type="text" class="form-control form-control-sm" id="{{ field }}" ng-model="exercice[field]" ng-show="field == 'coefficient'" float>
<textarea class="form-control form-control-sm" id="{{field}}" ng-model="exercice[field]" ng-show="field == 'statement' || field == 'overview'"></textarea>
<select class="form-control form-control-sm" id="{{field}}" ng-model="exercice[field]" ng-options="ex.id as ex.title group by ex.path.split('/')[0] for ex in exercices" ng-show="field == 'depend'">
<input type="text" class="form-control form-control-sm" id="{{ field }}" ng-model="exercice[field]" ng-if="field != 'statement' && field != 'overview' && field != 'depend' && field != 'gain' && field != 'coefficient'">
<input type="text" class="form-control form-control-sm" id="{{ field }}" ng-model="exercice[field]" ng-if="field == 'gain'" integer>
<input type="text" class="form-control form-control-sm" id="{{ field }}" ng-model="exercice[field]" ng-if="field == 'coefficient'" float>
<textarea class="form-control form-control-sm" id="{{field}}" ng-model="exercice[field]" ng-if="field == 'statement' || field == 'overview'"></textarea>
<select class="form-control form-control-sm" id="{{field}}" ng-model="exercice[field]" ng-options="ex.id as ex.title group by ex.path.split('/')[0] for ex in exercices" ng-if="field == 'depend'">
<option value="">Aucune</option>
</select>
</div>