From d6cf444def6d61c9d101b83078a0c2f70452d2f6 Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Mon, 20 Jan 2025 21:43:20 +0100 Subject: [PATCH] adding comment to document repository --- .../model/database/documentrepository.go | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/internal/model/database/documentrepository.go b/src/internal/model/database/documentrepository.go index 944148d..ae9fb01 100644 --- a/src/internal/model/database/documentrepository.go +++ b/src/internal/model/database/documentrepository.go @@ -74,6 +74,9 @@ func (d *DocumentRepository) CountAll() (uint, error) { 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 { stmt := `UPDATE documents SET content = $1, summary = $2 @@ -83,27 +86,33 @@ func (m *DocumentRepository) Update(d *model.Document) error { 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) { - processed := 0 + processed := 0 - count, err := d.CountAll() - if err != nil { - return processed, err - } + count, err := d.CountAll() + if err != nil { + return processed, err + } - for i := 0; i < int(count); i += 10 { - docs, err := d.All(10, i) - if err != nil { - return processed, err - } - - for _, doc := range docs { - new_doc := transform(doc) - err = d.Update(new_doc) - if err != nil { return processed, err } - processed++ - } - } + for i := 0; i < int(count); i += 10 { + docs, err := d.All(10, i) + if err != nil { + return processed, err + } - 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 }