admin: display file dependancies and be able to remove them

This commit is contained in:
nemunaire 2019-07-21 23:46:23 +02:00
parent c8ece39cb2
commit 8131fda0e7
5 changed files with 93 additions and 3 deletions

View File

@ -3,6 +3,8 @@ package api
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors"
"fmt"
"srs.epita.fr/fic-server/admin/sync" "srs.epita.fr/fic-server/admin/sync"
"srs.epita.fr/fic-server/libfic" "srs.epita.fr/fic-server/libfic"
@ -18,6 +20,8 @@ func init() {
router.PUT("/api/files/:fileid", apiHandler(fileHandler(updateFile))) router.PUT("/api/files/:fileid", apiHandler(fileHandler(updateFile)))
router.DELETE("/api/files/:fileid", apiHandler(fileHandler(deleteFile))) router.DELETE("/api/files/:fileid", apiHandler(fileHandler(deleteFile)))
router.DELETE("/api/files/:fileid/dependancies/:depid", apiHandler(fileDependancyHandler(deleteFileDep)))
router.GET("/api/exercices/:eid/files", apiHandler(exerciceHandler(listExerciceFiles))) router.GET("/api/exercices/:eid/files", apiHandler(exerciceHandler(listExerciceFiles)))
router.POST("/api/exercices/:eid/files", apiHandler(exerciceHandler(createExerciceFile))) router.POST("/api/exercices/:eid/files", apiHandler(exerciceHandler(createExerciceFile)))
@ -35,13 +39,51 @@ func init() {
}))) })))
} }
type APIFile struct {
fic.EFile
Depends []fic.Flag `json:"depends,omitempty"`
}
func genFileList(in []fic.EFile, e error) (out []APIFile, err error) {
if e != nil {
return nil, e
}
for _, f := range in {
g := APIFile{EFile: f}
var deps []fic.Flag
deps, err = f.GetDepends()
if err != nil {
return
}
for _, d := range deps {
if k, ok := d.(fic.FlagKey); ok {
k, err = fic.GetFlagKey(k.Id)
if err != nil {
return
}
g.Depends = append(g.Depends, k)
} else {
err = errors.New(fmt.Sprintf("Unknown type %T to handle file dependancy", k))
return
}
}
out = append(out, g)
}
return
}
func listFiles(_ httprouter.Params, body []byte) (interface{}, error) { func listFiles(_ httprouter.Params, body []byte) (interface{}, error) {
// List all files return genFileList(fic.GetFiles())
return fic.GetFiles()
} }
func listExerciceFiles(exercice fic.Exercice, body []byte) (interface{}, error) { func listExerciceFiles(exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetFiles() return genFileList(exercice.GetFiles())
} }
func clearFiles(_ httprouter.Params, _ []byte) (interface{}, error) { func clearFiles(_ httprouter.Params, _ []byte) (interface{}, error) {
@ -92,6 +134,10 @@ func deleteFile(file fic.EFile, _ []byte) (interface{}, error) {
return file.Delete() return file.Delete()
} }
func deleteFileDep(file fic.EFile, depid int64, _ []byte) (interface{}, error) {
return true, file.DeleteDepend(fic.FlagKey{Id: depid})
}
func checkFile(file fic.EFile, _ []byte) (interface{}, error) { func checkFile(file fic.EFile, _ []byte) (interface{}, error) {
return true, file.CheckFileOnDisk() return true, file.CheckFileOnDisk()
} }

View File

@ -316,6 +316,18 @@ func fileHandler(f func(fic.EFile, []byte) (interface{}, error)) func(httprouter
} }
} }
func fileDependancyHandler(f func(fic.EFile, int64, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
if depid, err := strconv.ParseInt(string(ps.ByName("depid")), 10, 64); err != nil {
return nil, err
} else {
return fileHandler(func(file fic.EFile, b []byte) (interface{}, error) {
return f(file, depid, b)
})(ps, body)
}
}
}
func certificateHandler(f func(fic.Certificate, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) { func certificateHandler(f func(fic.Certificate, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) { return func(ps httprouter.Params, body []byte) (interface{}, error) {
if certid, err := strconv.ParseUint(strings.TrimSuffix(string(ps.ByName("certid")), ".p12"), 10, 64); err != nil { if certid, err := strconv.ParseUint(strings.TrimSuffix(string(ps.ByName("certid")), ".p12"), 10, 64); err != nil {

View File

@ -1335,6 +1335,18 @@ angular.module("FICApp")
$rootScope.staticFilesNeedUpdate++; $rootScope.staticFilesNeedUpdate++;
return false; return false;
} }
$scope.deleteFileDep = function() {
$http({
url: "/api//files/" + this.file.id + "/dependancies/" + this.dep.id,
method: "DELETE"
}).then(function(response) {
$scope.files = ExerciceFile.query({ exerciceId: $routeParams.exerciceId });
}, function(response) {
$rootScope.newBox('danger', 'An error occurs when removing file dep:', response.data.errmsg);
});
$rootScope.staticFilesNeedUpdate++;
return false;
}
$scope.saveFile = function() { $scope.saveFile = function() {
this.file.$update(); this.file.$update();
$rootScope.staticFilesNeedUpdate++; $rootScope.staticFilesNeedUpdate++;

View File

@ -57,6 +57,16 @@
Taille&nbsp;: <span title="{{ file.size }} octets">{{ file.size | size }}</span> &dash; Taille&nbsp;: <span title="{{ file.size }} octets">{{ file.size | size }}</span> &dash;
BLAKE2b&nbsp;: <samp title="{{ file.checksum | cksum }}">{{ file.checksum | cksum }}</samp> BLAKE2b&nbsp;: <samp title="{{ file.checksum | cksum }}">{{ file.checksum | cksum }}</samp>
</div> </div>
<div>
Dépend de la validation de :
<span ng-if="!file.depends">aucun flag</span>
<ul ng-if="file.depends">
<li ng-repeat="dep in file.depends">
{{ dep.label }}
<button type="button" class="btn btn-sm btn-danger" ng-click="deleteFileDep()"><span class="glyphicon glyphicon-trash"></span></button>
</li>
</ul>
</div>
</form> </form>
</div> </div>
</div> </div>

View File

@ -300,6 +300,16 @@ func (f EFile) AddDepend(j Flag) (err error) {
return return
} }
// DeleteDepend insert a new dependency to a given flag.
func (f EFile) DeleteDepend(j Flag) (err error) {
if k, ok := j.(FlagKey); ok {
_, err = DBExec("DELETE FROM exercice_files_deps WHERE id_file = ? AND id_flag = ?", f.Id, k.Id)
} else {
err = errors.New("Dependancy type not implemented for this file.")
}
return
}
// GetDepends retrieve the flag's dependency list. // GetDepends retrieve the flag's dependency list.
func (f EFile) GetDepends() ([]Flag, error) { func (f EFile) GetDepends() ([]Flag, error) {
if rows, err := DBQuery("SELECT id_flag FROM exercice_files_deps WHERE id_file = ?", f.Id); err != nil { if rows, err := DBQuery("SELECT id_flag FROM exercice_files_deps WHERE id_file = ?", f.Id); err != nil {