66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
package models
|
|
|
|
import (
|
|
"github.com/sujit-baniya/fiber-boilerplate/app"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type NotificacionRegla struct {
|
|
gorm.Model
|
|
Nombre string `json:"nombre" gorm:"column:nombre"`
|
|
DiasAntes int `json:"dias_antes" gorm:"column:dias_antes"`
|
|
PlantillaID uint `json:"plantilla_id" gorm:"column:plantilla_id"`
|
|
Plantilla PlantillaCorreo `json:"plantilla" gorm:"foreignKey:PlantillaID"`
|
|
Activo bool `json:"activo" gorm:"column:activo;default:true"`
|
|
AplicaA string `json:"aplica_a" gorm:"column:aplica_a;default:'todos'"` // todos | renovable | unico
|
|
}
|
|
|
|
func (NotificacionRegla) TableName() string { return "notificacion_reglas" }
|
|
|
|
func GetAllReglas() ([]NotificacionRegla, error) {
|
|
var items []NotificacionRegla
|
|
if err := app.Http.Database.DB.Preload("Plantilla").Order("dias_antes DESC").Find(&items).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func GetReglasActivas() ([]NotificacionRegla, error) {
|
|
var items []NotificacionRegla
|
|
if err := app.Http.Database.DB.Preload("Plantilla").
|
|
Where("activo = ?", true).Order("dias_antes DESC").Find(&items).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func GetReglaByID(id uint) (*NotificacionRegla, error) {
|
|
var item NotificacionRegla
|
|
if err := app.Http.Database.DB.Preload("Plantilla").First(&item, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func CreateRegla(r NotificacionRegla) error {
|
|
return app.Http.Database.DB.Create(&r).Error
|
|
}
|
|
|
|
func UpdateRegla(r NotificacionRegla) error {
|
|
return app.Http.Database.DB.Model(&r).Updates(map[string]interface{}{
|
|
"nombre": r.Nombre,
|
|
"dias_antes": r.DiasAntes,
|
|
"plantilla_id": r.PlantillaID,
|
|
"activo": r.Activo,
|
|
"aplica_a": r.AplicaA,
|
|
}).Error
|
|
}
|
|
|
|
func DeleteRegla(id uint) error {
|
|
var r NotificacionRegla
|
|
if err := app.Http.Database.DB.First(&r, id).Error; err != nil {
|
|
return err
|
|
}
|
|
return app.Http.Database.DB.Delete(&r).Error
|
|
}
|