From 56038e014a7a7801b876f0c29307311faf40876f Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Fri, 7 Jul 2023 18:01:17 +0200 Subject: [PATCH] Implement UpdateQuiz on filesystem store --- store/file/file.go | 46 +++++++++++++++++-- store/file/file_test.go | 42 +++++++++++++++++ ...cb791ebc5977ce1801e12059f58ce4d66c0c7f6.md | 7 +++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 store/file/test/quizzes/94ed4e9cdf8e0a75a2c5ce925cb791ebc5977ce1801e12059f58ce4d66c0c7f6.md diff --git a/store/file/file.go b/store/file/file.go index 3f7df36..5ddb722 100644 --- a/store/file/file.go +++ b/store/file/file.go @@ -19,6 +19,7 @@ type FileProboCollectorStore struct { Dir string memoryStore *memory.MemoryProboCollectorStore + paths map[string]string } func NewFileProboCollectorStore(dirname string) (*FileProboCollectorStore, error) { @@ -29,6 +30,8 @@ func NewFileProboCollectorStore(dirname string) (*FileProboCollectorStore, error return nil, err } + s.paths = make(map[string]string) + markdownFiles := make([]fs.FileInfo, 0) for _, file := range files { @@ -71,13 +74,23 @@ func NewFileProboCollectorStore(dirname string) (*FileProboCollectorStore, error func (s *FileProboCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) { return s.memoryStore.ReadAllQuizzes() } + func (s *FileProboCollectorStore) CreateQuiz(r *client.CreateUpdateQuizRequest) (*models.Quiz, error) { quiz, err := s.memoryStore.CreateQuiz(r) if err != nil { 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 { return nil, err } @@ -146,17 +159,42 @@ func QuizFromMarkdown(markdown string) (*client.Quiz, error) { 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) if err != nil { 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 = ioutil.WriteFile(filename, []byte(markdown), 0644) + _, err = file.Write([]byte(markdown)) if err != nil { return err } 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 +// } diff --git a/store/file/file_test.go b/store/file/file_test.go index 9fd202d..0f890c0 100644 --- a/store/file/file_test.go +++ b/store/file/file_test.go @@ -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 { return reflect.DeepEqual(got, want) } diff --git a/store/file/test/quizzes/94ed4e9cdf8e0a75a2c5ce925cb791ebc5977ce1801e12059f58ce4d66c0c7f6.md b/store/file/test/quizzes/94ed4e9cdf8e0a75a2c5ce925cb791ebc5977ce1801e12059f58ce4d66c0c7f6.md new file mode 100644 index 0000000..356d066 --- /dev/null +++ b/store/file/test/quizzes/94ed4e9cdf8e0a75a2c5ce925cb791ebc5977ce1801e12059f58ce4d66c0c7f6.md @@ -0,0 +1,7 @@ +Newly created question text. + +* Answer 1 +* Answer 1 +* Answer 2 +* Answer 3 +* Answer 4