v1
This commit is contained in:
26
repo.go
26
repo.go
@@ -16,17 +16,15 @@ var (
|
||||
)
|
||||
|
||||
type Repo[T any] interface {
|
||||
// Create inserts object into repository table. If id already exists
|
||||
// Create saves object to the repository. If id already exists
|
||||
// in the database it is an error.
|
||||
Create(ctx context.Context, id string, v *T) error
|
||||
// Read returns object with specified id or ErrNotFound
|
||||
Read(ctx context.Context, id string) (*T, error)
|
||||
// Update updates object with id.
|
||||
Update(ctx context.Context, id string, v *T) error
|
||||
// Delete performs soft-delete (actually marks object as unavailable).
|
||||
// Delete deletes object from the database.
|
||||
Delete(ctx context.Context, id string) error
|
||||
// Purge actually deletes database record with id.
|
||||
Purge(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
// OpenOrCreate accepts *sql.DB and tablename and tries to create the named table
|
||||
@@ -65,7 +63,7 @@ func (r *repo[T]) Create(ctx context.Context, id string, v *T) error {
|
||||
}
|
||||
|
||||
func (r *repo[T]) Read(ctx context.Context, id string) (*T, error) {
|
||||
query := "SELECT payload FROM " + r.table + " WHERE id = $1 AND deleted_at is NULL"
|
||||
query := "SELECT payload FROM " + r.table + " WHERE id = $1"
|
||||
|
||||
row := r.db.QueryRowContext(ctx, query, id)
|
||||
|
||||
@@ -94,7 +92,7 @@ func (r *repo[T]) Update(ctx context.Context, id string, v *T) error {
|
||||
return err
|
||||
}
|
||||
|
||||
query := "UPDATE " + r.table + " SET updated_at = $1, payload = $2 WHERE id = $3 AND deleted_at IS NULL"
|
||||
query := "UPDATE " + r.table + " SET updated_at = $1, payload = $2 WHERE id = $3"
|
||||
|
||||
if err := r.execContext(ctx, query, now, string(b), id); err != nil {
|
||||
return err
|
||||
@@ -103,21 +101,8 @@ func (r *repo[T]) Update(ctx context.Context, id string, v *T) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete performs soft-delete. It just marks the record as deleted and it will
|
||||
// no longer be available to Read and Update methods.
|
||||
// Delete deletes record with cpecified id.
|
||||
func (r *repo[T]) Delete(ctx context.Context, id string) error {
|
||||
now := time.Now()
|
||||
|
||||
query := "UPDATE " + r.table + " SET deleted_at = $1 WHERE id = $2 AND deleted_at IS NULL"
|
||||
|
||||
if err := r.execContext(ctx, query, now, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo[T]) Purge(ctx context.Context, id string) error {
|
||||
query := "DELETE FROM " + r.table + " WHERE id = $1"
|
||||
|
||||
if err := r.execContext(ctx, query, id); err != nil {
|
||||
@@ -132,6 +117,7 @@ func (r *repo[T]) execContext(ctx context.Context, query string, args ...any) er
|
||||
if err != nil {
|
||||
return errors.Join(ErrExecQuery, err)
|
||||
}
|
||||
|
||||
affected, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return errors.Join(ErrExecQuery, err)
|
||||
|
Reference in New Issue
Block a user