@@ -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"
|
||||
)
|
||||
|
||||
@@ -42,12 +44,20 @@ type (
|
||||
TrustCenterReferences []*TrustCenterReference
|
||||
|
||||
ErrTrustCenterReferenceNotFound struct {
|
||||
ID string
|
||||
Identifier string
|
||||
}
|
||||
|
||||
ErrTrustCenterReferenceAlreadyExists struct {
|
||||
message string
|
||||
}
|
||||
)
|
||||
|
||||
func (e ErrTrustCenterReferenceNotFound) Error() string {
|
||||
return fmt.Sprintf("trust center reference not found: %s", e.ID)
|
||||
return fmt.Sprintf("trust center reference not found: %q", e.Identifier)
|
||||
}
|
||||
|
||||
func (e ErrTrustCenterReferenceAlreadyExists) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
func (t TrustCenterReference) CursorKey(orderBy TrustCenterReferenceOrderField) page.CursorKey {
|
||||
@@ -156,6 +166,14 @@ RETURNING rank;
|
||||
|
||||
err := conn.QueryRow(ctx, q, args).Scan(&t.Rank)
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
if pgErr.Code == "23505" && pgErr.ConstraintName == "trust_center_references_trust_center_id_rank_key" {
|
||||
return &ErrTrustCenterReferenceAlreadyExists{
|
||||
message: fmt.Sprintf("trust center reference with trust_center_id %s and rank already exists", t.TrustCenterID),
|
||||
}
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("cannot insert trust center reference: %w", err)
|
||||
}
|
||||
|
||||
@@ -198,7 +216,7 @@ WHERE
|
||||
}
|
||||
|
||||
if result.RowsAffected() == 0 {
|
||||
return ErrTrustCenterReferenceNotFound{ID: t.ID.String()}
|
||||
return ErrTrustCenterReferenceNotFound{Identifier: t.ID.String()}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user