2021-11-16 12:28:52 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Controller struct {
|
|
|
|
gorm.Model
|
|
|
|
CID string `gorm:"uniqueIndex;column:cid" json:"cid"` // Controller ID
|
|
|
|
CName string `gorm:"column:cname" json:"cname"` // Device ID
|
|
|
|
Key string `gorm:"column:key" json:"key"` // Service ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *dbHandler) AddController(r io.Reader) (*Controller, error) {
|
|
|
|
decoder := json.NewDecoder(r)
|
|
|
|
var controller = &Controller{}
|
|
|
|
|
|
|
|
err := decoder.Decode(controller)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
controller.CID = uuid.NewString()
|
|
|
|
controller.Key = controller.CID
|
|
|
|
|
|
|
|
tx := s.db.Create(controller)
|
|
|
|
if tx.Error != nil {
|
|
|
|
return nil, tx.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.First(&controller, "cid=?", controller.CID)
|
|
|
|
fmt.Println(controller)
|
|
|
|
return controller, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *dbHandler) GetControllers() ([]*Controller, error) {
|
|
|
|
var list []*Controller
|
|
|
|
|
|
|
|
result := s.db.Find(&list)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *dbHandler) IsExistController(cid string) bool {
|
|
|
|
var controller = Controller{}
|
|
|
|
|
|
|
|
result := s.db.First(&controller, "cid=?", cid)
|
|
|
|
|
2021-12-13 05:39:16 +00:00
|
|
|
return result.Error == nil
|
2021-11-16 12:28:52 +00:00
|
|
|
}
|