tests pass woo

This commit is contained in:
Bel LaPointe
2024-02-20 08:55:31 -07:00
parent 706522eeef
commit ce1c1e0205
2 changed files with 54 additions and 22 deletions

View File

@@ -35,7 +35,7 @@ type Handler struct {
type DB interface {
GetQuestions() ([]string, error)
GetQuestion(string) (Question, error)
PutAnswer(string, string, Answer) error
InsertAnswer(string, string, Answer) error
GetAnswers(string) ([]Answer, error)
}
@@ -220,7 +220,7 @@ func (h Handler) handleAPIV1QuestionsAnswersPost(session Session, w http.Respons
if err := json.NewDecoder(r.Body).Decode(&a); err != nil {
return err
}
return h.db.PutAnswer(qid, uid, a)
return h.db.InsertAnswer(qid, uid, a)
}
func (db fsDB) GetQuestion(qid string) (Question, error) {
@@ -237,13 +237,16 @@ func (db fsDB) GetQuestion(qid string) (Question, error) {
return q, nil
}
func (db fsDB) PutAnswer(qid, uid string, a Answer) error {
func (db fsDB) InsertAnswer(qid, uid string, a Answer) error {
p := path.Join(db.path(qid)+".d", uid)
os.MkdirAll(path.Dir(p), os.ModePerm)
b, err := json.Marshal(a)
if err != nil {
return err
}
if _, err := os.Stat(p); !os.IsNotExist(err) {
return nil
}
return os.WriteFile(p, b, os.ModePerm)
}
@@ -268,7 +271,7 @@ func (db fsDB) GetAnswers(qid string) ([]Answer, error) {
}
results := []Answer{}
for _, entry := range entries {
b, err := os.ReadFile(entry.Name())
b, err := os.ReadFile(path.Join(p, entry.Name()))
if err != nil {
return nil, err
}