wallet/pkg/store/webhook.go
Anton Nesterov b19bf34e5f
[init]
2024-08-31 16:54:43 +02:00

80 lines
1.6 KiB
Go

package store
import "gorm.io/gorm"
type Webhook struct {
gorm.Model
AccountID string `json:"account_id"`
Network string `json:"network"`
Type string `json:"type"`
ContractAddress string `json:"contract_address"`
TransactionHash string `json:"transaction_hash"`
From string `json:"from"`
To string `json:"to"`
Value string `json:"value"`
IsPending bool `json:"is_pending"`
}
func (a *Webhook) GetNetwork() string {
return a.Network
}
func (a *Webhook) GetType() string {
return a.Type
}
func (a *Webhook) GetContractAddress() string {
return a.ContractAddress
}
func (a *Webhook) GetTransactionHash() string {
return a.TransactionHash
}
func (a *Webhook) GetFrom() string {
return a.From
}
func (a *Webhook) GetTo() string {
return a.To
}
func (a *Webhook) GetValue() string {
return a.Value
}
func CreateWebhook(a Webhook) error {
err := db.Create(&a).Error
return err
}
func DeleteWebhook(r Webhook) error {
err := db.Exec("DELETE FROM webhooks WHERE id = ?", r.ID).Error
return err
}
func SetWebhookPending(id uint, is_pending bool) error {
err := db.Model(Webhook{}).Where("id = ?", id).Update("is_pending", is_pending).Error
return err
}
func InitWebhookStore() error {
err := db.Exec("UPDATE webhooks SET is_pending = false WHERE is_pending = true").Error
return err
}
func GetLastWebhook() (Webhook, error) {
var webhook Webhook
err := db.Raw(`
UPDATE webhooks SET is_pending = true
WHERE id IN (
SELECT id FROM webhooks WHERE
is_pending = false
LIMIT 1
FOR UPDATE SKIP LOCKED
)
RETURNING *
`).Scan(&webhook).Error
return webhook, err
}