66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"next-terminal/server/model"
|
|
)
|
|
|
|
var SshGatewayRepository = new(sshGatewayRepository)
|
|
|
|
type sshGatewayRepository struct {
|
|
baseRepository
|
|
}
|
|
|
|
func (r sshGatewayRepository) FindAll(c context.Context) (o []model.SshGateway, err error) {
|
|
err = r.GetDB(c).Order("sort asc, created asc").Find(&o).Error
|
|
return
|
|
}
|
|
|
|
func (r sshGatewayRepository) Find(c context.Context, pageIndex, pageSize int, name string) (o []model.SshGatewayForPage, total int64, err error) {
|
|
db := r.GetDB(c).Table("ssh_gateways")
|
|
dbCounter := r.GetDB(c).Table("ssh_gateways")
|
|
|
|
if len(name) > 0 {
|
|
db = db.Where("name like ?", "%"+name+"%")
|
|
dbCounter = dbCounter.Where("name like ?", "%"+name+"%")
|
|
}
|
|
|
|
err = dbCounter.Count(&total).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err = db.Order("sort asc, created asc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
|
if o == nil {
|
|
o = make([]model.SshGatewayForPage, 0)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r sshGatewayRepository) Create(c context.Context, o *model.SshGateway) error {
|
|
return r.GetDB(c).Create(o).Error
|
|
}
|
|
|
|
func (r sshGatewayRepository) UpdateById(c context.Context, o *model.SshGateway, id string) error {
|
|
o.ID = id
|
|
return r.GetDB(c).Updates(o).Error
|
|
}
|
|
|
|
func (r sshGatewayRepository) DeleteById(c context.Context, id string) error {
|
|
return r.GetDB(c).Where("id = ?", id).Delete(&model.SshGateway{}).Error
|
|
}
|
|
|
|
func (r sshGatewayRepository) FindById(c context.Context, id string) (o model.SshGateway, err error) {
|
|
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
|
|
return
|
|
}
|
|
|
|
func (r sshGatewayRepository) FindAvailableAssets(c context.Context) (o []model.Asset, err error) {
|
|
err = r.GetDB(c).Where("protocol = 'ssh' AND active = ?", true).
|
|
Select("id, name, ip, port").
|
|
Order("name asc").
|
|
Find(&o).Error
|
|
return
|
|
}
|