frontend: beautiful URLs

This commit is contained in:
nemunaire 2018-01-18 11:07:50 +01:00 committed by nemunaire
parent bd75157a79
commit 0c540a39eb
13 changed files with 54 additions and 31 deletions

View file

@ -120,7 +120,7 @@ func createExercice(theme fic.Theme, body []byte) (interface{}, error) {
}
}
return theme.AddExercice(ue.Title, ue.Path, ue.Statement, ue.Overview, depend, ue.Gain, ue.VideoURI)
return theme.AddExercice(ue.Title, ue.URLId, ue.Path, ue.Statement, ue.Overview, depend, ue.Gain, ue.VideoURI)
}

View file

@ -108,6 +108,7 @@ func showThemedExercice(theme fic.Theme, exercice fic.Exercice, body []byte) (in
type uploadedTheme struct {
Name string
URLId string
Authors string
Intro string
}
@ -122,7 +123,7 @@ func createTheme(_ httprouter.Params, body []byte) (interface{}, error) {
return nil, errors.New("Theme's name not filled")
}
return fic.CreateTheme(ut.Name, ut.Authors, ut.Intro)
return fic.CreateTheme(ut.Name, ut.URLId, ut.Authors, ut.Intro)
}
func updateTheme(theme fic.Theme, body []byte) (interface{}, error) {

View file

@ -816,7 +816,7 @@ angular.module("FICApp")
})
.controller("ThemeController", function($scope, Theme, $routeParams, $location, $rootScope, $http) {
$scope.theme = Theme.get({ themeId: $routeParams.themeId });
$scope.fields = ["name", "authors", "intro"];
$scope.fields = ["name", "urlid", "authors", "intro"];
$scope.saveTheme = function() {
if (this.theme.id) {
@ -914,7 +914,7 @@ angular.module("FICApp")
$scope.exercice = Exercice.get({ exerciceId: $routeParams.exerciceId });
}
$scope.exercices = Exercice.query();
$scope.fields = ["title", "statement", "overview", "depend", "gain", "coefficient", "videoURI"];
$scope.fields = ["title", "urlid", "statement", "overview", "depend", "gain", "coefficient", "videoURI"];
$scope.saveExercice = function() {
if (this.exercice.id) {

View file

@ -98,7 +98,7 @@ func SyncExercices(i Importer, theme fic.Theme) []string {
}
if e, err := theme.GetExerciceByTitle(ename); err != nil {
if ex, err := theme.AddExercice(ename, path.Join(theme.Name, edir), statement, overview, nil, gain, videoURI); err != nil {
if ex, err := theme.AddExercice(ename, fic.ToURLid(ename), path.Join(theme.Name, edir), statement, overview, nil, gain, videoURI); err != nil {
errs = append(errs, fmt.Sprintf("%q: error on exercice add: %s", edir, err))
continue
} else {

View file

@ -50,7 +50,7 @@ func SyncThemes(i Importer) []string {
} else if intro, err = getFileContent(i, path.Join(tname, "introduction.txt")); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to get introduction: %s", tname, err))
} else if theme, err = fic.GetThemeByName(tname); err != nil {
if _, err := fic.CreateTheme(tname, strings.Join(authors, ", "), intro); err != nil {
if _, err := fic.CreateTheme(tname, fic.ToURLid(tname), strings.Join(authors, ", "), intro); err != nil {
errs = append(errs, fmt.Sprintf("%q: an error occurs during add: %s", tname, err))
continue
}

View file

@ -75,7 +75,7 @@
</button>
<div class="collapse navbar-collapse" id="themesMenu">
<ul class="navbar-nav mr-auto">
<li ng-repeat="(k,theme) in themes" ng-class="{active: k == current_theme}" class="nav-item"><a class="nav-link" ng-href="/{{ k }}" data-toggle="tab">{{ theme.name }} <span class="badge badge-light"><span class="glyphicon glyphicon-fire" aria-hidden="true" ng-if="max_solved > 1 && theme.solved == max_solved" alt="Déjà {{ theme.solved }} challenges résolus dans ce thème"></span> <span class="glyphicon glyphicon-gift" aria-hidden="true" ng-if="theme.exercice_coeff_max > 1" alt="Des bonus existent pour au moins un challenge de ce thème"></span> <span ng-if="(my.team_id)">{{ theme.exercice_solved }}/</span>{{ theme.exercice_count }}</span></a></li>
<li ng-repeat="(k,theme) in themes" ng-class="{active: k == current_theme}" class="nav-item"><a class="nav-link" ng-href="/{{ theme.urlid }}" data-toggle="tab">{{ theme.name }} <span class="badge badge-light"><span class="glyphicon glyphicon-fire" aria-hidden="true" ng-if="max_solved > 1 && theme.solved == max_solved" alt="Déjà {{ theme.solved }} challenges résolus dans ce thème"></span> <span class="glyphicon glyphicon-gift" aria-hidden="true" ng-if="theme.exercice_coeff_max > 1" alt="Des bonus existent pour au moins un challenge de ce thème"></span> <span ng-if="(my.team_id)">{{ theme.exercice_solved }}/</span>{{ theme.exercice_count }}</span></a></li>
</ul>
</div>
<span class="navbar-text text-light" ng-show="(my.team_id)" ng-cloak>

View file

@ -107,12 +107,16 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
$scope.themes = response.data;
$scope.max_gain = 0;
$scope.max_solved = 0;
$scope.themesUrl = {};
$scope.exercicesUrl = {};
angular.forEach(response.data, function(theme, key) {
$scope.themesUrl[theme.urlid] = key;
this[key].exercice_count = Object.keys(theme.exercices).length;
this[key].exercice_coeff_max = 0;
this[key].gain = 0;
this[key].solved = 0;
angular.forEach(theme.exercices, function(ex, k) {
$scope.exercicesUrl[ex.urlid] = k;
this.gain += ex.gain;
this.solved += ex.solved;
this.exercice_coeff_max = Math.max(this.exercice_coeff_max, ex.curcoeff);
@ -160,10 +164,10 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
$rootScope.refresh();
})
.controller("ExerciceController", function($scope, $routeParams, $http, $rootScope, $timeout) {
$rootScope.current_theme = $routeParams.theme;
$rootScope.current_theme = $scope.themesUrl[$routeParams.theme];
if ($routeParams.exercice) {
$rootScope.current_exercice = $routeParams.exercice;
$rootScope.current_exercice = $scope.exercicesUrl[$routeParams.exercice];
} else {
if ($scope.themes && $scope.my && $scope.themes[$scope.current_theme]) {
var exos = Object.keys($scope.themes[$scope.current_theme].exercices);

View file

@ -1,6 +1,6 @@
<nav role="navigation">
<ol class="breadcrumb">
<li ng-repeat="(k,exercice) in themes[current_theme].exercices" class="breadcrumb-item" ng-class="{active: k == current_exercice, disabled: !my.exercices[k]}"><a ng-if="(k != current_exercice && !my.exercices[k])">{{ exercice.title }}</a><a ng-href="/{{ current_theme }}/{{ k }}" ng-if="(k != current_exercice && my.exercices[k])">{{ exercice.title }} <span class="glyphicon glyphicon-gift" aria-hidden="true" ng-if="themes[current_theme].exercices[k].curcoeff > 1.0"></span> <span class="glyphicon glyphicon-ok" aria-hidden="true" ng-if="(my.team_id && my.exercices[k].solved)"></span></a><em ng-if="k == current_exercice">{{ exercice.title }}</em></li>
<li ng-repeat="(k,exercice) in themes[current_theme].exercices" class="breadcrumb-item" ng-class="{active: k == current_exercice, disabled: !my.exercices[k]}"><a ng-if="(k != current_exercice && !my.exercices[k])">{{ exercice.title }}</a><a ng-href="/{{ themes[current_theme].urlid }}/{{ themes[current_theme].exercices[k].urlid }}" ng-if="(k != current_exercice && my.exercices[k])">{{ exercice.title }} <span class="glyphicon glyphicon-gift" aria-hidden="true" ng-if="themes[current_theme].exercices[k].curcoeff > 1.0"></span> <span class="glyphicon glyphicon-ok" aria-hidden="true" ng-if="(my.team_id && my.exercices[k].solved)"></span></a><em ng-if="k == current_exercice">{{ exercice.title }}</em></li>
</ol>
</nav>
@ -95,7 +95,7 @@
<p class="card-text">
Vous êtes la {{ my.exercices[current_exercice].solved_rank }}<sup><ng-pluralize count="my.exercices[current_exercice].solved_rank" when="{'one': 're', 'other': 'e'}"></ng-pluralize></sup> équipe à avoir résolu ce challenge à {{ my.exercices[current_exercice].solved_time | date:"mediumTime" }}. Vous avez marqué <ng-pluralize count="my.exercices[current_exercice].gain" when="{'one': '{} point', 'other': '{} points'}"></ng-pluralize> !
</p>
<a href="/{{ current_theme }}" class="btn btn-success">Passer au challenge suivant</a>
<a href="/{{ themes[current_theme].urlid }}" class="btn btn-success">Passer au challenge suivant</a>
</div>
</div>

View file

@ -66,6 +66,7 @@ CREATE TABLE IF NOT EXISTS events(
CREATE TABLE IF NOT EXISTS themes(
id_theme INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL UNIQUE,
url_id VARCHAR(255) NOT NULL UNIQUE,
intro TEXT NOT NULL,
authors TEXT NOT NULL
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
@ -100,6 +101,7 @@ CREATE TABLE IF NOT EXISTS exercices(
id_exercice INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_theme INTEGER NOT NULL,
title VARCHAR(255) NOT NULL,
url_id VARCHAR(255) NOT NULL,
path VARCHAR(255) NOT NULL,
statement TEXT NOT NULL,
overview TEXT NOT NULL,

View file

@ -11,6 +11,7 @@ var PartialMCQValidation bool
type Exercice struct {
Id int64 `json:"id"`
Title string `json:"title"`
URLId string `json:"urlid"`
Path string `json:"path"`
Statement string `json:"statement"`
Overview string `json:"overview"`
@ -22,7 +23,7 @@ type Exercice struct {
func GetExercice(id int64) (Exercice, error) {
var e Exercice
if err := DBQueryRow("SELECT id_exercice, title, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.Title, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
if err := DBQueryRow("SELECT id_exercice, title, url_id, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
return Exercice{}, err
}
@ -31,7 +32,7 @@ func GetExercice(id int64) (Exercice, error) {
func (t Theme) GetExercice(id int) (Exercice, error) {
var e Exercice
if err := DBQueryRow("SELECT id_exercice, title, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.Title, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
if err := DBQueryRow("SELECT id_exercice, title, url_id, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
return Exercice{}, err
}
@ -40,7 +41,7 @@ func (t Theme) GetExercice(id int) (Exercice, error) {
func (t Theme) GetExerciceByTitle(title string) (Exercice, error) {
var e Exercice
if err := DBQueryRow("SELECT id_exercice, title, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_theme = ? AND title = ?", t.Id, title).Scan(&e.Id, &e.Title, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
if err := DBQueryRow("SELECT id_exercice, title, url_id, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_theme = ? AND title = ?", t.Id, title).Scan(&e.Id, &e.Title, &t.URLId, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
return Exercice{}, err
}
@ -48,7 +49,7 @@ func (t Theme) GetExerciceByTitle(title string) (Exercice, error) {
}
func GetExercices() ([]Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, title, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices"); err != nil {
if rows, err := DBQuery("SELECT id_exercice, title, url_id, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices"); err != nil {
return nil, err
} else {
defer rows.Close()
@ -56,7 +57,7 @@ func GetExercices() ([]Exercice, error) {
var exos = make([]Exercice, 0)
for rows.Next() {
var e Exercice
if err := rows.Scan(&e.Id, &e.Title, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
if err := rows.Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
return nil, err
}
exos = append(exos, e)
@ -70,7 +71,7 @@ func GetExercices() ([]Exercice, error) {
}
func (t Theme) GetExercices() ([]Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, title, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_theme = ?", t.Id); err != nil {
if rows, err := DBQuery("SELECT id_exercice, title, url_id, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_theme = ?", t.Id); err != nil {
return nil, err
} else {
defer rows.Close()
@ -78,7 +79,7 @@ func (t Theme) GetExercices() ([]Exercice, error) {
var exos = make([]Exercice, 0)
for rows.Next() {
var e Exercice
if err := rows.Scan(&e.Id, &e.Title, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
if err := rows.Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
return nil, err
}
exos = append(exos, e)
@ -91,28 +92,28 @@ func (t Theme) GetExercices() ([]Exercice, error) {
}
}
func (t Theme) AddExercice(title string, path string, statement string, overview string, depend *Exercice, gain int64, videoURI string) (Exercice, error) {
func (t Theme) AddExercice(title string, urlId string, path string, statement string, overview string, depend *Exercice, gain int64, videoURI string) (Exercice, error) {
var dpd interface{}
if depend == nil {
dpd = nil
} else {
dpd = depend.Id
}
if res, err := DBExec("INSERT INTO exercices (id_theme, title, path, statement, overview, depend, gain, video_uri) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", t.Id, title, path, statement, overview, dpd, gain, videoURI); err != nil {
if res, err := DBExec("INSERT INTO exercices (id_theme, title, url_id, path, statement, overview, depend, gain, video_uri) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", t.Id, title, urlId, path, statement, overview, dpd, gain, videoURI); err != nil {
return Exercice{}, err
} else if eid, err := res.LastInsertId(); err != nil {
return Exercice{}, err
} else {
if depend == nil {
return Exercice{eid, title, path, statement, overview, nil, gain, 1.0, videoURI}, nil
return Exercice{eid, title, urlId, path, statement, overview, nil, gain, 1.0, videoURI}, nil
} else {
return Exercice{eid, title, path, statement, overview, &depend.Id, gain, 1.0, videoURI}, nil
return Exercice{eid, title, urlId, path, statement, overview, &depend.Id, gain, 1.0, videoURI}, nil
}
}
}
func (e Exercice) Update() (int64, error) {
if res, err := DBExec("UPDATE exercices SET title = ?, path = ?, statement = ?, overview = ?, depend = ?, gain = ?, coefficient_cur = ?, video_uri = ? WHERE id_exercice = ?", e.Title, e.Path, e.Statement, e.Overview, e.Depend, e.Gain, e.Coefficient, e.VideoURI, e.Id); err != nil {
if res, err := DBExec("UPDATE exercices SET title = ?, url_id = ?, path = ?, statement = ?, overview = ?, depend = ?, gain = ?, coefficient_cur = ?, video_uri = ? WHERE id_exercice = ?", e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Depend, e.Gain, e.Coefficient, e.VideoURI, e.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err

View file

@ -5,12 +5,13 @@ import ()
type Theme struct {
Id int64 `json:"id"`
Name string `json:"name"`
URLId string `json:"urlid"`
Authors string `json:"authors,omitempty"`
Intro string `json:"intro,omitempty"`
}
func GetThemes() ([]Theme, error) {
if rows, err := DBQuery("SELECT id_theme, name, authors, intro FROM themes"); err != nil {
if rows, err := DBQuery("SELECT id_theme, name, url_id, authors, intro FROM themes"); err != nil {
return nil, err
} else {
defer rows.Close()
@ -18,7 +19,7 @@ func GetThemes() ([]Theme, error) {
var themes = make([]Theme, 0)
for rows.Next() {
var t Theme
if err := rows.Scan(&t.Id, &t.Name, &t.Authors, &t.Intro); err != nil {
if err := rows.Scan(&t.Id, &t.Name, &t.URLId, &t.Authors, &t.Intro); err != nil {
return nil, err
}
themes = append(themes, t)
@ -33,7 +34,7 @@ func GetThemes() ([]Theme, error) {
func GetTheme(id int) (Theme, error) {
var t Theme
if err := DBQueryRow("SELECT id_theme, name, authors, intro FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name, &t.Authors, &t.Intro); err != nil {
if err := DBQueryRow("SELECT id_theme, name, url_id, authors, intro FROM themes WHERE id_theme=?", id).Scan(&t.Id, &t.Name, &t.URLId, &t.Authors, &t.Intro); err != nil {
return t, err
}
@ -42,25 +43,25 @@ func GetTheme(id int) (Theme, error) {
func GetThemeByName(name string) (Theme, error) {
var t Theme
if err := DBQueryRow("SELECT id_theme, name, authors, intro FROM themes WHERE name=?", name).Scan(&t.Id, &t.Name, &t.Authors, &t.Intro); err != nil {
if err := DBQueryRow("SELECT id_theme, name, url_id, authors, intro FROM themes WHERE name=?", name).Scan(&t.Id, &t.Name, &t.URLId, &t.Authors, &t.Intro); err != nil {
return t, err
}
return t, nil
}
func CreateTheme(name string, authors string, intro string) (Theme, error) {
if res, err := DBExec("INSERT INTO themes (name, authors, intro) VALUES (?, ?, ?)", name, authors, intro); err != nil {
func CreateTheme(name string, url_id string, authors string, intro string) (Theme, error) {
if res, err := DBExec("INSERT INTO themes (name, url_id, authors, intro) VALUES (?, ?, ?, ?)", name, url_id, authors, intro); err != nil {
return Theme{}, err
} else if tid, err := res.LastInsertId(); err != nil {
return Theme{}, err
} else {
return Theme{tid, name, authors, intro}, nil
return Theme{tid, name, url_id, authors, intro}, nil
}
}
func (t Theme) Update() (int64, error) {
if res, err := DBExec("UPDATE themes SET name = ?, authors = ?, intro = ? WHERE id_theme = ?", t.Name, t.Authors, t.Intro, t.Id); err != nil {
if res, err := DBExec("UPDATE themes SET name = ?, url_id = ?, authors = ?, intro = ? WHERE id_theme = ?", t.Name, t.URLId, t.Authors, t.Intro, t.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err

View file

@ -6,6 +6,7 @@ import (
type ExportedExercice struct {
Title string `json:"title"`
URLId string `json:"urlid"`
Gain int64 `json:"gain"`
Coeff float64 `json:"curcoeff"`
Solved int64 `json:"solved"`
@ -14,6 +15,7 @@ type ExportedExercice struct {
type exportedTheme struct {
Name string `json:"name"`
URLId string `json:"urlid"`
Authors string `json:"authors"`
Intro string `json:"intro"`
Exercices map[string]ExportedExercice `json:"exercices"`
@ -32,6 +34,7 @@ func ExportThemes() (interface{}, error) {
for _, exercice := range exercices {
exos[fmt.Sprintf("%d", exercice.Id)] = ExportedExercice{
exercice.Title,
exercice.URLId,
exercice.Gain,
exercice.Coefficient,
exercice.SolvedCount(),
@ -40,6 +43,7 @@ func ExportThemes() (interface{}, error) {
}
ret[fmt.Sprintf("%d", theme.Id)] = exportedTheme{
theme.Name,
theme.URLId,
theme.Authors,
theme.Intro,
exos,

10
libfic/utils.go Normal file
View file

@ -0,0 +1,10 @@
package fic
import (
"regexp"
)
func ToURLid(str string) string {
re := regexp.MustCompile("[^a-zA-Z0-9]+")
return re.ReplaceAllLiteralString(str, "-")
}