Skip to content

Commit 5838230

Browse files
committed
fix: Add missing files
1 parent d18f401 commit 5838230

File tree

8 files changed

+877
-0
lines changed

8 files changed

+877
-0
lines changed

internal/domain/models/document.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: AGPL-3.0-or-later
2+
package models
3+
4+
import "time"
5+
6+
// Document represents document metadata for tracking and integrity verification
7+
type Document struct {
8+
DocID string `json:"doc_id" db:"doc_id"`
9+
Title string `json:"title" db:"title"`
10+
URL string `json:"url" db:"url"`
11+
Checksum string `json:"checksum" db:"checksum"`
12+
ChecksumAlgorithm string `json:"checksum_algorithm" db:"checksum_algorithm"`
13+
Description string `json:"description" db:"description"`
14+
CreatedAt time.Time `json:"created_at" db:"created_at"`
15+
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
16+
CreatedBy string `json:"created_by" db:"created_by"`
17+
}
18+
19+
// DocumentInput represents the input for creating/updating document metadata
20+
type DocumentInput struct {
21+
Title string `json:"title"`
22+
URL string `json:"url"`
23+
Checksum string `json:"checksum"`
24+
ChecksumAlgorithm string `json:"checksum_algorithm"`
25+
Description string `json:"description"`
26+
}
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
// SPDX-License-Identifier: AGPL-3.0-or-later
2+
package database
3+
4+
import (
5+
"context"
6+
"database/sql"
7+
"fmt"
8+
9+
"github.com/btouchard/ackify-ce/internal/domain/models"
10+
"github.com/btouchard/ackify-ce/pkg/logger"
11+
)
12+
13+
// DocumentRepository handles document metadata persistence
14+
type DocumentRepository struct {
15+
db *sql.DB
16+
}
17+
18+
// NewDocumentRepository creates a new DocumentRepository
19+
func NewDocumentRepository(db *sql.DB) *DocumentRepository {
20+
return &DocumentRepository{db: db}
21+
}
22+
23+
// Create creates a new document metadata entry
24+
func (r *DocumentRepository) Create(ctx context.Context, docID string, input models.DocumentInput, createdBy string) (*models.Document, error) {
25+
query := `
26+
INSERT INTO documents (doc_id, title, url, checksum, checksum_algorithm, description, created_by)
27+
VALUES ($1, $2, $3, $4, $5, $6, $7)
28+
RETURNING doc_id, title, url, checksum, checksum_algorithm, description, created_at, updated_at, created_by
29+
`
30+
31+
doc := &models.Document{}
32+
err := r.db.QueryRowContext(
33+
ctx,
34+
query,
35+
docID,
36+
input.Title,
37+
input.URL,
38+
input.Checksum,
39+
input.ChecksumAlgorithm,
40+
input.Description,
41+
createdBy,
42+
).Scan(
43+
&doc.DocID,
44+
&doc.Title,
45+
&doc.URL,
46+
&doc.Checksum,
47+
&doc.ChecksumAlgorithm,
48+
&doc.Description,
49+
&doc.CreatedAt,
50+
&doc.UpdatedAt,
51+
&doc.CreatedBy,
52+
)
53+
54+
if err != nil {
55+
logger.Logger.Error("Failed to create document", "error", err.Error(), "doc_id", docID)
56+
return nil, fmt.Errorf("failed to create document: %w", err)
57+
}
58+
59+
return doc, nil
60+
}
61+
62+
// GetByDocID retrieves document metadata by document ID
63+
func (r *DocumentRepository) GetByDocID(ctx context.Context, docID string) (*models.Document, error) {
64+
query := `
65+
SELECT doc_id, title, url, checksum, checksum_algorithm, description, created_at, updated_at, created_by
66+
FROM documents
67+
WHERE doc_id = $1
68+
`
69+
70+
doc := &models.Document{}
71+
err := r.db.QueryRowContext(ctx, query, docID).Scan(
72+
&doc.DocID,
73+
&doc.Title,
74+
&doc.URL,
75+
&doc.Checksum,
76+
&doc.ChecksumAlgorithm,
77+
&doc.Description,
78+
&doc.CreatedAt,
79+
&doc.UpdatedAt,
80+
&doc.CreatedBy,
81+
)
82+
83+
if err == sql.ErrNoRows {
84+
return nil, nil
85+
}
86+
87+
if err != nil {
88+
logger.Logger.Error("Failed to get document", "error", err.Error(), "doc_id", docID)
89+
return nil, fmt.Errorf("failed to get document: %w", err)
90+
}
91+
92+
return doc, nil
93+
}
94+
95+
// Update updates document metadata
96+
func (r *DocumentRepository) Update(ctx context.Context, docID string, input models.DocumentInput) (*models.Document, error) {
97+
query := `
98+
UPDATE documents
99+
SET title = $2, url = $3, checksum = $4, checksum_algorithm = $5, description = $6
100+
WHERE doc_id = $1
101+
RETURNING doc_id, title, url, checksum, checksum_algorithm, description, created_at, updated_at, created_by
102+
`
103+
104+
doc := &models.Document{}
105+
err := r.db.QueryRowContext(
106+
ctx,
107+
query,
108+
docID,
109+
input.Title,
110+
input.URL,
111+
input.Checksum,
112+
input.ChecksumAlgorithm,
113+
input.Description,
114+
).Scan(
115+
&doc.DocID,
116+
&doc.Title,
117+
&doc.URL,
118+
&doc.Checksum,
119+
&doc.ChecksumAlgorithm,
120+
&doc.Description,
121+
&doc.CreatedAt,
122+
&doc.UpdatedAt,
123+
&doc.CreatedBy,
124+
)
125+
126+
if err == sql.ErrNoRows {
127+
return nil, fmt.Errorf("document not found")
128+
}
129+
130+
if err != nil {
131+
logger.Logger.Error("Failed to update document", "error", err.Error(), "doc_id", docID)
132+
return nil, fmt.Errorf("failed to update document: %w", err)
133+
}
134+
135+
return doc, nil
136+
}
137+
138+
// CreateOrUpdate creates or updates document metadata
139+
func (r *DocumentRepository) CreateOrUpdate(ctx context.Context, docID string, input models.DocumentInput, createdBy string) (*models.Document, error) {
140+
query := `
141+
INSERT INTO documents (doc_id, title, url, checksum, checksum_algorithm, description, created_by)
142+
VALUES ($1, $2, $3, $4, $5, $6, $7)
143+
ON CONFLICT (doc_id) DO UPDATE SET
144+
title = EXCLUDED.title,
145+
url = EXCLUDED.url,
146+
checksum = EXCLUDED.checksum,
147+
checksum_algorithm = EXCLUDED.checksum_algorithm,
148+
description = EXCLUDED.description
149+
RETURNING doc_id, title, url, checksum, checksum_algorithm, description, created_at, updated_at, created_by
150+
`
151+
152+
doc := &models.Document{}
153+
err := r.db.QueryRowContext(
154+
ctx,
155+
query,
156+
docID,
157+
input.Title,
158+
input.URL,
159+
input.Checksum,
160+
input.ChecksumAlgorithm,
161+
input.Description,
162+
createdBy,
163+
).Scan(
164+
&doc.DocID,
165+
&doc.Title,
166+
&doc.URL,
167+
&doc.Checksum,
168+
&doc.ChecksumAlgorithm,
169+
&doc.Description,
170+
&doc.CreatedAt,
171+
&doc.UpdatedAt,
172+
&doc.CreatedBy,
173+
)
174+
175+
if err != nil {
176+
logger.Logger.Error("Failed to create or update document", "error", err.Error(), "doc_id", docID)
177+
return nil, fmt.Errorf("failed to create or update document: %w", err)
178+
}
179+
180+
return doc, nil
181+
}
182+
183+
// Delete deletes document metadata
184+
func (r *DocumentRepository) Delete(ctx context.Context, docID string) error {
185+
query := `DELETE FROM documents WHERE doc_id = $1`
186+
187+
result, err := r.db.ExecContext(ctx, query, docID)
188+
if err != nil {
189+
logger.Logger.Error("Failed to delete document", "error", err.Error(), "doc_id", docID)
190+
return fmt.Errorf("failed to delete document: %w", err)
191+
}
192+
193+
rows, err := result.RowsAffected()
194+
if err != nil {
195+
return fmt.Errorf("failed to get rows affected: %w", err)
196+
}
197+
198+
if rows == 0 {
199+
return fmt.Errorf("document not found")
200+
}
201+
202+
return nil
203+
}
204+
205+
// List retrieves all documents with pagination
206+
func (r *DocumentRepository) List(ctx context.Context, limit, offset int) ([]*models.Document, error) {
207+
query := `
208+
SELECT doc_id, title, url, checksum, checksum_algorithm, description, created_at, updated_at, created_by
209+
FROM documents
210+
ORDER BY created_at DESC
211+
LIMIT $1 OFFSET $2
212+
`
213+
214+
rows, err := r.db.QueryContext(ctx, query, limit, offset)
215+
if err != nil {
216+
logger.Logger.Error("Failed to list documents", "error", err.Error())
217+
return nil, fmt.Errorf("failed to list documents: %w", err)
218+
}
219+
defer rows.Close()
220+
221+
documents := []*models.Document{}
222+
for rows.Next() {
223+
doc := &models.Document{}
224+
err := rows.Scan(
225+
&doc.DocID,
226+
&doc.Title,
227+
&doc.URL,
228+
&doc.Checksum,
229+
&doc.ChecksumAlgorithm,
230+
&doc.Description,
231+
&doc.CreatedAt,
232+
&doc.UpdatedAt,
233+
&doc.CreatedBy,
234+
)
235+
if err != nil {
236+
logger.Logger.Error("Failed to scan document row", "error", err.Error())
237+
return nil, fmt.Errorf("failed to scan document: %w", err)
238+
}
239+
documents = append(documents, doc)
240+
}
241+
242+
if err := rows.Err(); err != nil {
243+
return nil, fmt.Errorf("error iterating documents: %w", err)
244+
}
245+
246+
return documents, nil
247+
}

0 commit comments

Comments
 (0)