Files
soft_usite/pkg/models/plantilla_correo.go
T
2026-04-29 23:36:30 -05:00

65 lines
1.9 KiB
Go

package models
import (
"github.com/sujit-baniya/fiber-boilerplate/app"
"gorm.io/gorm"
)
type PlantillaCorreo struct {
gorm.Model
Nombre string `json:"nombre" gorm:"column:nombre"`
Asunto string `json:"asunto" gorm:"column:asunto"`
CuerpoHTML string `json:"cuerpo_html" gorm:"column:cuerpo_html;type:text"`
Tipo string `json:"tipo" gorm:"column:tipo;default:'renovacion'"` // renovacion | vencimiento | pago | personalizado
}
func (PlantillaCorreo) TableName() string { return "plantillas_correo" }
func GetAllPlantillas(limit, offset int, search string) ([]PlantillaCorreo, int64, error) {
var items []PlantillaCorreo
var total int64
db := app.Http.Database.DB.Model(&PlantillaCorreo{})
if search != "" {
db = db.Where("nombre ILIKE ? OR asunto ILIKE ?", "%"+search+"%", "%"+search+"%")
}
if err := db.Count(&total).Error; err != nil {
return nil, 0, err
}
if err := db.Order("nombre ASC").Limit(limit).Offset(offset).Find(&items).Error; err != nil {
return nil, 0, err
}
return items, total, nil
}
func GetAllPlantillasSelect() ([]PlantillaCorreo, error) {
var items []PlantillaCorreo
if err := app.Http.Database.DB.Select("id, nombre, tipo").Order("nombre ASC").Find(&items).Error; err != nil {
return nil, err
}
return items, nil
}
func GetPlantillaByID(id uint) (*PlantillaCorreo, error) {
var item PlantillaCorreo
if err := app.Http.Database.DB.First(&item, id).Error; err != nil {
return nil, err
}
return &item, nil
}
func CreatePlantilla(p PlantillaCorreo) error {
return app.Http.Database.DB.Create(&p).Error
}
func UpdatePlantilla(id uint, updates map[string]interface{}) error {
return app.Http.Database.DB.Model(&PlantillaCorreo{}).Where("id = ?", id).Updates(updates).Error
}
func DeletePlantilla(id uint) error {
var p PlantillaCorreo
if err := app.Http.Database.DB.First(&p, id).Error; err != nil {
return err
}
return app.Http.Database.DB.Delete(&p).Error
}