完成重构数据库操作代码

This commit is contained in:
dushixiang
2021-03-19 20:22:04 +08:00
parent 25b8381a4f
commit 2eb4dc3969
37 changed files with 294 additions and 329 deletions

View File

@ -2,7 +2,6 @@ package repository
import (
"gorm.io/gorm"
"next-terminal/server/global"
"next-terminal/server/model"
)
@ -63,19 +62,19 @@ func (r AccessSecurityRepository) Find(pageIndex, pageSize int, ip, rule, order,
}
func (r AccessSecurityRepository) Create(o *model.AccessSecurity) error {
return global.DB.Create(o).Error
return r.DB.Create(o).Error
}
func (r AccessSecurityRepository) UpdateById(o *model.AccessSecurity, id string) error {
o.ID = id
return global.DB.Updates(o).Error
return r.DB.Updates(o).Error
}
func (r AccessSecurityRepository) DeleteById(id string) error {
return global.DB.Where("id = ?", id).Delete(model.AccessSecurity{}).Error
return r.DB.Where("id = ?", id).Delete(model.AccessSecurity{}).Error
}
func (r AccessSecurityRepository) FindById(id string) (o *model.AccessSecurity, err error) {
err = global.DB.Where("id = ?", id).First(&o).Error
err = r.DB.Where("id = ?", id).First(&o).Error
return
}

View File

@ -169,7 +169,7 @@ func (r AssetRepository) DeleteById(id string) error {
return r.DB.Where("id = ?", id).Delete(&model.Asset{}).Error
}
func (r AssetRepository) CountAsset() (total int64, err error) {
func (r AssetRepository) Count() (total int64, err error) {
err = r.DB.Find(&model.Asset{}).Count(&total).Error
return
}

View File

@ -3,7 +3,6 @@ package repository
import (
"gorm.io/gorm"
"next-terminal/server/constant"
"next-terminal/server/global"
"next-terminal/server/model"
)
@ -68,13 +67,13 @@ func (r CommandRepository) Create(o *model.Command) (err error) {
}
func (r CommandRepository) FindById(id string) (o model.Command, err error) {
err = global.DB.Where("id = ?", id).First(&o).Error
err = r.DB.Where("id = ?", id).First(&o).Error
return
}
func (r CommandRepository) UpdateById(o *model.Command, id string) {
func (r CommandRepository) UpdateById(o *model.Command, id string) error {
o.ID = id
global.DB.Updates(o)
return r.DB.Updates(o).Error
}
func (r CommandRepository) DeleteById(id string) error {

View File

@ -15,7 +15,7 @@ func NewCredentialRepository(db *gorm.DB) *CredentialRepository {
return credentialRepository
}
func (r CredentialRepository) FindAllByUser(account model.User) (o []model.CredentialSimpleVo, err error) {
func (r CredentialRepository) FindByUser(account model.User) (o []model.CredentialSimpleVo, err error) {
db := r.DB.Table("credentials").Select("DISTINCT credentials.id,credentials.name").Joins("left join resource_sharers on credentials.id = resource_sharers.resource_id")
if account.Type == constant.TypeUser {
db = db.Where("credentials.owner = ? or resource_sharers.user_id = ?", account.ID, account.ID)

View File

@ -2,7 +2,6 @@ package repository
import (
"gorm.io/gorm"
"next-terminal/server/global"
"next-terminal/server/model"
"next-terminal/server/utils"
)
@ -64,19 +63,6 @@ func (r JobRepository) FindByFunc(function string) (o []model.Job, err error) {
}
func (r JobRepository) Create(o *model.Job) (err error) {
//
//if o.Status == constant.JobStatusRunning {
// j, err := getJob(o)
// if err != nil {
// return err
// }
// jobId, err := global.Cron.AddJob(o.Cron, j)
// if err != nil {
// return err
// }
// o.CronJobId = int(jobId)
//}
return r.DB.Create(o).Error
}
@ -104,5 +90,5 @@ func (r JobRepository) DeleteJobById(id string) error {
// return err
// }
//}
return global.DB.Where("id = ?", id).Delete(model.Job{}).Error
return r.DB.Where("id = ?", id).Delete(model.Job{}).Error
}

View File

@ -2,7 +2,6 @@ package repository
import (
"gorm.io/gorm"
"next-terminal/server/global"
"next-terminal/server/model"
)
@ -21,6 +20,6 @@ func (r NumRepository) FindAll() (o []model.Num, err error) {
}
func (r NumRepository) Create(o *model.Num) (err error) {
err = global.DB.Create(o).Error
err = r.DB.Create(o).Error
return
}

View File

@ -1,11 +1,7 @@
package repository
import (
"github.com/jordan-wright/email"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
"net/smtp"
"next-terminal/server/constant"
"next-terminal/server/guacd"
"next-terminal/server/model"
)
@ -31,7 +27,7 @@ func (r PropertyRepository) Create(o *model.Property) (err error) {
return
}
func (r PropertyRepository) UpdatePropertyByName(o *model.Property, name string) error {
func (r PropertyRepository) UpdateByName(o *model.Property, name string) error {
o.Name = name
return r.DB.Updates(o).Error
}
@ -65,26 +61,3 @@ func (r PropertyRepository) GetRecordingPath() (string, error) {
}
return property.Value, nil
}
func (r PropertyRepository) SendMail(to, subject, text string) {
propertiesMap := r.FindAllMap()
host := propertiesMap[constant.MailHost]
port := propertiesMap[constant.MailPort]
username := propertiesMap[constant.MailUsername]
password := propertiesMap[constant.MailPassword]
if host == "" || port == "" || username == "" || password == "" {
logrus.Debugf("邮箱信息不完整,跳过发送邮件。")
return
}
e := email.NewEmail()
e.From = "Next Terminal <" + username + ">"
e.To = []string{to}
e.Subject = subject
e.Text = []byte(text)
err := e.Send(host+":"+port, smtp.PlainAuth("", username, password, host))
if err != nil {
logrus.Errorf("邮件发送失败: %v", err.Error())
}
}

View File

@ -2,7 +2,6 @@ package repository
import (
"gorm.io/gorm"
"next-terminal/server/global"
"next-terminal/server/model"
"next-terminal/server/utils"
)
@ -56,7 +55,7 @@ func (r UserGroupRepository) Find(pageIndex, pageSize int, name, order, field st
}
func (r UserGroupRepository) FindById(id string) (o model.UserGroup, err error) {
err = global.DB.Where("id = ?", id).First(&o).Error
err = r.DB.Where("id = ?", id).First(&o).Error
return
}
@ -66,7 +65,7 @@ func (r UserGroupRepository) FindUserGroupIdsByUserId(userId string) (o []string
return
}
func (r UserGroupRepository) FindUserGroupMembersByUserGroupId(userGroupId string) (o []string, err error) {
func (r UserGroupRepository) FindMembersById(userGroupId string) (o []string, err error) {
err = r.DB.Table("user_group_members").Select("user_id").Where("user_group_id = ?", userGroupId).Find(&o).Error
return
}
@ -112,9 +111,12 @@ func (r UserGroupRepository) Update(o *model.UserGroup, members []string, id str
})
}
func (r UserGroupRepository) DeleteById(id string) {
r.DB.Where("id = ?", id).Delete(&model.UserGroup{})
r.DB.Where("user_group_id = ?", id).Delete(&model.UserGroupMember{})
func (r UserGroupRepository) DeleteById(id string) (err error) {
err = r.DB.Where("id = ?", id).Delete(&model.UserGroup{}).Error
if err != nil {
return err
}
return r.DB.Where("user_group_id = ?", id).Delete(&model.UserGroupMember{}).Error
}
func AddUserGroupMembers(tx *gorm.DB, userIds []string, userGroupId string) error {