diff --git a/admin/api_theme.go b/admin/api_theme.go index 52d8d429..48bc0180 100644 --- a/admin/api_theme.go +++ b/admin/api_theme.go @@ -1,21 +1,53 @@ package main import ( + "encoding/json" + "errors" "strconv" ) 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) { - if len(args) == 1 { - // List given theme - if tid, err := strconv.Atoi(string(args[0])); err != nil { - return nil, err - } else { - return GetTheme(tid) - } + if len(args) == 2 { + return getExercice(args) + } else if len(args) == 1 { + return getTheme(args) } else if len(args) == 0 { // List all themes return GetThemes() @@ -23,3 +55,64 @@ func listTheme(args []string, body []byte) (interface{}, error) { 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 + } +} diff --git a/admin/exercice.go b/admin/exercice.go index b7bb1f56..2d9fc1af 100644 --- a/admin/exercice.go +++ b/admin/exercice.go @@ -9,11 +9,29 @@ type Exercice struct { Title string `json:"title"` Statement string `json:"statement"` Hint string `json:"hint"` - Depend int64 `json:"depend"` + Depend *int64 `json:"depend"` Gain int64 `json:"gain"` 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) { 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 @@ -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) { - 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 } else if eid, err := res.LastInsertId(); err != nil { return Exercice{}, err } 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 + } } }