Add/remove/view themes and exercices
This commit is contained in:
parent
5bc3395b04
commit
c2d26b6053
2 changed files with 132 additions and 11 deletions
|
@ -1,21 +1,53 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ApiThemesRouting = map[string]DispatchFunction{
|
var ApiThemesRouting = map[string]DispatchFunction{
|
||||||
"GET": listTheme,
|
"GET": listTheme,
|
||||||
|
"POST": creationTheme,
|
||||||
|
"DELETE": deletionTheme,
|
||||||
|
}
|
||||||
|
|
||||||
|
type uploadedTheme struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type uploadedExercice struct {
|
||||||
|
Title string
|
||||||
|
Statement string
|
||||||
|
Hint string
|
||||||
|
Depend *int
|
||||||
|
Gain int
|
||||||
|
VideoURI string
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTheme(args []string) (Theme, error) {
|
||||||
|
if tid, err := strconv.Atoi(string(args[0])); err != nil {
|
||||||
|
return Theme{}, err
|
||||||
|
} else {
|
||||||
|
return GetTheme(tid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getExercice(args []string) (Exercice, error) {
|
||||||
|
if theme, err := getTheme(args); err != nil {
|
||||||
|
return Exercice{}, err
|
||||||
|
} else if eid, err := strconv.Atoi(string(args[1])); err != nil {
|
||||||
|
return Exercice{}, err
|
||||||
|
} else {
|
||||||
|
return theme.GetExercice(eid)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func listTheme(args []string, body []byte) (interface{}, error) {
|
func listTheme(args []string, body []byte) (interface{}, error) {
|
||||||
if len(args) == 1 {
|
if len(args) == 2 {
|
||||||
// List given theme
|
return getExercice(args)
|
||||||
if tid, err := strconv.Atoi(string(args[0])); err != nil {
|
} else if len(args) == 1 {
|
||||||
return nil, err
|
return getTheme(args)
|
||||||
} else {
|
|
||||||
return GetTheme(tid)
|
|
||||||
}
|
|
||||||
} else if len(args) == 0 {
|
} else if len(args) == 0 {
|
||||||
// List all themes
|
// List all themes
|
||||||
return GetThemes()
|
return GetThemes()
|
||||||
|
@ -23,3 +55,64 @@ func listTheme(args []string, body []byte) (interface{}, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func creationTheme(args []string, body []byte) (interface{}, error) {
|
||||||
|
if len(args) == 1 {
|
||||||
|
if theme, err := getTheme(args); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
// Create a new exercice
|
||||||
|
var ue uploadedExercice
|
||||||
|
if err := json.Unmarshal(body, &ue); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ue.Title) == 0 {
|
||||||
|
return nil, errors.New("Title not filled")
|
||||||
|
}
|
||||||
|
|
||||||
|
var depend *Exercice = nil
|
||||||
|
if ue.Depend != nil {
|
||||||
|
if d, err := GetExercice(*ue.Depend); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
depend = &d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return theme.AddExercice(ue.Title, ue.Statement, ue.Hint, depend, ue.Gain, ue.VideoURI)
|
||||||
|
}
|
||||||
|
} else if len(args) == 0 {
|
||||||
|
// Create a new theme
|
||||||
|
var ut uploadedTheme
|
||||||
|
if err := json.Unmarshal(body, &ut); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ut.Name) == 0 {
|
||||||
|
return nil, errors.New("Theme's name not filled")
|
||||||
|
}
|
||||||
|
|
||||||
|
return CreateTheme(ut.Name)
|
||||||
|
} else {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func deletionTheme(args []string, body []byte) (interface{}, error) {
|
||||||
|
if len(args) == 2 {
|
||||||
|
if exercice, err := getExercice(args); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return exercice.Delete()
|
||||||
|
}
|
||||||
|
} else if len(args) == 1 {
|
||||||
|
if theme, err := getTheme(args); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return theme.Delete()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -9,11 +9,29 @@ type Exercice struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Statement string `json:"statement"`
|
Statement string `json:"statement"`
|
||||||
Hint string `json:"hint"`
|
Hint string `json:"hint"`
|
||||||
Depend int64 `json:"depend"`
|
Depend *int64 `json:"depend"`
|
||||||
Gain int64 `json:"gain"`
|
Gain int64 `json:"gain"`
|
||||||
VideoURI string `json:"videoURI"`
|
VideoURI string `json:"videoURI"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetExercice(id int) (Exercice, error) {
|
||||||
|
var e Exercice
|
||||||
|
if err := DBQueryRow("SELECT id_exercice, title, statement, hint, depend, gain, video_uri FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.Title, &e.Statement, &e.Hint, &e.Depend, &e.Gain, &e.VideoURI); err != nil {
|
||||||
|
return Exercice{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Theme) GetExercice(id int) (Exercice, error) {
|
||||||
|
var e Exercice
|
||||||
|
if err := DBQueryRow("SELECT id_exercice, title, statement, hint, depend, gain, video_uri FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.Title, &e.Statement, &e.Hint, &e.Depend, &e.Gain, &e.VideoURI); err != nil {
|
||||||
|
return Exercice{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (t Theme) GetExercices() ([]Exercice, error) {
|
func (t Theme) GetExercices() ([]Exercice, error) {
|
||||||
if rows, err := DBQuery("SELECT id_exercice, title, statement, hint, depend, gain, video_uri FROM teams WHERE id_theme = ?", t.Id); err != nil {
|
if rows, err := DBQuery("SELECT id_exercice, title, statement, hint, depend, gain, video_uri FROM teams WHERE id_theme = ?", t.Id); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -37,12 +55,22 @@ func (t Theme) GetExercices() ([]Exercice, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Theme) AddExercice(title string, statement string, hint string, depend *Exercice, gain int, videoURI string) (Exercice, error) {
|
func (t Theme) AddExercice(title string, statement string, hint string, depend *Exercice, gain int, videoURI string) (Exercice, error) {
|
||||||
if res, err := DBExec("INSERT INTO exercices (id_theme, title, statement, hint, depend, gain, video_uri) VALUES (?, ?, ?, ?, ?, ?, ?)", t.Id, title, statement, hint, depend.Id, gain, videoURI); err != nil {
|
var dpd interface{}
|
||||||
|
if depend == nil {
|
||||||
|
dpd = nil
|
||||||
|
} else {
|
||||||
|
dpd = depend.Id
|
||||||
|
}
|
||||||
|
if res, err := DBExec("INSERT INTO exercices (id_theme, title, statement, hint, depend, gain, video_uri) VALUES (?, ?, ?, ?, ?, ?, ?)", t.Id, title, statement, hint, dpd, gain, videoURI); err != nil {
|
||||||
return Exercice{}, err
|
return Exercice{}, err
|
||||||
} else if eid, err := res.LastInsertId(); err != nil {
|
} else if eid, err := res.LastInsertId(); err != nil {
|
||||||
return Exercice{}, err
|
return Exercice{}, err
|
||||||
} else {
|
} else {
|
||||||
return Exercice{eid, title, statement, hint, depend.Id, int64(gain), videoURI}, nil
|
if depend == nil {
|
||||||
|
return Exercice{eid, title, statement, hint, nil, int64(gain), videoURI}, nil
|
||||||
|
} else {
|
||||||
|
return Exercice{eid, title, statement, hint, &depend.Id, int64(gain), videoURI}, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue