55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
|
package service
|
||
|
|
||
|
import (
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"mc-client-updater-server/pkg/dao"
|
||
|
"mc-client-updater-server/pkg/dao/entity"
|
||
|
"mc-client-updater-server/pkg/util"
|
||
|
)
|
||
|
|
||
|
type InstanceService struct {
|
||
|
ctx *gin.Context
|
||
|
}
|
||
|
|
||
|
func NewInstanceService(c *gin.Context) *InstanceService {
|
||
|
return &InstanceService{ctx: c}
|
||
|
}
|
||
|
|
||
|
func (s *InstanceService) AddInstance(name, updateURL string) (*entity.Instance, error) {
|
||
|
instEntity := entity.Instance{
|
||
|
Name: name,
|
||
|
UpdateURL: updateURL,
|
||
|
}
|
||
|
tx := dao.DB().Create(&instEntity)
|
||
|
if tx.Error != nil {
|
||
|
return nil, tx.Error
|
||
|
}
|
||
|
|
||
|
tx = dao.DB().Where(&instEntity).Last(&instEntity)
|
||
|
return &instEntity, tx.Error
|
||
|
}
|
||
|
|
||
|
func (s *InstanceService) GetInstanceByName(name string) (*entity.Instance, error) {
|
||
|
instEntity := entity.Instance{}
|
||
|
tx := dao.DB().Where("name=?", name).Last(&instEntity)
|
||
|
return &instEntity, tx.Error
|
||
|
}
|
||
|
|
||
|
func (s *InstanceService) NewGrantToken(instName string, expireStr string) (*entity.Grant, error) {
|
||
|
expireAt := util.MustParseSQLTime(expireStr)
|
||
|
grantEntity := entity.Grant{GrantTo: instName, ExpireAt: expireAt, Token: util.RandStr(32)}
|
||
|
tx := dao.DB().Create(&grantEntity)
|
||
|
if tx.Error != nil {
|
||
|
return nil, tx.Error
|
||
|
}
|
||
|
|
||
|
tx = dao.DB().Where(&grantEntity).Last(&grantEntity)
|
||
|
return &grantEntity, tx.Error
|
||
|
}
|
||
|
|
||
|
func (s *InstanceService) GetGrantByToken(token string) (*entity.Grant, error) {
|
||
|
grantEntity := entity.Grant{}
|
||
|
tx := dao.DB().Where("token=?", token).Last(&grantEntity)
|
||
|
return &grantEntity, tx.Error
|
||
|
}
|