99 lines
3.2 KiB
Go
99 lines
3.2 KiB
Go
package models
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/sujit-baniya/fiber-boilerplate/app"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type DlocalApi struct {
|
|
gorm.Model
|
|
AccessKeyID string `gorm:"not null" json:"access_key_id"` // Access Key ID
|
|
AccessKeySecret string `gorm:"not null" json:"access_key_secret"` // Access Key Secret
|
|
AccessKeyIDdev string `gorm:"not null" json:"access_key_id_dev"` // Access Key ID
|
|
AccessKeySecretdev string `gorm:"not null" json:"access_key_secret_dev"`
|
|
IsActive bool `gorm:"default:true" json:"is_active"` // Activar o desactivar config
|
|
UrlDev string `gorm:"type:text" json:"url_dev"`
|
|
UrlProd string `gorm:"type:text" json:"url_prod"`
|
|
Modo string `gorm:"default:'prod'" json:"modo"`
|
|
}
|
|
type PlanRequest struct {
|
|
Name string `json:"name" validate:"required"`
|
|
Description string `json:"description" validate:"required"`
|
|
Currency string `json:"currency" validate:"required"`
|
|
Amount float64 `json:"amount" validate:"required"`
|
|
FrequencyType string `json:"frequency_type" validate:"required"`
|
|
|
|
Country string `json:"country,omitempty"`
|
|
FrequencyValue int `json:"frequency_value,omitempty"`
|
|
DayOfMonth int `json:"day_of_month,omitempty"`
|
|
NotificationURL string `json:"notification_url,omitempty"`
|
|
SuccessURL string `json:"success_url,omitempty"`
|
|
BackURL string `json:"back_url,omitempty"`
|
|
ErrorURL string `json:"error_url,omitempty"`
|
|
}
|
|
|
|
// TableName asegura que GORM use la tabla 'qrvcard'
|
|
func (DlocalApi) TableName() string {
|
|
return "dlocal_api"
|
|
}
|
|
|
|
// GetAllQrVcard obtiene todos los registros de QrVcard con paginación y búsqueda
|
|
func GetAllDlocalApi(limit, offset int, search string) ([]DlocalApi, int64, error) {
|
|
var items []DlocalApi
|
|
var total int64
|
|
db := app.Http.Database.DB.Model(&DlocalApi{})
|
|
|
|
// Contamos el total de registros
|
|
if err := db.Count(&total).Error; err != nil {
|
|
log.Printf("Error counting DlocalApi: %v", err)
|
|
return nil, 0, err
|
|
}
|
|
|
|
// Obtenemos los registros con paginación
|
|
if err := db.Order("id DESC").Limit(limit).Offset(offset).Find(&items).Error; err != nil {
|
|
log.Printf("Error retrieving DlocalApi: %v", err)
|
|
return nil, 0, err
|
|
}
|
|
|
|
return items, total, nil
|
|
}
|
|
|
|
// CreateQrVcard crea un nuevo registro de QrVcard en la base de datos
|
|
func CreateDlocalApi(DlocalApi *DlocalApi) error {
|
|
if err := app.Http.Database.DB.Create(&DlocalApi).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UpdateQrVcard actualiza un registro de QrVcard existente
|
|
func UpdateDlocalApi(DlocalApi *DlocalApi) error {
|
|
if err := app.Http.Database.DB.Model(&DlocalApi).Updates(DlocalApi).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteQrVcard elimina un registro de QrVcard de la base de datos
|
|
func DeleteDlocalApi(DlocalApi *DlocalApi) error {
|
|
if err := app.Http.Database.DB.Delete(&DlocalApi).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetLastActiveDlocalApi obtiene el último registro activo
|
|
func GetLastActiveDlocalApi() (*DlocalApi, error) {
|
|
var dlocalConfig DlocalApi
|
|
err := app.Http.Database.DB.
|
|
Where("is_active = ?", true).
|
|
Order("id DESC").
|
|
First(&dlocalConfig).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dlocalConfig, nil
|
|
}
|