nginx-proxy-manager/backend/internal/entity/accesslist/model.go

55 lines
1.2 KiB
Go
Raw Normal View History

2023-01-13 05:03:28 +00:00
package accesslist
import (
"npm/internal/database"
"npm/internal/entity"
2023-01-13 05:03:28 +00:00
"npm/internal/entity/user"
"npm/internal/types"
2023-02-24 07:19:07 +00:00
"github.com/rotisserie/eris"
2023-01-13 05:03:28 +00:00
)
// Model is the model
2023-01-13 05:03:28 +00:00
type Model struct {
entity.ModelBase
UserID int `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
Name string `json:"name" gorm:"column:name" filter:"name,string"`
Meta types.JSONB `json:"meta" gorm:"column:meta"`
2023-01-13 05:03:28 +00:00
// Expansions
User *user.Model `json:"user,omitempty" gorm:"-"`
2023-01-13 05:03:28 +00:00
}
// TableName overrides the table name used by gorm
func (Model) TableName() string {
return "access_list"
2023-01-13 05:03:28 +00:00
}
// LoadByID will load from an ID
func (m *Model) LoadByID(id uint) error {
db := database.GetDB()
result := db.First(&m, id)
return result.Error
2023-01-13 05:03:28 +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")
2023-01-13 05:03:28 +00:00
}
db := database.GetDB()
result := db.Save(m)
return result.Error
2023-01-13 05:03:28 +00:00
}
// Delete will mark row as deleted
2023-01-13 05:03:28 +00:00
func (m *Model) Delete() bool {
if m.ID == 0 {
// Can't delete a new object
2023-01-13 05:03:28 +00:00
return false
}
db := database.GetDB()
result := db.Delete(m)
return result.Error == nil
2023-01-13 05:03:28 +00:00
}