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

@ -40,6 +40,7 @@ type FileStoreConfig[T store.Storable, K Storer[T]] struct {
UnmarshalFunc func(K, string, []byte) (T, error) UnmarshalFunc func(K, string, []byte) (T, error)
MarshalFunc func(K, string, T) error MarshalFunc func(K, string, T) error
IndexDirFunc func(*FileStore[T, K]) error IndexDirFunc func(*FileStore[T, K]) error
NoIndexOnCreate bool
} }
type FileStore[T store.Storable, K Storer[T]] struct { type FileStore[T store.Storable, K Storer[T]] struct {
@ -130,11 +131,13 @@ func NewFileStore[T store.Storable, K Storer[T]](config *FileStoreConfig[T, K],
paths: make(map[string]string, 0), paths: make(map[string]string, 0),
} }
if !config.NoIndexOnCreate {
err := store.IndexDir() err := store.IndexDir()
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
return store, nil return store, nil
} }

View file

@ -212,57 +212,3 @@ func removeQuizHeader(path string) (*models.Meta, error) {
return &meta, nil 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), len(result),
), ),
) )
_, err = removeQuizHeader(filepath.Join(store.Dir, "quiz_5.md"))
} }
} }
@ -48,6 +50,7 @@ func (t *quizTestSuite) TestCreate() {
IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore], IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore],
UnmarshalFunc: DefaultUnmarshalQuizFunc, UnmarshalFunc: DefaultUnmarshalQuizFunc,
MarshalFunc: DefaultMarshalQuizFunc, MarshalFunc: DefaultMarshalQuizFunc,
NoIndexOnCreate: true,
}, },
) )
t.Nil(err) t.Nil(err)
@ -110,6 +113,7 @@ func (t *quizTestSuite) TestDelete() {
IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore], IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore],
UnmarshalFunc: DefaultUnmarshalQuizFunc, UnmarshalFunc: DefaultUnmarshalQuizFunc,
MarshalFunc: DefaultMarshalQuizFunc, MarshalFunc: DefaultMarshalQuizFunc,
NoIndexOnCreate: true,
}, },
) )
t.Nil(err) t.Nil(err)
@ -149,6 +153,7 @@ func (t *quizTestSuite) TestUpdate() {
IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore], IndexDirFunc: DefaultIndexDirFunc[*models.Quiz, *store.QuizStore],
UnmarshalFunc: DefaultUnmarshalQuizFunc, UnmarshalFunc: DefaultUnmarshalQuizFunc,
MarshalFunc: DefaultMarshalQuizFunc, MarshalFunc: DefaultMarshalQuizFunc,
NoIndexOnCreate: true,
}, },
) )
t.Nil(err) 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. This quiz is initially without metadata.
* Answer 1 * Answer 1