Files
terminal/server/repository/website.go
T

66 lines
1.9 KiB
Go

package repository
import (
"context"
"next-terminal/server/common"
"next-terminal/server/model"
)
var WebsiteRepository = new(websiteRepository)
type websiteRepository struct {
baseRepository
}
func (r websiteRepository) FindAll(c context.Context) (o []model.Website, err error) {
err = r.GetDB(c).Order("sort asc, created asc").Find(&o).Error
return
}
func (r websiteRepository) Find(c context.Context, pageIndex, pageSize int, keyword string) (o []model.WebsiteForPage, total int64, err error) {
db := r.GetDB(c).Table("websites").Select(`id,name,enabled,target_url,target_host,target_port,domain,status,status_text,
strftime('%s', created) * 1000 as created,group_id,sort`)
dbCounter := r.GetDB(c).Table("websites")
if len(keyword) > 0 {
db = db.Where("name like ? or domain like ?", "%"+keyword+"%", "%"+keyword+"%")
dbCounter = dbCounter.Where("name like ? or domain like ?", "%"+keyword+"%", "%"+keyword+"%")
}
err = dbCounter.Count(&total).Error
if err != nil {
return nil, 0, err
}
err = db.Order("sort asc, created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
if o == nil {
o = make([]model.WebsiteForPage, 0)
}
return
}
func (r websiteRepository) Create(c context.Context, o *model.Website) error {
o.Created = common.NowJsonTime()
return r.GetDB(c).Create(o).Error
}
func (r websiteRepository) UpdateById(c context.Context, o *model.Website, id string) error {
o.ID = id
return r.GetDB(c).Where("id = ?", id).Updates(o).Error
}
func (r websiteRepository) DeleteById(c context.Context, id string) error {
return r.GetDB(c).Where("id = ?", id).Delete(&model.Website{}).Error
}
func (r websiteRepository) FindById(c context.Context, id string) (o model.Website, err error) {
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
return
}
func (r websiteRepository) Count(c context.Context) (total int64, err error) {
err = r.GetDB(c).Model(&model.Website{}).Count(&total).Error
return
}