2023-01-04 05:36:56 +00:00
|
|
|
package nginxtemplate
|
2022-05-11 22:47:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"npm/internal/database"
|
2023-05-26 01:04:43 +00:00
|
|
|
"npm/internal/entity"
|
2023-02-24 07:19:07 +00:00
|
|
|
|
|
|
|
"github.com/rotisserie/eris"
|
2022-05-11 22:47:31 +00:00
|
|
|
)
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
// Model is the model
|
2022-05-11 22:47:31 +00:00
|
|
|
type Model struct {
|
2023-05-26 01:04:43 +00:00
|
|
|
entity.ModelBase
|
2023-05-29 04:33:58 +00:00
|
|
|
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
|
2023-05-26 01:04:43 +00:00
|
|
|
Name string `json:"name" gorm:"column:name" filter:"name,string"`
|
|
|
|
Type string `json:"type" gorm:"column:type" filter:"type,string"`
|
|
|
|
Template string `json:"template" gorm:"column:template" filter:"template,string"`
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
// TableName overrides the table name used by gorm
|
|
|
|
func (Model) TableName() string {
|
|
|
|
return "nginx_template"
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadByID will load from an ID
|
2023-05-26 01:04:43 +00:00
|
|
|
func (m *Model) LoadByID(id uint) error {
|
|
|
|
db := database.GetDB()
|
|
|
|
result := db.First(&m, id)
|
|
|
|
return result.Error
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save will save this model to the DB
|
|
|
|
func (m *Model) Save() error {
|
|
|
|
if m.UserID == 0 {
|
2023-02-24 07:19:07 +00:00
|
|
|
return eris.Errorf("User ID must be specified")
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
db := database.GetDB()
|
|
|
|
result := db.Save(m)
|
|
|
|
return result.Error
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
// Delete will mark row as deleted
|
2022-05-11 22:47:31 +00:00
|
|
|
func (m *Model) Delete() bool {
|
2023-05-26 01:04:43 +00:00
|
|
|
if m.ID == 0 {
|
|
|
|
// Can't delete a new object
|
2022-05-11 22:47:31 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-05-26 01:04:43 +00:00
|
|
|
db := database.GetDB()
|
|
|
|
result := db.Delete(m)
|
|
|
|
return result.Error == nil
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|