adding comment to document repository

This commit is contained in:
2025-01-20 21:43:20 +01:00
parent 2f0a17763d
commit d6cf444def

View File

@@ -74,6 +74,9 @@ func (d *DocumentRepository) CountAll() (uint, error) {
return count, nil return count, nil
} }
// Update an document in the database. Will use the id that is set in the
// document object as an reference to the database row. This may throw an error
// if the connection to database fails.
func (m *DocumentRepository) Update(d *model.Document) error { func (m *DocumentRepository) Update(d *model.Document) error {
stmt := `UPDATE documents stmt := `UPDATE documents
SET content = $1, summary = $2 SET content = $1, summary = $2
@@ -83,27 +86,33 @@ func (m *DocumentRepository) Update(d *model.Document) error {
return err return err
} }
// Will transform every document in the database given a transformation
// function. Will load the document, parse it to the transform function and call
// update on the returned document. May throw an error if the connection to the
// database fails.
func (d *DocumentRepository) Map(transform func(*model.Document) *model.Document) (int, error) { func (d *DocumentRepository) Map(transform func(*model.Document) *model.Document) (int, error) {
processed := 0 processed := 0
count, err := d.CountAll() count, err := d.CountAll()
if err != nil { if err != nil {
return processed, err return processed, err
} }
for i := 0; i < int(count); i += 10 { for i := 0; i < int(count); i += 10 {
docs, err := d.All(10, i) docs, err := d.All(10, i)
if err != nil { if err != nil {
return processed, err return processed, err
} }
for _, doc := range docs {
new_doc := transform(doc)
err = d.Update(new_doc)
if err != nil { return processed, err }
processed++
}
}
return processed, nil for _, doc := range docs {
new_doc := transform(doc)
err = d.Update(new_doc)
if err != nil {
return processed, err
}
processed++
}
}
return processed, nil
} }