package models import ( "log" "github.com/sujit-baniya/fiber-boilerplate/app" "gorm.io/gorm" ) type OssApi struct { gorm.Model Name string `gorm:"size:100;not null" json:"name"` // Alias o nombre interno Provider string `gorm:"size:20;default:alibaba" json:"provider"` // alibaba | s3 Endpoint string `gorm:"not null" json:"endpoint"` // Endpoint del access point AccessKeyID string `gorm:"not null" json:"access_key_id"` // Access Key ID AccessKeySecret string `gorm:"not null" json:"access_key_secret"` // Access Key Secret BucketName string `gorm:"not null" json:"bucket_name"` // Nombre del bucket o access point Region string `gorm:"size:50" json:"region"` // Región, opcional IsActive bool `gorm:"default:true" json:"is_active"` // Activar o desactivar config PublicURL string `gorm:"size:255" json:"public_url"` // URL pública para S3 (opcional) Notes string `gorm:"type:text" json:"notes"` } // TableName asegura que GORM use la tabla 'qrvcard' func (OssApi) TableName() string { return "oss_api" } // GetAllOssApi obtiene todos los registros de OssApi con paginación, búsqueda, filtros y orden func GetAllOssApi(limit, offset int, search, provider, estado, sortBy, sortDir, dateFrom, dateTo string) ([]OssApi, int64, error) { var items []OssApi var total int64 db := app.Http.Database.DB.Model(&OssApi{}) if search != "" { db = db.Where("name LIKE ?", "%"+search+"%") } if provider != "" { db = db.Where("provider = ?", provider) } if estado == "activo" { db = db.Where("is_active = ?", true) } else if estado == "inactivo" { db = db.Where("is_active = ?", false) } if dateFrom != "" { db = db.Where("created_at >= ?", dateFrom) } if dateTo != "" { db = db.Where("created_at <= ?", dateTo+" 23:59:59") } if err := db.Count(&total).Error; err != nil { log.Printf("Error counting OssApi: %v", err) return nil, 0, err } allowedSorts := map[string]bool{ "id": true, "name": true, "provider": true, "endpoint": true, "bucket_name": true, "region": true, "is_active": true, "created_at": true, } if !allowedSorts[sortBy] { sortBy = "id" } if sortDir != "asc" { sortDir = "desc" } if err := db.Order(sortBy+" "+sortDir).Limit(limit).Offset(offset).Find(&items).Error; err != nil { log.Printf("Error retrieving OssApi: %v", err) return nil, 0, err } return items, total, nil } // CreateQrVcard crea un nuevo registro de QrVcard en la base de datos func CreateOssApi(OssApi *OssApi) error { if err := app.Http.Database.DB.Create(&OssApi).Error; err != nil { return err } return nil } // UpdateOssApi actualiza un registro de configuración OSS (incluye campos zero-value) func UpdateOssApi(oss *OssApi) error { if err := app.Http.Database.DB.Model(oss).Select("*").Updates(oss).Error; err != nil { return err } return nil } // DeleteOssApi elimina un registro de configuración OSS func DeleteOssApi(oss *OssApi) error { if err := app.Http.Database.DB.Delete(oss).Error; err != nil { return err } return nil } // GetOssApiByID obtiene un registro por ID func GetOssApiByID(id uint) (*OssApi, error) { var item OssApi err := app.Http.Database.DB.First(&item, id).Error if err != nil { return nil, err } return &item, nil } // GetActiveOssApis obtiene todas las configuraciones activas func GetActiveOssApis() ([]OssApi, error) { var items []OssApi err := app.Http.Database.DB.Where("is_active = ?", true).Order("id DESC").Find(&items).Error return items, err } // GetLastActiveOssApi obtiene el último registro activo func GetLastActiveOssApi() (*OssApi, error) { var ossConfig OssApi err := app.Http.Database.DB. Where("is_active = ?", true). Order("id DESC"). First(&ossConfig).Error if err != nil { return nil, err } return &ossConfig, nil }