backend: can open hint
authornemunaire <nemunaire@nemunai.re>
Fri, 9 Dec 2016 10:49:29 +0000 (11:49 +0100)
committerPierre-Olivier Mercier <nemunaire@nemunai.re>
Wed, 17 Jan 2018 17:51:51 +0000 (18:51 +0100)
backend/hint.go [new file with mode: 0644]
backend/main.go
libfic/hint.go
libfic/team.go

diff --git a/backend/hint.go b/backend/hint.go
new file mode 100644 (file)
index 0000000..17124f5
--- /dev/null
@@ -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)
+               }
+       }
+}
index 44e44ee25086cf072a8ac34165d88147c3c46025..229c9507c50f953ef6937dc2619dc4036490b426 100644 (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])
                }
index 46ae6a6f488a47945e251fd9352539e9755bbb3d..123d4a0257637fa379ec9530b82942e88a7b458d 100644 (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
index 27fbb40ac4a0ed5c74e395b6da9929b32119f1db..03d7a3db8f5061c6d436c06adb3c18fad1b7a50e 100644 (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