Codevault initial commit

This commit is contained in:
2026-08-16 16:23:14 +03:00
commit 6ee80f1c8f
47 changed files with 3177 additions and 0 deletions

42
internal/db/secrets.go Normal file
View File

@@ -0,0 +1,42 @@
package db
import (
"context"
"database/sql"
"errors"
"fmt"
)
var ErrNotFound = errors.New("secret not found")
type SecretsRepository struct {
db *sql.DB
}
func NewSecretsRepository(db *sql.DB) *SecretsRepository {
return &SecretsRepository{db: db}
}
func (r *SecretsRepository) Get(ctx context.Context, code string) (string, error) {
var value string
err := r.db.QueryRowContext(ctx, "SELECT value FROM secrets WHERE code = ?", code).Scan(&value)
if errors.Is(err, sql.ErrNoRows) {
return "", ErrNotFound
}
if err != nil {
return "", fmt.Errorf("could not get secret: %w", err)
}
return value, nil
}
func (r *SecretsRepository) Set(ctx context.Context, code, value string) error {
_, err := r.db.ExecContext(ctx, `
INSERT INTO secrets (code, value, updated_at)
VALUES (?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(code) DO UPDATE SET value = excluded.value, updated_at = CURRENT_TIMESTAMP
`, code, value)
if err != nil {
return fmt.Errorf("could not set secret: %w", err)
}
return nil
}