diff --git a/admin/api_theme.go b/admin/api_theme.go index e0bcb87c..599c4794 100644 --- a/admin/api_theme.go +++ b/admin/api_theme.go @@ -23,7 +23,8 @@ type exportedExercice struct { } type exportedTheme struct { - Name string `json:"name"` + Name string `json:"name"` + Authors string `json:"authors"` Exercices map[string]exportedExercice `json:"exercices"` } @@ -46,6 +47,7 @@ func exportThemes() (interface{}, error) { } ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{ theme.Name, + theme.Authors, exos, } } @@ -108,7 +110,8 @@ func listTheme(args []string, body []byte) (interface{}, error) { } type uploadedTheme struct { - Name string + Name string + Authors string } func creationTheme(args []string, body []byte) (interface{}, error) { @@ -129,7 +132,7 @@ func creationTheme(args []string, body []byte) (interface{}, error) { return nil, errors.New("Theme's name not filled") } - return fic.CreateTheme(ut.Name) + return fic.CreateTheme(ut.Name, ut.Authors) } else { return nil, nil } diff --git a/admin/fill_exercices.sh b/admin/fill_exercices.sh index 3014c395..91248456 100755 --- a/admin/fill_exercices.sh +++ b/admin/fill_exercices.sh @@ -6,7 +6,8 @@ CLOUDPASS=fic:'f>t\nV33R|(+?$i*' new_theme() { NAME=`echo $1 | sed 's/"/\\\\"/g'` - curl -f -s -d "{\"name\": \"$NAME\"}" "${BASEURL}/api/themes/" | + AUTHORS=`echo $2 | sed 's/"/\\\\"/g'` + curl -f -s -d "{\"name\": \"$NAME\", \"authors\": \"$AUTHORS\"}" "${BASEURL}/api/themes/" | grep -Eo '"id":[0-9]+,' | grep -Eo "[0-9]+" } @@ -43,10 +44,12 @@ new_key() { } # Theme -curl -f -s -X PROPFIND -u "${CLOUDPASS}" "${BASEURI}" | xmllint --format - | grep 'd:href' | sed -E 's/^.*>(.*)<.*$/\1/' | sed 1d | while read f; do basename "$f"; done | while read THEME_URI +curl -f -s -X PROPFIND -u "${CLOUDPASS}" "${BASEURI}" | xmllint --format - | grep 'd:href' | sed -E 's/^.*>(.*)<.*$/\1/' | sed 1d | tac | while read f; do basename "$f"; done | while read THEME_URI do + THM_BASEURI="/${THEME_URI}/" THEME_NAME=$(echo "${THEME_URI}" | sed -E 's/%20/ /g' | sed -E 's/%c3%a9/é/g' | sed -E 's/%c3%a8/è/g') - THEME_ID=`new_theme "$THEME_NAME"` + THEME_AUTHORS=$(curl -f -s -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}/AUTHORS.txt" | sed 's/$/,/' | xargs) + THEME_ID=`new_theme "$THEME_NAME" "$THEME_AUTHORS"` if [ -z "$THEME_ID" ]; then echo -e "\e[31;01m!!! An error occured during theme add\e[00m" continue @@ -56,7 +59,6 @@ do LAST=null EXO_NUM=0 - THM_BASEURI="/${THEME_URI}/" curl -f -s -X PROPFIND -u "${CLOUDPASS}" "${BASEURI}${THM_BASEURI}" | xmllint --format - | grep 'd:href' | sed -E 's/^.*>(.*)<.*$/\1/' | sed -E 's/%20/ /g' | sed -E 's/%c3%a9/é/g' | sed -E 's/%c3%a8/è/g' | sed 1d | while read f; do basename "$f"; done | while read EXO_NAME do if ! echo $EXO_NAME | grep Exercice > /dev/null diff --git a/libfic/db.go b/libfic/db.go index 52f495c4..711ca48a 100644 --- a/libfic/db.go +++ b/libfic/db.go @@ -23,7 +23,8 @@ func DBCreate() error { _, err := db.Exec(` CREATE TABLE IF NOT EXISTS themes( id_theme INTEGER NOT NULL PRIMARY KEY, - name TEXT NOT NULL + name TEXT NOT NULL UNIQUE, + authors TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS teams( id_team INTEGER NOT NULL PRIMARY KEY, diff --git a/libfic/theme.go b/libfic/theme.go index bb2e6fc8..9c8fe6fa 100644 --- a/libfic/theme.go +++ b/libfic/theme.go @@ -3,12 +3,13 @@ package fic import () type Theme struct { - Id int64 `json:"id"` - Name string `json:"name"` + Id int64 `json:"id"` + Name string `json:"name"` + Authors string `json:"authors"` } func GetThemes() ([]Theme, error) { - if rows, err := DBQuery("SELECT id_theme, name FROM themes"); err != nil { + if rows, err := DBQuery("SELECT id_theme, name, authors FROM themes"); err != nil { return nil, err } else { defer rows.Close() @@ -16,7 +17,7 @@ func GetThemes() ([]Theme, error) { var themes = make([]Theme, 0) for rows.Next() { var t Theme - if err := rows.Scan(&t.Id, &t.Name); err != nil { + if err := rows.Scan(&t.Id, &t.Name, &t.Authors); err != nil { return nil, err } themes = append(themes, t) @@ -31,25 +32,25 @@ func GetThemes() ([]Theme, error) { func GetTheme(id int) (Theme, error) { var t Theme - if err := DBQueryRow("SELECT id_theme, name FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name); err != nil { + if err := DBQueryRow("SELECT id_theme, name, authors FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name, &t.Authors); err != nil { return t, err } return t, nil } -func CreateTheme(name string) (Theme, error) { - if res, err := DBExec("INSERT INTO themes (name) VALUES (?)", name); err != nil { +func CreateTheme(name string, authors string) (Theme, error) { + if res, err := DBExec("INSERT INTO themes (name, authors) VALUES (?, ?)", name, authors); err != nil { return Theme{}, err } else if tid, err := res.LastInsertId(); err != nil { return Theme{}, err } else { - return Theme{tid, name}, nil + return Theme{tid, name, authors}, nil } } func (t Theme) Update() (int64, error) { - if res, err := DBExec("UPDATE themes SET name = ? WHERE id_theme = ?", t.Name, t.Id); err != nil { + if res, err := DBExec("UPDATE themes SET name = ?, authors = ? WHERE id_theme = ?", t.Name, t.Authors, t.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err