This commit is contained in:
@@ -7,8 +7,11 @@ import (
|
||||
"log"
|
||||
|
||||
"FamilyHub/src/domain"
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
var ErrReceiptAlreadyExists = errors.New("receipt already exists")
|
||||
|
||||
type ReceiptsRepository interface {
|
||||
Create(ctx context.Context, receipt *domain.Receipt) (int64, error)
|
||||
GetByID(ctx context.Context, id int64) (*domain.Receipt, error)
|
||||
@@ -129,6 +132,9 @@ func (r *ReceiptsSQLRepository) Create(ctx context.Context, receipt *domain.Rece
|
||||
|
||||
err = tx.QueryRowContext(ctx, query, args...).Scan(&receiptID)
|
||||
if err != nil {
|
||||
if isReceiptAlreadyExistsError(err) {
|
||||
return 0, ErrReceiptAlreadyExists
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -185,6 +191,15 @@ func (r *ReceiptsSQLRepository) Create(ctx context.Context, receipt *domain.Rece
|
||||
return receiptID, nil
|
||||
}
|
||||
|
||||
func isReceiptAlreadyExistsError(err error) bool {
|
||||
var pqErr *pq.Error
|
||||
if !errors.As(err, &pqErr) {
|
||||
return false
|
||||
}
|
||||
|
||||
return pqErr.Code == "23505" && pqErr.Constraint == "receipts_receipt_number_key"
|
||||
}
|
||||
|
||||
func (r *ReceiptsSQLRepository) GetByID(ctx context.Context, id int64) (*domain.Receipt, error) {
|
||||
|
||||
var receipt domain.Receipt
|
||||
|
||||
Reference in New Issue
Block a user