sync: Parse resolution.md

This commit is contained in:
nemunaire 2021-12-10 18:55:47 +01:00
parent 0cc72712a4
commit f8001653cd
4 changed files with 62 additions and 28 deletions

View File

@ -338,7 +338,7 @@ func createExercice(theme *fic.Theme, body []byte) (interface{}, error) {
}
}
return theme.AddExercice(ue.Title, ue.URLId, ue.Path, ue.Statement, ue.Overview, ue.Headline, depend, ue.Gain, ue.VideoURI, ue.Finished)
return theme.AddExercice(ue.Title, ue.URLId, ue.Path, ue.Statement, ue.Overview, ue.Headline, depend, ue.Gain, ue.VideoURI, ue.Resolution, ue.SeeAlso, ue.Finished)
}
type uploadedHint struct {

View File

@ -190,14 +190,11 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
}
}
// Handle video
// Handle resolutions
resolutionFound := false
e.VideoURI = path.Join(epath, "resolution.mp4")
if !i.exists(e.VideoURI) {
if LogMissingResolution {
log.Printf("%q: resolution.mp4: no video file found at %s", edir, e.VideoURI)
} else {
errs = append(errs, fmt.Sprintf("%q: resolution.mp4: no video file found at %s", edir, e.VideoURI))
}
e.VideoURI = ""
} else if size, err := getFileSize(i, e.VideoURI); err != nil {
errs = append(errs, fmt.Sprintf("%q: resolution.mp4: ", edir, err))
@ -205,6 +202,35 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
} else if size == 0 {
errs = append(errs, fmt.Sprintf("%q: resolution.mp4: The file is empty!", edir))
e.VideoURI = ""
} else {
resolutionFound = true
}
writeup := path.Join(epath, "resolution.md")
if !i.exists(writeup) {
writeup = path.Join(epath, "resolution.txt")
}
if i.exists(writeup) {
if size, err := getFileSize(i, writeup); err != nil {
errs = append(errs, fmt.Sprintf("%q: resolution.md: %s", edir, err.Error()))
} else if size == 0 {
errs = append(errs, fmt.Sprintf("%q: resolution.md: The file is empty!", edir))
} else if e.Resolution, err = getFileContent(i, writeup); err != nil {
errs = append(errs, fmt.Sprintf("%q: resolution.md: %s", edir, err.Error()))
} else if e.Resolution, err = ProcessMarkdown(i, e.Overview, epath); err != nil {
errs = append(errs, fmt.Sprintf("%q: resolution.md: error during markdown processing: %s", edir, err.Error()))
} else {
resolutionFound = true
}
}
if !resolutionFound {
if LogMissingResolution {
log.Printf("%q: no resolution video or text file found in %s", edir, epath, epath)
} else {
errs = append(errs, fmt.Sprintf("%q: no resolution video or text file found in %s", edir, epath))
}
}
return

View File

@ -143,8 +143,10 @@ CREATE TABLE IF NOT EXISTS exercices(
depend INTEGER,
gain INTEGER NOT NULL,
coefficient_cur FLOAT NOT NULL DEFAULT 1.0,
video_uri VARCHAR(255) NOT NULL,
finished TEXT NOT NULL,
video_uri VARCHAR(255) NOT NULL,
resolution TEXT NOT NULL,
seealso TEXT NOT NULL,
FOREIGN KEY(id_theme) REFERENCES themes(id_theme),
FOREIGN KEY(depend) REFERENCES exercices(id_exercice)
) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

View File

@ -49,12 +49,16 @@ type Exercice struct {
Coefficient float64 `json:"coefficient"`
// VideoURI is the link to the resolution video
VideoURI string `json:"videoURI"`
// Resolution is a write-up (in HTML)
Resolution string `json:"resolution"`
// SeeAlso is a collection of links (HTML formated)
SeeAlso string `json:"seealso"`
}
// GetExercice retrieves the challenge with the given id.
func GetExercice(id int64) (*Exercice, error) {
var e Exercice
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil {
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.IdTheme, &e.Title, &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
}
@ -64,7 +68,7 @@ func GetExercice(id int64) (*Exercice, error) {
// GetExercice retrieves the challenge with the given id.
func (t *Theme) GetExercice(id int) (*Exercice, error) {
e := &Exercice{}
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil {
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.IdTheme, &e.Title, &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
}
@ -74,7 +78,7 @@ func (t *Theme) GetExercice(id int) (*Exercice, error) {
// GetExerciceByTitle retrieves the challenge with the given title.
func (t *Theme) GetExerciceByTitle(title string) (*Exercice, error) {
e := &Exercice{}
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ? AND title = ?", t.Id, title).Scan(&e.Id, &e.IdTheme, &e.Title, &t.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil {
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? AND title = ?", t.Id, title).Scan(&e.Id, &e.IdTheme, &e.Title, &t.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
}
@ -83,7 +87,7 @@ func (t *Theme) GetExerciceByTitle(title string) (*Exercice, error) {
// GetExercices returns the list of all challenges present in the database.
func GetExercices() ([]*Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices"); err != nil {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices"); err != nil {
return nil, err
} else {
defer rows.Close()
@ -91,7 +95,7 @@ func GetExercices() ([]*Exercice, error) {
exos := []*Exercice{}
for rows.Next() {
e := &Exercice{}
if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil {
if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &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
}
exos = append(exos, e)
@ -106,7 +110,7 @@ func GetExercices() ([]*Exercice, error) {
// GetExercices returns the list of all challenges in the Theme.
func (t *Theme) GetExercices() ([]*Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ?", t.Id); err != nil {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ?", t.Id); err != nil {
return nil, err
} else {
defer rows.Close()
@ -114,7 +118,7 @@ func (t *Theme) GetExercices() ([]*Exercice, error) {
exos := []*Exercice{}
for rows.Next() {
e := &Exercice{}
if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil {
if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &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
}
exos = append(exos, e)
@ -163,7 +167,7 @@ func (t *Theme) addExercice(e *Exercice) (err error) {
cc = fmt.Sprintf("%f", e.Coefficient)
}
if res, err := DBExec("INSERT INTO exercices (id_theme, title, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "+ik+", "+cc+")", t.Id, e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI); err != nil {
if res, err := DBExec("INSERT INTO exercices (id_theme, title, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, resolution, seealso, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "+ik+", "+cc+")", t.Id, e.Title, 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
} else if eid, err := res.LastInsertId(); err != nil {
return err
@ -175,23 +179,25 @@ func (t *Theme) addExercice(e *Exercice) (err error) {
}
// AddExercice creates and fills a new struct Exercice and registers it into the database.
func (t *Theme) AddExercice(title string, urlId string, path string, statement string, overview string, headline string, depend *Exercice, gain int64, videoURI string, finished string) (e *Exercice, err error) {
func (t *Theme) AddExercice(title string, 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
if depend != nil {
dpd = &depend.Id
}
e = &Exercice{
Title: title,
URLId: urlId,
Path: path,
Statement: statement,
Overview: overview,
Headline: headline,
Depend: dpd,
Finished: finished,
Gain: gain,
VideoURI: videoURI,
Title: title,
URLId: urlId,
Path: path,
Statement: statement,
Overview: overview,
Headline: headline,
Depend: dpd,
Finished: finished,
Gain: gain,
VideoURI: videoURI,
Resolution: resolution,
SeeAlso: seealso,
}
err = t.addExercice(e)
@ -201,7 +207,7 @@ func (t *Theme) AddExercice(title string, urlId string, path string, statement s
// Update applies modifications back to the database.
func (e *Exercice) Update() (int64, error) {
if res, err := DBExec("UPDATE exercices SET title = ?, url_id = ?, path = ?, statement = ?, overview = ?, headline = ?, issue = ?, issue_kind = ?, depend = ?, gain = ?, coefficient_cur = ?, video_uri = ?, finished = ? WHERE id_exercice = ?", e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Headline, e.Issue, e.IssueKind, e.Depend, e.Gain, e.Coefficient, e.VideoURI, e.Finished, e.Id); err != nil {
if res, err := DBExec("UPDATE exercices SET title = ?, url_id = ?, path = ?, statement = ?, overview = ?, headline = ?, issue = ?, issue_kind = ?, depend = ?, gain = ?, coefficient_cur = ?, video_uri = ?, resolution = ?, seealso = ?, finished = ? WHERE id_exercice = ?", e.Title, 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
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err