fic: Can overwrite authors for each exercice

This commit is contained in:
nemunaire 2023-06-14 21:04:56 +02:00
parent 1a8ebcb8bf
commit 28ad0fa791
5 changed files with 23 additions and 11 deletions

View File

@ -613,7 +613,7 @@ func createExercice(c *gin.Context) {
} }
} }
exercice, err := theme.AddExercice(ue.Title, ue.Image, ue.WIP, ue.URLId, ue.Path, ue.Statement, ue.Overview, ue.Headline, depend, ue.Gain, ue.VideoURI, ue.Resolution, ue.SeeAlso, ue.Finished) exercice, err := theme.AddExercice(ue.Title, ue.Authors, ue.Image, ue.WIP, ue.URLId, ue.Path, ue.Statement, ue.Overview, ue.Headline, depend, ue.Gain, ue.VideoURI, ue.Resolution, ue.SeeAlso, ue.Finished)
if err != nil { if err != nil {
log.Println("Unable to createExercice:", err.Error()) log.Println("Unable to createExercice:", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during exercice creation."}) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during exercice creation."})

View File

@ -1717,7 +1717,7 @@ angular.module("FICApp")
} }
}); });
$scope.exercices = Exercice.query(); $scope.exercices = Exercice.query();
$scope.fields = ["title", "urlid", "disabled", "statement", "headline", "overview", "finished", "depend", "gain", "coefficient", "videoURI", "image", "resolution", "issue", "issuekind", "wip"]; $scope.fields = ["title", "urlid", "authors", "disabled", "statement", "headline", "overview", "finished", "depend", "gain", "coefficient", "videoURI", "image", "resolution", "issue", "issuekind", "wip"];
$scope.inSync = false; $scope.inSync = false;
$scope.syncExo = function() { $scope.syncExo = function() {

View File

@ -139,6 +139,15 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
e.URLId = fic.ToURLid(e.Title) e.URLId = fic.ToURLid(e.Title)
e.Title = fixnbsp(e.Title) e.Title = fixnbsp(e.Title)
if i.Exists(path.Join(epath, "AUTHORS.txt")) {
if authors, err := getAuthors(i, epath); err != nil {
errs = append(errs, NewExerciceError(e, fmt.Errorf("unable to get AUTHORS.txt: %w", err)))
} else {
// Format authors
e.Authors = strings.Join(authors, ", ")
}
}
// Texts to format using Markdown // Texts to format using Markdown
if i.Exists(path.Join(epath, "overview.txt")) { if i.Exists(path.Join(epath, "overview.txt")) {
e.Overview, err = GetFileContent(i, path.Join(epath, "overview.txt")) e.Overview, err = GetFileContent(i, path.Join(epath, "overview.txt"))

View File

@ -142,6 +142,7 @@ CREATE TABLE IF NOT EXISTS exercices(
id_exercice INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, id_exercice INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_theme INTEGER NOT NULL, id_theme INTEGER NOT NULL,
title VARCHAR(255) NOT NULL, title VARCHAR(255) NOT NULL,
authors TEXT NOT NULL,
image VARCHAR(255) NOT NULL, image VARCHAR(255) NOT NULL,
disabled BOOLEAN NOT NULL DEFAULT 0, disabled BOOLEAN NOT NULL DEFAULT 0,
headline TEXT NOT NULL, headline TEXT NOT NULL,
@ -562,7 +563,7 @@ func DBRecreateDiscountedView() (err error) {
return return
} }
_, err = db.Exec("CREATE OR REPLACE VIEW exercices_discounted AS SELECT E.id_exercice, E.id_theme, E.title, E.image, E.disabled, E.headline, E.url_id, E.path, E.statement, E.overview, E.issue, E.issue_kind, E.depend, E.gain - " + fmt.Sprintf("%f", DiscountedFactor) + " * E.gain * (COUNT(*) - 1) AS gain, E.coefficient_cur, E.finished, E.video_uri, E.resolution, E.seealso FROM exercices E LEFT OUTER JOIN exercice_solved S ON S.id_exercice = E.id_exercice GROUP BY E.id_exercice;") _, err = db.Exec("CREATE OR REPLACE VIEW exercices_discounted AS SELECT E.id_exercice, E.id_theme, E.title, E.authors, E.image, E.disabled, E.headline, E.url_id, E.path, E.statement, E.overview, E.issue, E.issue_kind, E.depend, E.gain - " + fmt.Sprintf("%f", DiscountedFactor) + " * E.gain * (COUNT(*) - 1) AS gain, E.coefficient_cur, E.finished, E.video_uri, E.resolution, E.seealso FROM exercices E LEFT OUTER JOIN exercice_solved S ON S.id_exercice = E.id_exercice GROUP BY E.id_exercice;")
return return
} }

View File

@ -25,6 +25,7 @@ type Exercice struct {
IdTheme int64 `json:"id_theme"` IdTheme int64 `json:"id_theme"`
Language string `json:"lang,omitempty"` Language string `json:"lang,omitempty"`
Title string `json:"title"` Title string `json:"title"`
Authors string `json:"authors"`
Image string `json:"image"` Image string `json:"image"`
// Disabled indicates if the exercice is available to players now or not // Disabled indicates if the exercice is available to players now or not
Disabled bool `json:"disabled"` Disabled bool `json:"disabled"`
@ -74,7 +75,7 @@ func (e *Exercice) AnalyzeTitle() {
func getExercice(table, condition string, args ...interface{}) (*Exercice, error) { func getExercice(table, condition string, args ...interface{}) (*Exercice, error) {
var e Exercice var e Exercice
var tmpgain float64 var tmpgain float64
if err := DBQueryRow("SELECT id_exercice, id_theme, title, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM "+table+" "+condition, args...).Scan(&e.Id, &e.IdTheme, &e.Title, &e.Image, &e.Disabled, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &tmpgain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil { if err := DBQueryRow("SELECT id_exercice, id_theme, title, authors, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM "+table+" "+condition, args...).Scan(&e.Id, &e.IdTheme, &e.Title, &e.Authors, &e.Image, &e.Disabled, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &tmpgain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil {
return nil, err return nil, err
} }
e.Gain = int64(math.Trunc(tmpgain)) e.Gain = int64(math.Trunc(tmpgain))
@ -114,7 +115,7 @@ func GetDiscountedExercice(id int64) (*Exercice, error) {
// getExercices returns the list of all challenges present in the database. // getExercices returns the list of all challenges present in the database.
func getExercices(table string) ([]*Exercice, error) { func getExercices(table string) ([]*Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM " + table + " ORDER BY path ASC"); err != nil { if rows, err := DBQuery("SELECT id_exercice, id_theme, title, authors, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM " + table + " ORDER BY path ASC"); err != nil {
return nil, err return nil, err
} else { } else {
defer rows.Close() defer rows.Close()
@ -123,7 +124,7 @@ func getExercices(table string) ([]*Exercice, error) {
for rows.Next() { for rows.Next() {
e := &Exercice{} e := &Exercice{}
var tmpgain float64 var tmpgain float64
if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.Image, &e.Disabled, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &tmpgain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil { if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.Authors, &e.Image, &e.Disabled, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &tmpgain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil {
return nil, err return nil, err
} }
e.Gain = int64(math.Trunc(tmpgain)) e.Gain = int64(math.Trunc(tmpgain))
@ -152,7 +153,7 @@ func GetDiscountedExercices() ([]*Exercice, error) {
// GetExercices returns the list of all challenges in the Theme. // GetExercices returns the list of all challenges in the Theme.
func (t *Theme) GetExercices() ([]*Exercice, error) { func (t *Theme) GetExercices() ([]*Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? ORDER BY path ASC", t.Id); err != nil { if rows, err := DBQuery("SELECT id_exercice, id_theme, title, authors, image, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? ORDER BY path ASC", t.Id); err != nil {
return nil, err return nil, err
} else { } else {
defer rows.Close() defer rows.Close()
@ -160,7 +161,7 @@ func (t *Theme) GetExercices() ([]*Exercice, error) {
exos := []*Exercice{} exos := []*Exercice{}
for rows.Next() { for rows.Next() {
e := &Exercice{} e := &Exercice{}
if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.Image, &e.Disabled, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil { if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.Authors, &e.Image, &e.Disabled, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil {
return nil, err return nil, err
} }
e.AnalyzeTitle() e.AnalyzeTitle()
@ -223,7 +224,7 @@ func (t *Theme) addExercice(e *Exercice) (err error) {
if e.WIP { if e.WIP {
wip = "%" wip = "%"
} }
if res, err := DBExec("INSERT INTO exercices (id_theme, title, image, disabled, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, resolution, seealso, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "+ik+", "+cc+")", t.Id, wip+e.Title, e.Image, e.Disabled, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI, e.Resolution, e.SeeAlso); err != nil { if res, err := DBExec("INSERT INTO exercices (id_theme, title, authors, image, disabled, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, resolution, seealso, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "+ik+", "+cc+")", t.Id, wip+e.Title, e.Authors, e.Image, e.Disabled, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI, e.Resolution, e.SeeAlso); err != nil {
return err return err
} else if eid, err := res.LastInsertId(); err != nil { } else if eid, err := res.LastInsertId(); err != nil {
return err return err
@ -235,7 +236,7 @@ func (t *Theme) addExercice(e *Exercice) (err error) {
} }
// AddExercice creates and fills a new struct Exercice and registers it into the database. // AddExercice creates and fills a new struct Exercice and registers it into the database.
func (t *Theme) AddExercice(title string, image string, wip bool, urlId string, path string, statement string, overview string, headline string, depend *Exercice, gain int64, videoURI string, resolution string, seealso string, finished string) (e *Exercice, err error) { func (t *Theme) AddExercice(title string, authors string, image string, wip bool, urlId string, path string, statement string, overview string, headline string, depend *Exercice, gain int64, videoURI string, resolution string, seealso string, finished string) (e *Exercice, err error) {
var dpd *int64 = nil var dpd *int64 = nil
if depend != nil { if depend != nil {
dpd = &depend.Id dpd = &depend.Id
@ -243,6 +244,7 @@ func (t *Theme) AddExercice(title string, image string, wip bool, urlId string,
e = &Exercice{ e = &Exercice{
Title: title, Title: title,
Authors: authors,
Image: image, Image: image,
Disabled: false, Disabled: false,
WIP: wip, WIP: wip,
@ -271,7 +273,7 @@ func (e *Exercice) Update() (int64, error) {
wip = "%" wip = "%"
} }
if res, err := DBExec("UPDATE exercices SET title = ?, image = ?, disabled = ?, url_id = ?, path = ?, statement = ?, overview = ?, headline = ?, issue = ?, issue_kind = ?, depend = ?, gain = ?, coefficient_cur = ?, video_uri = ?, resolution = ?, seealso = ?, finished = ? WHERE id_exercice = ?", wip+e.Title, e.Image, e.Disabled, e.URLId, e.Path, e.Statement, e.Overview, e.Headline, e.Issue, e.IssueKind, e.Depend, e.Gain, e.Coefficient, e.VideoURI, e.Resolution, e.SeeAlso, e.Finished, e.Id); err != nil { if res, err := DBExec("UPDATE exercices SET title = ?, authors = ?, image = ?, disabled = ?, url_id = ?, path = ?, statement = ?, overview = ?, headline = ?, issue = ?, issue_kind = ?, depend = ?, gain = ?, coefficient_cur = ?, video_uri = ?, resolution = ?, seealso = ?, finished = ? WHERE id_exercice = ?", wip+e.Title, e.Authors, e.Image, e.Disabled, e.URLId, e.Path, e.Statement, e.Overview, e.Headline, e.Issue, e.IssueKind, e.Depend, e.Gain, e.Coefficient, e.VideoURI, e.Resolution, e.SeeAlso, e.Finished, e.Id); err != nil {
return 0, err return 0, err
} else if nb, err := res.RowsAffected(); err != nil { } else if nb, err := res.RowsAffected(); err != nil {
return 0, err return 0, err