From 3196982a643c08e5a0a0e1bbd8cbac1d33d0f89c Mon Sep 17 00:00:00 2001 From: andrea Date: Sat, 18 Nov 2023 12:01:57 +0100 Subject: [PATCH] Add NoIndexOnCreate config option --- store/file/file.go | 17 +++++---- store/file/quiz.go | 54 --------------------------- store/file/quiz_test.go | 29 ++++++++------ store/file/testdata/quizzes/quiz_5.md | 5 --- 4 files changed, 27 insertions(+), 78 deletions(-) diff --git a/store/file/file.go b/store/file/file.go index 352592d..fc6b8c6 100644 --- a/store/file/file.go +++ b/store/file/file.go @@ -36,10 +36,11 @@ type FilePathConfig struct { type FileStoreConfig[T store.Storable, K Storer[T]] struct { FilePathConfig - FilepathFunc func(T, *FilePathConfig) string - UnmarshalFunc func(K, string, []byte) (T, error) - MarshalFunc func(K, string, T) error - IndexDirFunc func(*FileStore[T, K]) error + FilepathFunc func(T, *FilePathConfig) string + UnmarshalFunc func(K, string, []byte) (T, error) + MarshalFunc func(K, string, T) error + IndexDirFunc func(*FileStore[T, K]) error + NoIndexOnCreate bool } type FileStore[T store.Storable, K Storer[T]] struct { @@ -130,10 +131,12 @@ func NewFileStore[T store.Storable, K Storer[T]](config *FileStoreConfig[T, K], paths: make(map[string]string, 0), } - err := store.IndexDir() - if err != nil { - return nil, err + if !config.NoIndexOnCreate { + err := store.IndexDir() + if err != nil { + return nil, err + } } return store, nil diff --git a/store/file/quiz.go b/store/file/quiz.go index 55ec062..e79a3c5 100644 --- a/store/file/quiz.go +++ b/store/file/quiz.go @@ -212,57 +212,3 @@ func removeQuizHeader(path string) (*models.Meta, error) { return &meta, nil } - -// filepath.Join(BaseDir, QuizzesDir), -// "quiz", -// ".md", -// DefaultIndexDirFunc, -// nil, -// func(s *store.QuizStore, filepath string, content []byte) (*models.Quiz, error) { -// quiz, meta, err := models.MarkdownToQuiz(string(content)) -// if err != nil { -// return nil, err -// } - -// var errQuizAlreadyPresent *store.ErrQuizAlreadyPresent - -// q, err := s.Create(quiz) -// if err != nil && !errors.As(err, &errQuizAlreadyPresent) { -// return nil, err -// } - -// if meta == nil { -// writeQuizHeader(filepath, &models.Meta{ -// ID: q.ID, -// CreatedAt: time.Now(), -// }) -// } - -// return q, nil -// }, -// func(s *store.QuizStore, filePath string, quiz *models.Quiz) error { -// markdown, err := models.QuizToMarkdown(quiz) -// if err != nil { -// return err -// } - -// file, err := os.Create(filePath) -// if err != nil { -// return err -// } - -// defer file.Close() - -// markdownWithMetaHeader, err := addMetaHeaderToMarkdown(markdown, &quiz.Meta) -// if err != nil { -// return err -// } - -// _, err = file.Write([]byte(markdownWithMetaHeader)) -// if err != nil { -// return err -// } - -// return nil -// }, -// ) diff --git a/store/file/quiz_test.go b/store/file/quiz_test.go index 97b4147..c9a1a7e 100644 --- a/store/file/quiz_test.go +++ b/store/file/quiz_test.go @@ -37,6 +37,8 @@ func (t *quizTestSuite) TestReadAll() { len(result), ), ) + + _, err = removeQuizHeader(filepath.Join(store.Dir, "quiz_5.md")) } } @@ -44,10 +46,11 @@ func (t *quizTestSuite) TestCreate() { filePathConfig := FilePathConfig{"testdata/quizzes", "quiz", ".md"} store, err := NewQuizFileStore( &FileStoreConfig[*models.Quiz, *store.QuizStore]{ - FilePathConfig: filePathConfig, - IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore], - UnmarshalFunc: DefaultUnmarshalQuizFunc, - MarshalFunc: DefaultMarshalQuizFunc, + FilePathConfig: filePathConfig, + IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore], + UnmarshalFunc: DefaultUnmarshalQuizFunc, + MarshalFunc: DefaultMarshalQuizFunc, + NoIndexOnCreate: true, }, ) t.Nil(err) @@ -106,10 +109,11 @@ func (t *quizTestSuite) TestDelete() { filePathConfig := FilePathConfig{"testdata/quizzes", "quiz", ".md"} store, err := NewQuizFileStore( &FileStoreConfig[*models.Quiz, *store.QuizStore]{ - FilePathConfig: filePathConfig, - IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore], - UnmarshalFunc: DefaultUnmarshalQuizFunc, - MarshalFunc: DefaultMarshalQuizFunc, + FilePathConfig: filePathConfig, + IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore], + UnmarshalFunc: DefaultUnmarshalQuizFunc, + MarshalFunc: DefaultMarshalQuizFunc, + NoIndexOnCreate: true, }, ) t.Nil(err) @@ -145,10 +149,11 @@ func (t *quizTestSuite) TestUpdate() { filePathConfig := FilePathConfig{"testdata/quizzes", "quiz", ".md"} store, err := NewQuizFileStore( &FileStoreConfig[*models.Quiz, *store.QuizStore]{ - FilePathConfig: filePathConfig, - IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore], - UnmarshalFunc: DefaultUnmarshalQuizFunc, - MarshalFunc: DefaultMarshalQuizFunc, + FilePathConfig: filePathConfig, + IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore], + UnmarshalFunc: DefaultUnmarshalQuizFunc, + MarshalFunc: DefaultMarshalQuizFunc, + NoIndexOnCreate: true, }, ) t.Nil(err) diff --git a/store/file/testdata/quizzes/quiz_5.md b/store/file/testdata/quizzes/quiz_5.md index 42eeba8..6331e1a 100644 --- a/store/file/testdata/quizzes/quiz_5.md +++ b/store/file/testdata/quizzes/quiz_5.md @@ -1,8 +1,3 @@ ---- -id: edadaa90-802f-4a74-83cc-4b4cb5192f28 -created_at: 2023-11-17T16:50:38.479350601+01:00 -updated_at: 0001-01-01T00:00:00Z ---- This quiz is initially without metadata. * Answer 1