Add NoIndexOnCreate config option

This commit is contained in:
andrea 2023-11-18 12:01:57 +01:00
parent 3cdfa72403
commit 3196982a64
4 changed files with 27 additions and 78 deletions

View file

@ -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

View file

@ -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
// },
// )

View file

@ -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)

View file

@ -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