1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package dao
- import (
- "errors"
- "github.com/gin-gonic/gin"
- "gorm.io/gorm"
- "time"
- )
- // Node 节点数据库
- type Node struct {
- Id int `json:"id" gorm:"primary_key" description:"自增主键"`
- Name string `json:"name" gorm:"column:name" description:"服务器名称"`
- Host string `json:"host" gorm:"column:host" description:"服务器IP"`
- Port int `json:"port" gorm:"column:port" description:"服务器端口"`
- Url string `json:"url" gorm:"column:url" description:"更新IP地址"`
- UpdatedAt time.Time `json:"update_at" gorm:"column:update_at" description:"更新时间"`
- CreatedAt time.Time `json:"create_at" gorm:"column:create_at" description:"创建时间"`
- }
- func (t *Node) TableName() string {
- return "node"
- }
- func (t *Node) GetNode(c *gin.Context, tx *gorm.DB) ([]*Node, error) {
- var node []*Node
- if err := tx.Table(t.TableName()).Find(&node).Error; err != nil {
- return nil, err
- }
- return node, nil
- }
- func (t *Node) IsExists(c *gin.Context, tx *gorm.DB, host string) error {
- var exists bool
- //var count int64
- err := tx.Table("node").WithContext(c).Select("count(*) > 0").Where("host = ?", host).Find(&exists).Error
- if err != nil {
- return err
- }
- if exists {
- return errors.New("数据已经存在")
- }
- return nil
- }
- func (t *Node) Save(c *gin.Context, tx *gorm.DB) error {
- if err := tx.WithContext(c).Save(t).Error; err != nil {
- return err
- }
- return nil
- }
- func (t *Node) Del(c *gin.Context, tx *gorm.DB, host string) error {
- if err := tx.WithContext(c).Where("host = ?", host).Delete(&Node{}).Error; err != nil {
- return err
- }
- return nil
- }
|