cruds update
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ConxDb struct {
|
||||
gorm.Model
|
||||
Puerto string `json:"puerto" gorm:"column:puerto"`
|
||||
Usuario string `json:"usuario" gorm:"column:usuario"`
|
||||
Password string `json:"password" gorm:"column:password"`
|
||||
TipoDbID uint `json:"tipo_db_id" gorm:"column:tipo_db_id;foreignKey:TipoDbID"`
|
||||
TipoDb TipoDb `json:"tipo_db" gorm:"foreignKey:TipoDbID"`
|
||||
ServidorID uint `json:"servidor_id" gorm:"column:servidor_id;foreignKey:ServidorID"`
|
||||
Servidor Servidor `json:"servidor" gorm:"foreignKey:ServidorID"`
|
||||
}
|
||||
|
||||
func (ConxDb) TableName() string {
|
||||
return "conx_db"
|
||||
}
|
||||
|
||||
func GetAllConxDb(limit, offset int, search string) ([]ConxDb, int64, error) {
|
||||
var items []ConxDb
|
||||
var total int64
|
||||
db := app.Http.Database.DB.Model(&ConxDb{})
|
||||
|
||||
if search != "" {
|
||||
db = db.Where("nombre LIKE ?", "%"+search+"%")
|
||||
}
|
||||
|
||||
db = db.Preload("Servidor").Preload("TipoDb")
|
||||
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
log.Printf("Error counting conx ssh: %v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if err := db.Order("id DESC").Limit(limit).Offset(offset).Find(&items).Error; err != nil {
|
||||
log.Printf("Error retrieving conx ssh: %v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return items, total, nil
|
||||
}
|
||||
|
||||
func CreateConxDb(conxDb ConxDb) error {
|
||||
if err := app.Http.Database.DB.Create(&conxDb).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateConxDb(conxDb ConxDb) error {
|
||||
if err := app.Http.Database.DB.Model(&conxDb).Updates(conxDb).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteConxDb(conxDb ConxDb) error {
|
||||
if err := app.Http.Database.DB.Delete(&conxDb).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user