feat: integración Hostinger + Cloudflare API (modelo, servicio, controlador, rutas, migración)
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CloudflareConfig almacena el API Token de Cloudflare.
|
||||
// Solo un registro puede estar activo a la vez.
|
||||
type CloudflareConfig struct {
|
||||
gorm.Model
|
||||
// API Token (recomendado) – se usa como Bearer token
|
||||
APIToken string `json:"api_token" gorm:"column:api_token;type:text;not null"`
|
||||
// Account ID principal (opcional, para endpoints de cuenta)
|
||||
AccountID string `json:"account_id" gorm:"column:account_id;type:text"`
|
||||
Activo bool `json:"activo" gorm:"column:activo;default:true"`
|
||||
Nota string `json:"nota" gorm:"column:nota;type:text"`
|
||||
}
|
||||
|
||||
func (CloudflareConfig) TableName() string { return "cloudflare_config" }
|
||||
|
||||
// GetCloudflareConfig obtiene la configuración activa.
|
||||
func GetCloudflareConfig() (*CloudflareConfig, error) {
|
||||
var item CloudflareConfig
|
||||
if err := app.Http.Database.DB.Where("activo = ?", true).Order("id DESC").First(&item).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
// SaveCloudflareConfig desactiva la config previa y guarda la nueva.
|
||||
func SaveCloudflareConfig(s CloudflareConfig) error {
|
||||
app.Http.Database.DB.Model(&CloudflareConfig{}).Where("activo = ?", true).
|
||||
Update("activo", false)
|
||||
s.Activo = true
|
||||
if s.ID > 0 {
|
||||
return app.Http.Database.DB.Model(&s).Updates(map[string]interface{}{
|
||||
"api_token": s.APIToken,
|
||||
"account_id": s.AccountID,
|
||||
"nota": s.Nota,
|
||||
"activo": true,
|
||||
}).Error
|
||||
}
|
||||
return app.Http.Database.DB.Create(&s).Error
|
||||
}
|
||||
|
||||
// GetAllCloudflareConfig lista todos los registros con paginación.
|
||||
func GetAllCloudflareConfig(limit, offset int) ([]CloudflareConfig, int64, error) {
|
||||
var items []CloudflareConfig
|
||||
var total int64
|
||||
db := app.Http.Database.DB.Model(&CloudflareConfig{})
|
||||
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
|
||||
}
|
||||
|
||||
// DeleteCloudflareConfig elimina un registro.
|
||||
func DeleteCloudflareConfig(id uint) error {
|
||||
return app.Http.Database.DB.Delete(&CloudflareConfig{}, id).Error
|
||||
}
|
||||
@@ -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