Implement UpdateQuiz on filesystem store

This commit is contained in:
Andrea Fazzi 2023-07-07 18:01:17 +02:00
parent 483e0a7fe7
commit 56038e014a
3 changed files with 91 additions and 4 deletions

View file

@ -19,6 +19,7 @@ type FileProboCollectorStore struct {
Dir string Dir string
memoryStore *memory.MemoryProboCollectorStore memoryStore *memory.MemoryProboCollectorStore
paths map[string]string
} }
func NewFileProboCollectorStore(dirname string) (*FileProboCollectorStore, error) { func NewFileProboCollectorStore(dirname string) (*FileProboCollectorStore, error) {
@ -29,6 +30,8 @@ func NewFileProboCollectorStore(dirname string) (*FileProboCollectorStore, error
return nil, err return nil, err
} }
s.paths = make(map[string]string)
markdownFiles := make([]fs.FileInfo, 0) markdownFiles := make([]fs.FileInfo, 0)
for _, file := range files { for _, file := range files {
@ -71,13 +74,23 @@ func NewFileProboCollectorStore(dirname string) (*FileProboCollectorStore, error
func (s *FileProboCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) { func (s *FileProboCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) {
return s.memoryStore.ReadAllQuizzes() return s.memoryStore.ReadAllQuizzes()
} }
func (s *FileProboCollectorStore) CreateQuiz(r *client.CreateUpdateQuizRequest) (*models.Quiz, error) { func (s *FileProboCollectorStore) CreateQuiz(r *client.CreateUpdateQuizRequest) (*models.Quiz, error) {
quiz, err := s.memoryStore.CreateQuiz(r) quiz, err := s.memoryStore.CreateQuiz(r)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = s.writeMarkdownFile(quiz) err = s.createOrUpdateMarkdownFile(quiz)
if err != nil {
return nil, err
}
return quiz, nil
}
func (s *FileProboCollectorStore) UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) {
quiz, err := s.memoryStore.UpdateQuiz(r, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -146,17 +159,42 @@ func QuizFromMarkdown(markdown string) (*client.Quiz, error) {
return quiz, nil return quiz, nil
} }
func (s *FileProboCollectorStore) writeMarkdownFile(quiz *models.Quiz) error { func (s *FileProboCollectorStore) createOrUpdateMarkdownFile(quiz *models.Quiz) error {
var filename string
markdown, err := MarkdownFromQuiz(quiz) markdown, err := MarkdownFromQuiz(quiz)
if err != nil { if err != nil {
return err return err
} }
fn := s.paths[quiz.ID]
if fn == "" {
filename = filepath.Join(s.Dir, quiz.Hash+".md")
}
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
filename := filepath.Join(s.Dir, quiz.Hash+".md") _, err = file.Write([]byte(markdown))
err = ioutil.WriteFile(filename, []byte(markdown), 0644)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
// func (s *FileProboCollectorStore) writeMarkdownFile(quiz *models.Quiz) error {
// markdown, err := MarkdownFromQuiz(quiz)
// if err != nil {
// return err
// }
// filename := filepath.Join(s.Dir, quiz.Hash+".md")
// err = ioutil.WriteFile(filename, []byte(markdown), 0644)
// if err != nil {
// return err
// }
// return nil
// }

View file

@ -109,6 +109,48 @@ func (t *testSuite) TestCreateQuiz() {
} }
} }
func (t *testSuite) TestUpdateQuiz() {
dirname := "./test/quizzes"
store, err := NewFileProboCollectorStore(dirname)
t.True(err == nil, fmt.Sprintf("A file store should be initialized without problems but an error occurred: %v", err))
if !t.Failed() {
quiz, err := createQuizOnDisk(store, &client.CreateUpdateQuizRequest{
Quiz: &client.Quiz{
Question: &client.Question{Text: "Newly created question text."},
Answers: []*client.Answer{
{Text: "Answer 1", Correct: true},
{Text: "Answer 2", Correct: false},
{Text: "Answer 3", Correct: false},
{Text: "Answer 4", Correct: false},
},
},
})
t.Nil(err, "The quiz to update should be created without issue")
if !t.Failed() {
_, err = store.UpdateQuiz(
&client.CreateUpdateQuizRequest{
Quiz: &client.Quiz{
Question: &client.Question{Text: "Newly created question text."},
Answers: []*client.Answer{
{Text: "Answer 1", Correct: true},
{Text: "Answer 2", Correct: false},
{Text: "Answer 3", Correct: false},
{Text: "Answer 4", Correct: false},
},
},
}, quiz.ID)
}
}
}
func createQuizOnDisk(store *FileProboCollectorStore, req *client.CreateUpdateQuizRequest) (*models.Quiz, error) {
return store.CreateQuiz(req)
}
func testsAreEqual(got, want []*models.Quiz) bool { func testsAreEqual(got, want []*models.Quiz) bool {
return reflect.DeepEqual(got, want) return reflect.DeepEqual(got, want)
} }

View file

@ -0,0 +1,7 @@
Newly created question text.
* Answer 1
* Answer 1
* Answer 2
* Answer 3
* Answer 4