Skip to content

Commit

Permalink
fix: use limit and find rather than take (#440)
Browse files Browse the repository at this point in the history
take throws an error if a record is not found.

with limit and find, we can control what to do if the record does not exist
  • Loading branch information
rolznz authored Aug 10, 2024
1 parent 11b7140 commit 026b8ba
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion nip47/permissions/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
26 changes: 18 additions & 8 deletions transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
})
Expand Down

0 comments on commit 026b8ba

Please sign in to comment.