85 lines
2.9 KiB
Go
85 lines
2.9 KiB
Go
package models
|
|
|
|
import (
|
|
"github.com/sujit-baniya/fiber-boilerplate/app"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// SaasProducto representa un producto SaaS al que se le puede asociar documentación.
|
|
// servicio_id es opcional: un SaaS puede estar relacionado con un servicio existente o ser independiente.
|
|
type SaasProducto struct {
|
|
gorm.Model
|
|
Nombre string `json:"nombre" gorm:"column:nombre;not null"`
|
|
Slug string `json:"slug" gorm:"column:slug;uniqueIndex;not null"`
|
|
Descripcion string `json:"descripcion" gorm:"column:descripcion;type:text"`
|
|
LogoURL string `json:"logo_url" gorm:"column:logo_url"`
|
|
ServicioID *uint `json:"servicio_id" gorm:"column:servicio_id"` // nullable FK
|
|
Servicio *Servicio `json:"servicio" gorm:"foreignKey:ServicioID"`
|
|
Activo bool `json:"activo" gorm:"column:activo;default:true"`
|
|
Orden int `json:"orden" gorm:"column:orden;default:0"`
|
|
HealthURL string `json:"health_url" gorm:"column:health_url"` // URL para health check (opcional)
|
|
}
|
|
|
|
func (SaasProducto) TableName() string { return "saas_productos" }
|
|
|
|
func GetAllSaasProductos(limit, offset int, search string) ([]SaasProducto, int64, error) {
|
|
var items []SaasProducto
|
|
var total int64
|
|
db := app.Http.Database.DB.Model(&SaasProducto{}).Preload("Servicio")
|
|
if search != "" {
|
|
db = db.Where("nombre ILIKE ? OR descripcion ILIKE ?", "%"+search+"%", "%"+search+"%")
|
|
}
|
|
if err := db.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if err := db.Order("orden ASC, created_at DESC").Limit(limit).Offset(offset).Find(&items).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return items, total, nil
|
|
}
|
|
|
|
func GetAllSaasProductosSelect() ([]SaasProducto, error) {
|
|
var items []SaasProducto
|
|
if err := app.Http.Database.DB.Where("activo = ?", true).Order("orden ASC, nombre ASC").Find(&items).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func GetSaasProductoByID(id uint) (*SaasProducto, error) {
|
|
var item SaasProducto
|
|
if err := app.Http.Database.DB.Preload("Servicio").First(&item, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func GetSaasProductoBySlug(slug string) (*SaasProducto, error) {
|
|
var item SaasProducto
|
|
if err := app.Http.Database.DB.Where("slug = ?", slug).First(&item).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func CreateSaasProducto(s *SaasProducto) error {
|
|
return app.Http.Database.DB.Create(s).Error
|
|
}
|
|
|
|
func UpdateSaasProducto(s *SaasProducto) error {
|
|
return app.Http.Database.DB.Model(&SaasProducto{}).Where("id = ?", s.ID).Updates(map[string]interface{}{
|
|
"nombre": s.Nombre,
|
|
"slug": s.Slug,
|
|
"descripcion": s.Descripcion,
|
|
"logo_url": s.LogoURL,
|
|
"servicio_id": s.ServicioID,
|
|
"activo": s.Activo,
|
|
"orden": s.Orden,
|
|
"health_url": s.HealthURL,
|
|
}).Error
|
|
}
|
|
|
|
func DeleteSaasProducto(id uint) error {
|
|
return app.Http.Database.DB.Delete(&SaasProducto{}, id).Error
|
|
}
|