diff --git a/src/internal/model/database/documentRepository.go b/src/internal/model/database/documentRepository.go deleted file mode 100644 index 944148d..0000000 --- a/src/internal/model/database/documentRepository.go +++ /dev/null @@ -1,109 +0,0 @@ -package database - -import ( - "crowsnest/internal/model" - "database/sql" -) - -type DocumentRepository struct { - DB *sql.DB -} - -// Gets all the documents objects from the database. This may throw an error if -// the connection to the database fails. -func (d *DocumentRepository) All(limit int, offset int) ([]*model.Document, error) { - stmt := ` - SELECT id, content, summary - FROM documents - LIMIT $1 OFFSET $2 - ` - rows, err := d.DB.Query(stmt, limit, offset) - if err != nil { - return nil, err - } - - docs := []*model.Document{} - for rows.Next() { - d := model.Document{} - err := rows.Scan(&d.Id, &d.Content, &d.Summary) - if err != nil { - return nil, err - } - - docs = append(docs, &d) - } - - if err = rows.Err(); err != nil { - return nil, err - } - - return docs, nil -} - -// Will return an article given an id. This may fail if the connection to the -// database fails or there is no aritcle with the given id. -func (m *DocumentRepository) ById(id int) (*model.Document, error) { - stmt := ` - SELECT id, content, summary - FROM documents - WHERE a.id = $1 - ` - - rows := m.DB.QueryRow(stmt, id) - - d := &model.Document{} - if err := rows.Scan(&d.Id, &d.Content, &d.Summary); err != nil { - return nil, err - } - - return d, nil -} - -// Counts all documents in the database. This may throw an error if the -// connection to the database fails. -func (d *DocumentRepository) CountAll() (uint, error) { - stmt := `SELECT count(id) FROM documents` - - rows := d.DB.QueryRow(stmt) - - count := uint(0) - if err := rows.Scan(&count); err != nil { - return 0, err - } - - return count, nil -} - -func (m *DocumentRepository) Update(d *model.Document) error { - stmt := `UPDATE documents - SET content = $1, summary = $2 - WHERE id = $3 - ` - _, err := m.DB.Exec(stmt, d.Content, d.Summary, d.Id) - return err -} - -func (d *DocumentRepository) Map(transform func(*model.Document) *model.Document) (int, error) { - processed := 0 - - 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++ - } - } - - return processed, nil -}