package docs import ( "context" "database/sql" "encoding/json" "errors" "fmt" "time" ) type Repository struct { db *sql.DB } type DocumentRecord struct { ID string Path string CurrentHash string Title string Tags []string CreatedAt time.Time UpdatedAt time.Time } type AttachmentRecord struct { Hash string OriginalName string ContentType string SizeBytes int64 CreatedAt time.Time } type PersistDocumentInput struct { ID string Path string Title string Hash string PreviousHash string Tags []string } func NewRepository(db *sql.DB) *Repository { return &Repository{db: db} } func (r *Repository) PersistDocument(ctx context.Context, input PersistDocumentInput) error { now := time.Now().UTC() tagsJSON, err := json.Marshal(input.Tags) if err != nil { return fmt.Errorf("marshal tags: %w", err) } existing, err := r.GetDocumentByPath(ctx, input.Path) if err != nil && !errors.Is(err, sql.ErrNoRows) { return err } documentID := input.ID createdAt := now if existing != nil { documentID = existing.ID createdAt = existing.CreatedAt } if _, err := r.db.ExecContext(ctx, ` INSERT INTO documents (id, path, current_hash, title, tags, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT(path) DO UPDATE SET current_hash = excluded.current_hash, title = excluded.title, tags = excluded.tags, updated_at = excluded.updated_at `, documentID, input.Path, input.Hash, input.Title, string(tagsJSON), createdAt.Format(time.RFC3339), now.Format(time.RFC3339)); err != nil { return fmt.Errorf("upsert document %s: %w", input.Path, err) } if existing != nil && existing.CurrentHash == input.Hash { return nil } versionID := documentID + ":" + input.Hash[:12] if _, err := r.db.ExecContext(ctx, ` INSERT OR IGNORE INTO document_versions (id, document_id, hash, previous_hash, created_at, change_summary) VALUES (?, ?, ?, ?, ?, ?) `, versionID, documentID, input.Hash, input.PreviousHash, now.Format(time.RFC3339), "filesystem sync"); err != nil { return fmt.Errorf("insert document version %s: %w", input.Path, err) } return nil } func (r *Repository) GetDocumentByPath(ctx context.Context, path string) (*DocumentRecord, error) { var ( record DocumentRecord tagsJSON string created string updated string ) err := r.db.QueryRowContext(ctx, ` SELECT id, path, current_hash, title, tags, created_at, updated_at FROM documents WHERE path = ? `, path).Scan(&record.ID, &record.Path, &record.CurrentHash, &record.Title, &tagsJSON, &created, &updated) if err != nil { return nil, err } if err := parseDocumentTimes(&record, created, updated); err != nil { return nil, err } if err := json.Unmarshal([]byte(tagsJSON), &record.Tags); err != nil { return nil, fmt.Errorf("unmarshal document tags: %w", err) } return &record, nil } func (r *Repository) ListDocuments(ctx context.Context) ([]DocumentRecord, error) { rows, err := r.db.QueryContext(ctx, ` SELECT id, path, current_hash, title, tags, created_at, updated_at FROM documents ORDER BY path ASC `) if err != nil { return nil, fmt.Errorf("list documents: %w", err) } defer rows.Close() var records []DocumentRecord for rows.Next() { var ( record DocumentRecord tagsJSON string created string updated string ) if err := rows.Scan(&record.ID, &record.Path, &record.CurrentHash, &record.Title, &tagsJSON, &created, &updated); err != nil { return nil, fmt.Errorf("scan document: %w", err) } if err := parseDocumentTimes(&record, created, updated); err != nil { return nil, err } if err := json.Unmarshal([]byte(tagsJSON), &record.Tags); err != nil { return nil, fmt.Errorf("unmarshal document tags: %w", err) } records = append(records, record) } return records, rows.Err() } func (r *Repository) SaveAttachment(ctx context.Context, record AttachmentRecord) error { if _, err := r.db.ExecContext(ctx, ` INSERT INTO attachments (hash, original_name, content_type, size_bytes, created_at) VALUES (?, ?, ?, ?, ?) ON CONFLICT(hash) DO UPDATE SET original_name = excluded.original_name, content_type = excluded.content_type, size_bytes = excluded.size_bytes `, record.Hash, record.OriginalName, record.ContentType, record.SizeBytes, record.CreatedAt.Format(time.RFC3339)); err != nil { return fmt.Errorf("save attachment: %w", err) } return nil } func (r *Repository) GetAttachment(ctx context.Context, hash string) (*AttachmentRecord, error) { var ( record AttachmentRecord created string ) err := r.db.QueryRowContext(ctx, ` SELECT hash, original_name, content_type, size_bytes, created_at FROM attachments WHERE hash = ? `, hash).Scan(&record.Hash, &record.OriginalName, &record.ContentType, &record.SizeBytes, &created) if err != nil { return nil, err } parsed, err := time.Parse(time.RFC3339, created) if err != nil { return nil, fmt.Errorf("parse attachment created_at: %w", err) } record.CreatedAt = parsed return &record, nil } func (r *Repository) Ping(ctx context.Context) error { return r.db.PingContext(ctx) } func parseDocumentTimes(record *DocumentRecord, created string, updated string) error { createdAt, err := time.Parse(time.RFC3339, created) if err != nil { return fmt.Errorf("parse document created_at: %w", err) } updatedAt, err := time.Parse(time.RFC3339, updated) if err != nil { return fmt.Errorf("parse document updated_at: %w", err) } record.CreatedAt = createdAt record.UpdatedAt = updatedAt return nil }