backend: can open hint

This commit is contained in:
nemunaire 2016-12-09 11:49:29 +01:00
parent d2d4f8e628
commit 4b20c1dc1e
4 changed files with 52 additions and 0 deletions

36
backend/hint.go Normal file
View File

@ -0,0 +1,36 @@
package main
import (
"encoding/json"
"log"
"io/ioutil"
"os"
"srs.epita.fr/fic-server/libfic"
)
type askOpenHint struct {
HintId int64 `json:"id"`
}
func treatOpeningHint(pathname string, team fic.Team) {
var ask askOpenHint
if cnt_raw, err := ioutil.ReadFile(pathname); err != nil {
log.Println("[ERR]", err)
} else if err := json.Unmarshal(cnt_raw, &ask); err != nil {
log.Println("[ERR]", err)
} else if ask.HintId == 0 {
log.Println("[WRN] Invalid content in hint file: ", pathname)
os.Remove(pathname)
} else if hint, err := fic.GetHint(ask.HintId); err != nil {
log.Println("[ERR]", err)
} else if err := team.OpenHint(hint); err != nil {
log.Println("[ERR]", err)
} else {
genTeamMyFile(team)
if err := os.Remove(pathname); err != nil {
log.Println("[ERR]", err)
}
}
}

View File

@ -116,6 +116,8 @@ func treat(raw_path string) {
log.Println("[ERR]", err)
} else if spath[2] == "name" {
treatRename(raw_path, team)
} else if spath[2] == "hint" {
treatOpeningHint(raw_path, team)
} else {
treatSubmission(raw_path, team, spath[2])
}

View File

@ -11,6 +11,15 @@ type EHint struct {
Cost int64 `json:"cost"`
}
func GetHint(id int64) (EHint, error) {
var h EHint
if err := DBQueryRow("SELECT id_hint, id_exercice, title, content, cost FROM exercice_hints WHERE id_hint = ?", id).Scan(&h.Id, &h.IdExercice, &h.Title, &h.Content, &h.Cost); err != nil {
return h, err
}
return h, nil
}
func (e Exercice) GetHints() ([]EHint, error) {
if rows, err := DBQuery("SELECT id_hint, title, content, cost FROM exercice_hints WHERE id_exercice = ?", e.Id); err != nil {
return nil, err

View File

@ -216,6 +216,11 @@ func (t Team) HasHint(h EHint) (bool) {
return tm != nil
}
func (t Team) OpenHint(h EHint) (error) {
_, err := DBExec("INSERT INTO team_hints (id_team, id_hint, time) VALUES (?, ?, ?)", t.Id, h.Id, time.Now())
return err
}
func (t Team) HasSolved(e Exercice) (bool, time.Time, int64) {
var nb *int64
var tm *time.Time