diff --git a/api/api.go b/api/api.go index 63d72162..856db8da 100644 --- a/api/api.go +++ b/api/api.go @@ -280,7 +280,7 @@ func (api *api) ListApps() ([]App, error) { } var lastEvent db.RequestEvent - lastEventResult := api.db.Where("app_id = ?", dbApp.ID).Order("id desc").Take(&lastEvent) + lastEventResult := api.db.Where("app_id = ?", dbApp.ID).Order("id desc").Limit(1).Find(&lastEvent) if lastEventResult.RowsAffected > 0 { apiApp.LastEventAt = &lastEvent.CreatedAt } diff --git a/config/config.go b/config/config.go index 413129f3..10395be9 100644 --- a/config/config.go +++ b/config/config.go @@ -96,7 +96,7 @@ func (cfg *config) Get(key string, encryptionKey string) (string, error) { func (cfg *config) get(key string, encryptionKey string, gormDB *gorm.DB) (string, error) { var userConfig db.UserConfig - err := gormDB.Where(&db.UserConfig{Key: key}).Take(&userConfig).Error + err := gormDB.Where(&db.UserConfig{Key: key}).Limit(1).Find(&userConfig).Error if err != nil { return "", fmt.Errorf("failed to get configuration value: %w", gormDB.Error) } diff --git a/nip47/permissions/permissions.go b/nip47/permissions/permissions.go index aeb0ccd3..2ee6b7b7 100644 --- a/nip47/permissions/permissions.go +++ b/nip47/permissions/permissions.go @@ -38,7 +38,7 @@ func NewPermissionsService(db *gorm.DB, eventPublisher events.EventPublisher) *p func (svc *permissionsService) HasPermission(app *db.App, scope string) (result bool, code string, message string) { appPermission := db.AppPermission{} - findPermissionResult := svc.db.Take(&appPermission, &db.AppPermission{ + findPermissionResult := svc.db.Limit(1).Find(&appPermission, &db.AppPermission{ AppId: app.ID, Scope: scope, }) diff --git a/transactions/transactions_service.go b/transactions/transactions_service.go index 9061a8ed..c20f3b10 100644 --- a/transactions/transactions_service.go +++ b/transactions/transactions_service.go @@ -400,9 +400,12 @@ func (svc *transactionsService) LookupTransaction(ctx context.Context, paymentHa if appId != nil { var app db.App - svc.db.Take(&app, &db.App{ + result := svc.db.Limit(1).Find(&app, &db.App{ ID: *appId, }) + if result.RowsAffected == 0 { + return nil, NewNotFoundError() + } if app.Isolated { tx = tx.Where("app_id == ?", *appId) } @@ -471,9 +474,12 @@ func (svc *transactionsService) ListTransactions(ctx context.Context, from, unti if appId != nil { var app db.App - svc.db.Take(&app, &db.App{ + result := svc.db.Limit(1).Find(&app, &db.App{ ID: *appId, }) + if result.RowsAffected == 0 { + return nil, NewNotFoundError() + } if app.Isolated { tx = tx.Where("app_id == ?", *appId) } @@ -552,7 +558,7 @@ func (svc *transactionsService) ConsumeEvent(ctx context.Context, event *events. var dbTransaction db.Transaction err := svc.db.Transaction(func(tx *gorm.DB) error { - result := tx.Take(&dbTransaction, &db.Transaction{ + result := tx.Limit(1).Find(&dbTransaction, &db.Transaction{ Type: constants.TRANSACTION_TYPE_INCOMING, PaymentHash: lnClientTransaction.PaymentHash, }) @@ -637,7 +643,7 @@ func (svc *transactionsService) ConsumeEvent(ctx context.Context, event *events. var dbTransaction db.Transaction err := svc.db.Transaction(func(tx *gorm.DB) error { - result := tx.Take(&dbTransaction, &db.Transaction{ + result := tx.Limit(1).Find(&dbTransaction, &db.Transaction{ Type: constants.TRANSACTION_TYPE_OUTGOING, PaymentHash: lnClientTransaction.PaymentHash, }) @@ -678,7 +684,7 @@ func (svc *transactionsService) ConsumeEvent(ctx context.Context, event *events. lnClientTransaction := paymentFailedAsyncProperties.Transaction var dbTransaction db.Transaction - result := svc.db.Take(&dbTransaction, &db.Transaction{ + result := svc.db.Limit(1).Find(&dbTransaction, &db.Transaction{ Type: constants.TRANSACTION_TYPE_OUTGOING, PaymentHash: lnClientTransaction.PaymentHash, }) @@ -707,7 +713,7 @@ func (svc *transactionsService) ConsumeEvent(ctx context.Context, event *events. func (svc *transactionsService) interceptSelfPayment(paymentHash string) (*lnclient.PayInvoiceResponse, error) { // TODO: extract into separate function incomingTransaction := db.Transaction{} - result := svc.db.Take(&incomingTransaction, &db.Transaction{ + result := svc.db.Limit(1).Find(&incomingTransaction, &db.Transaction{ Type: constants.TRANSACTION_TYPE_INCOMING, State: constants.TRANSACTION_STATE_PENDING, PaymentHash: paymentHash, @@ -748,11 +754,15 @@ func (svc *transactionsService) validateCanPay(tx *gorm.DB, appId *uint, amount // ensure balance for isolated apps if appId != nil { var app db.App - tx.Take(&app, &db.App{ + result := tx.Limit(1).Find(&app, &db.App{ ID: *appId, }) + if result.RowsAffected == 0 { + return NewNotFoundError() + } + var appPermission db.AppPermission - result := tx.Take(&appPermission, &db.AppPermission{ + result = tx.Limit(1).Find(&appPermission, &db.AppPermission{ AppId: *appId, Scope: constants.PAY_INVOICE_SCOPE, })