oooooo i shoulda done different but i need a router for that hmmm
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
@@ -33,6 +32,7 @@ type Handler struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DB interface {
|
type DB interface {
|
||||||
|
GetQuestions() ([]string, error)
|
||||||
GetQuestion(string) (Question, error)
|
GetQuestion(string) (Question, error)
|
||||||
PutAnswer(string, string, Answer) error
|
PutAnswer(string, string, Answer) error
|
||||||
GetAnswers(string) ([]Answer, error)
|
GetAnswers(string) ([]Answer, error)
|
||||||
@@ -183,7 +183,11 @@ func (h Handler) handleAPIV1Question(session Session, w http.ResponseWriter, r *
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h Handler) handleAPIV1Questions(session Session, w http.ResponseWriter, r *http.Request) error {
|
func (h Handler) handleAPIV1Questions(session Session, w http.ResponseWriter, r *http.Request) error {
|
||||||
return errors.New("not impl")
|
qs, err := h.db.GetQuestions()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return json.NewEncoder(w).Encode(qs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h Handler) handleAPIV1Answers(session Session, w http.ResponseWriter, r *http.Request) error {
|
func (h Handler) handleAPIV1Answers(session Session, w http.ResponseWriter, r *http.Request) error {
|
||||||
@@ -207,7 +211,13 @@ func (h Handler) handleAPIV1AnswersGet(session Session, w http.ResponseWriter, r
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h Handler) handleAPIV1AnswersPost(session Session, w http.ResponseWriter, r *http.Request) error {
|
func (h Handler) handleAPIV1AnswersPost(session Session, w http.ResponseWriter, r *http.Request) error {
|
||||||
return errors.New("not impl")
|
qid := path.Base(r.URL.Path)
|
||||||
|
uid := session.User.ID
|
||||||
|
var a Answer
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&a); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return h.db.PutAnswer(qid, uid, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db fsDB) GetQuestion(qid string) (Question, error) {
|
func (db fsDB) GetQuestion(qid string) (Question, error) {
|
||||||
@@ -234,6 +244,19 @@ func (db fsDB) PutAnswer(qid, uid string, a Answer) error {
|
|||||||
return os.WriteFile(p, b, os.ModePerm)
|
return os.WriteFile(p, b, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db fsDB) GetQuestions() ([]string, error) {
|
||||||
|
p := db.path("")
|
||||||
|
entries, err := os.ReadDir(p)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
results := []string{}
|
||||||
|
for _, entry := range entries {
|
||||||
|
results = append(results, path.Base(entry.Name()))
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (db fsDB) GetAnswers(qid string) ([]Answer, error) {
|
func (db fsDB) GetAnswers(qid string) ([]Answer, error) {
|
||||||
p := db.path(qid) + ".d"
|
p := db.path(qid) + ".d"
|
||||||
entries, err := os.ReadDir(p)
|
entries, err := os.ReadDir(p)
|
||||||
|
|||||||
Reference in New Issue
Block a user