feat: integración Hostinger + Cloudflare API (modelo, servicio, controlador, rutas, migración)
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// HostingerConfig almacena el token de acceso a la API de Hostinger.
|
||||
// Solo un registro puede estar activo a la vez.
|
||||
type HostingerConfig struct {
|
||||
gorm.Model
|
||||
Token string `json:"token" gorm:"column:token;type:text;not null"` // Bearer token de Hostinger
|
||||
Activo bool `json:"activo" gorm:"column:activo;default:true"`
|
||||
Nota string `json:"nota" gorm:"column:nota;type:text"`
|
||||
}
|
||||
|
||||
func (HostingerConfig) TableName() string { return "hostinger_config" }
|
||||
|
||||
// GetHostingerConfig obtiene la configuración activa.
|
||||
func GetHostingerConfig() (*HostingerConfig, error) {
|
||||
var item HostingerConfig
|
||||
if err := app.Http.Database.DB.Where("activo = ?", true).Order("id DESC").First(&item).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
// SaveHostingerConfig desactiva la config previa y guarda la nueva.
|
||||
func SaveHostingerConfig(s HostingerConfig) error {
|
||||
app.Http.Database.DB.Model(&HostingerConfig{}).Where("activo = ?", true).
|
||||
Update("activo", false)
|
||||
s.Activo = true
|
||||
if s.ID > 0 {
|
||||
return app.Http.Database.DB.Model(&s).Updates(map[string]interface{}{
|
||||
"token": s.Token,
|
||||
"nota": s.Nota,
|
||||
"activo": true,
|
||||
}).Error
|
||||
}
|
||||
return app.Http.Database.DB.Create(&s).Error
|
||||
}
|
||||
|
||||
// GetAllHostingerConfig lista todos los registros con paginación.
|
||||
func GetAllHostingerConfig(limit, offset int) ([]HostingerConfig, int64, error) {
|
||||
var items []HostingerConfig
|
||||
var total int64
|
||||
db := app.Http.Database.DB.Model(&HostingerConfig{})
|
||||
db.Count(&total)
|
||||
if err := db.Order("id DESC").Limit(limit).Offset(offset).Find(&items).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return items, total, nil
|
||||
}
|
||||
|
||||
// DeleteHostingerConfig elimina un registro.
|
||||
func DeleteHostingerConfig(id uint) error {
|
||||
return app.Http.Database.DB.Delete(&HostingerConfig{}, id).Error
|
||||
}
|
||||
Reference in New Issue
Block a user