2025-06-09 19:14:28 -03:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
2025-06-09 19:24:06 -03:00
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
2025-06-09 19:14:28 -03:00
|
|
|
"errors"
|
2025-06-09 19:24:06 -03:00
|
|
|
"log/slog"
|
2025-06-09 19:14:28 -03:00
|
|
|
"time"
|
|
|
|
|
|
2025-10-13 15:26:31 -03:00
|
|
|
"code.capytal.cc/loreddev/x/tinyssert"
|
2025-06-09 19:14:28 -03:00
|
|
|
)
|
2025-06-09 19:24:06 -03:00
|
|
|
|
2025-06-26 19:11:14 -03:00
|
|
|
// TODO: Add rowback to all return errors, or use context to cancel operations
|
|
|
|
|
|
2025-06-09 19:24:06 -03:00
|
|
|
type baseRepostiory struct {
|
|
|
|
|
db *sql.DB
|
|
|
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
log *slog.Logger
|
|
|
|
|
assert tinyssert.Assertions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newBaseRepostiory(ctx context.Context, db *sql.DB, log *slog.Logger, assert tinyssert.Assertions) baseRepostiory {
|
|
|
|
|
assert.NotNil(db)
|
|
|
|
|
assert.NotNil(ctx)
|
|
|
|
|
assert.NotNil(log)
|
|
|
|
|
|
|
|
|
|
return baseRepostiory{
|
|
|
|
|
db: db,
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
log: log,
|
|
|
|
|
assert: assert,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 19:14:28 -03:00
|
|
|
var (
|
2025-06-13 19:16:32 -03:00
|
|
|
// TODO: Change all ErrDatabaseConn to ErrCloseConn
|
2025-11-18 13:19:55 -03:00
|
|
|
// TODO: Change error to be agnostic to underlying storage type
|
2025-06-10 14:54:30 -03:00
|
|
|
ErrDatabaseConn = errors.New("repository: failed to begin transaction/connection with database")
|
2025-06-13 19:16:32 -03:00
|
|
|
ErrCloseConn = errors.New("repository: failed to close/commit connection")
|
2025-06-10 14:54:30 -03:00
|
|
|
ErrExecuteQuery = errors.New("repository: failed to execute query")
|
|
|
|
|
ErrCommitQuery = errors.New("repository: failed to commit transaction")
|
|
|
|
|
ErrInvalidInput = errors.New("repository: data sent to save is invalid")
|
|
|
|
|
ErrInvalidOutput = errors.New("repository: data found is not valid")
|
|
|
|
|
ErrNotFound = sql.ErrNoRows
|
2025-06-09 19:14:28 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var dateFormat = time.RFC3339
|
2025-06-10 14:54:09 -03:00
|
|
|
|
|
|
|
|
type scan interface {
|
|
|
|
|
Scan(dest ...any) error
|
|
|
|
|
}
|