@@ -16,6 +16,7 @@ package coredata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
@@ -23,6 +24,7 @@ import (
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/getprobo/probo/pkg/page"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
|
||||
@@ -39,8 +41,24 @@ type (
|
||||
}
|
||||
|
||||
TrustCenters []*TrustCenter
|
||||
|
||||
ErrTrustCenterNotFound struct {
|
||||
Identifier string
|
||||
}
|
||||
|
||||
ErrTrustCenterAlreadyExists struct {
|
||||
message string
|
||||
}
|
||||
)
|
||||
|
||||
func (e ErrTrustCenterNotFound) Error() string {
|
||||
return fmt.Sprintf("trust center not found: %q", e.Identifier)
|
||||
}
|
||||
|
||||
func (e ErrTrustCenterAlreadyExists) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
func (tc *TrustCenter) CursorKey(orderBy TrustCenterOrderField) page.CursorKey {
|
||||
switch orderBy {
|
||||
case TrustCenterOrderFieldCreatedAt:
|
||||
@@ -218,6 +236,14 @@ INSERT INTO trust_centers (
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
if pgErr.Code == "23505" && pgErr.ConstraintName == "trust_centers_slug_key" {
|
||||
return &ErrTrustCenterAlreadyExists{
|
||||
message: fmt.Sprintf("trust center with slug %q already exists", tc.Slug),
|
||||
}
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("cannot insert trust center: %w", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user