Files
terminal/server/repository/authorised_asset.go
T

385 lines
13 KiB
Go

package repository
import (
"context"
"next-terminal/server/common"
"next-terminal/server/model"
)
var AuthorisedAssetRepository = new(authorisedAssetRepository)
type authorisedAssetRepository struct {
baseRepository
}
func (r authorisedAssetRepository) Find(c context.Context, pageIndex, pageSize int, userId, departmentId, assetGroupId, assetId string) (o []model.AuthorisedAsset, total int64, err error) {
db := r.GetDB(c).Model(&model.AuthorisedAsset{})
if userId != "" {
db = db.Where("user_id = ?", userId)
}
if departmentId != "" {
db = db.Where("department_id = ?", departmentId)
}
if assetGroupId != "" {
db = db.Where("asset_group_id = ?", assetGroupId)
}
if assetId != "" {
db = db.Where("asset_id = ?", assetId)
}
err = db.Count(&total).Error
if err != nil {
return
}
err = db.Order("created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
return
}
func (r authorisedAssetRepository) FindById(c context.Context, id string) (o model.AuthorisedAsset, err error) {
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
return
}
func (r authorisedAssetRepository) Create(c context.Context, o *model.AuthorisedAsset) error {
o.Created = common.NowJsonTime()
return r.GetDB(c).Create(o).Error
}
func (r authorisedAssetRepository) CreateInBatches(c context.Context, items []model.AuthorisedAsset) error {
for i := range items {
items[i].Created = common.NowJsonTime()
}
return r.GetDB(c).CreateInBatches(items, 100).Error
}
func (r authorisedAssetRepository) UpdateById(c context.Context, o *model.AuthorisedAsset) error {
return r.GetDB(c).Updates(o).Error
}
func (r authorisedAssetRepository) DeleteById(c context.Context, id string) error {
return r.GetDB(c).Where("id = ?", id).Delete(&model.AuthorisedAsset{}).Error
}
func (r authorisedAssetRepository) Selected(c context.Context, expect, userId, departmentId, assetId string) (result []string, err error) {
var items []model.AuthorisedAsset
db := r.GetDB(c)
switch expect {
case "userId":
db = db.Select("user_id")
case "departmentId":
db = db.Select("department_id")
case "assetId":
db = db.Select("asset_id")
case "assetGroupId":
db = db.Select("asset_group_id")
}
if userId != "" {
db = db.Where("user_id = ?", userId)
}
if departmentId != "" {
db = db.Where("department_id = ?", departmentId)
}
if assetId != "" {
db = db.Where("asset_id = ?", assetId)
}
err = db.Find(&items).Error
if err != nil {
return
}
for _, item := range items {
switch expect {
case "userId":
if item.UserId != "" {
result = append(result, item.UserId)
}
case "departmentId":
if item.DepartmentId != "" {
result = append(result, item.DepartmentId)
}
case "assetId":
if item.AssetId != "" {
result = append(result, item.AssetId)
}
case "assetGroupId":
if item.AssetGroupId != "" {
result = append(result, item.AssetGroupId)
}
}
}
return
}
func (r authorisedAssetRepository) FindWithDetails(c context.Context, pageIndex, pageSize int, userId, departmentId, assetGroupId, assetId string) (o []map[string]interface{}, total int64, err error) {
db := r.GetDB(c).Table("authorised_assets").
Select(`authorised_assets.id,
strftime('%s', authorised_assets.created) * 1000 as "createdAt",
authorised_assets.expired_at as "expiredAt",
authorised_assets.user_id as "userId", users.nickname as "userName",
authorised_assets.department_id as "departmentId", departments.name as "departmentName",
authorised_assets.asset_id as "assetId", assets.name as "assetName",
authorised_assets.asset_group_id as "assetGroupId", asset_groups.name as "assetGroupName",
authorised_assets.strategy_id as "strategyId", strategies.name as "strategyName"`).
Joins("left join users on users.id = authorised_assets.user_id").
Joins("left join departments on departments.id = authorised_assets.department_id").
Joins("left join assets on assets.id = authorised_assets.asset_id").
Joins("left join asset_groups on asset_groups.id = authorised_assets.asset_group_id").
Joins("left join strategies on strategies.id = authorised_assets.strategy_id")
dbCounter := r.GetDB(c).Model(&model.AuthorisedAsset{})
if userId != "" {
db = db.Where("authorised_assets.user_id = ?", userId)
dbCounter = dbCounter.Where("user_id = ?", userId)
}
if departmentId != "" {
db = db.Where("authorised_assets.department_id = ?", departmentId)
dbCounter = dbCounter.Where("department_id = ?", departmentId)
}
if assetGroupId != "" {
db = db.Where("authorised_assets.asset_group_id = ?", assetGroupId)
dbCounter = dbCounter.Where("asset_group_id = ?", assetGroupId)
}
if assetId != "" {
db = db.Where("authorised_assets.asset_id = ?", assetId)
dbCounter = dbCounter.Where("asset_id = ?", assetId)
}
err = dbCounter.Count(&total).Error
if err != nil {
return
}
err = db.Order("authorised_assets.created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
return
}
var AuthorisedDatabaseAssetRepository = new(authorisedDatabaseAssetRepository)
type authorisedDatabaseAssetRepository struct {
baseRepository
}
func (r authorisedDatabaseAssetRepository) Find(c context.Context, pageIndex, pageSize int, userId, departmentId, databaseAssetId string) (o []model.AuthorisedDatabaseAsset, total int64, err error) {
db := r.GetDB(c).Model(&model.AuthorisedDatabaseAsset{})
if userId != "" {
db = db.Where("user_id = ?", userId)
}
if departmentId != "" {
db = db.Where("department_id = ?", departmentId)
}
if databaseAssetId != "" {
db = db.Where("database_asset_id = ?", databaseAssetId)
}
err = db.Count(&total).Error
if err != nil {
return
}
err = db.Order("created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
return
}
func (r authorisedDatabaseAssetRepository) FindById(c context.Context, id string) (o model.AuthorisedDatabaseAsset, err error) {
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
return
}
func (r authorisedDatabaseAssetRepository) Create(c context.Context, o *model.AuthorisedDatabaseAsset) error {
o.Created = common.NowJsonTime()
return r.GetDB(c).Create(o).Error
}
func (r authorisedDatabaseAssetRepository) CreateInBatches(c context.Context, items []model.AuthorisedDatabaseAsset) error {
for i := range items {
items[i].Created = common.NowJsonTime()
}
return r.GetDB(c).CreateInBatches(items, 100).Error
}
func (r authorisedDatabaseAssetRepository) UpdateById(c context.Context, o *model.AuthorisedDatabaseAsset) error {
return r.GetDB(c).Updates(o).Error
}
func (r authorisedDatabaseAssetRepository) DeleteById(c context.Context, id string) error {
return r.GetDB(c).Where("id = ?", id).Delete(&model.AuthorisedDatabaseAsset{}).Error
}
func (r authorisedDatabaseAssetRepository) Selected(c context.Context, expect, userId, departmentId, databaseAssetId string) (result []string, err error) {
var items []model.AuthorisedDatabaseAsset
db := r.GetDB(c)
switch expect {
case "userId":
db = db.Select("user_id")
case "departmentId":
db = db.Select("department_id")
case "databaseAssetId":
db = db.Select("database_asset_id")
}
if userId != "" {
db = db.Where("user_id = ?", userId)
}
if departmentId != "" {
db = db.Where("department_id = ?", departmentId)
}
if databaseAssetId != "" {
db = db.Where("database_asset_id = ?", databaseAssetId)
}
err = db.Find(&items).Error
if err != nil {
return
}
for _, item := range items {
switch expect {
case "userId":
if item.UserId != "" {
result = append(result, item.UserId)
}
case "departmentId":
if item.DepartmentId != "" {
result = append(result, item.DepartmentId)
}
case "databaseAssetId":
if item.DatabaseAssetId != "" {
result = append(result, item.DatabaseAssetId)
}
}
}
return
}
func (r authorisedDatabaseAssetRepository) FindWithDetails(c context.Context, pageIndex, pageSize int, userId, departmentId, databaseAssetId string) (o []map[string]interface{}, total int64, err error) {
db := r.GetDB(c).Table("authorised_database_assets").
Select(`authorised_database_assets.id,
strftime('%s', authorised_database_assets.created) * 1000 as "createdAt",
authorised_database_assets.expired_at as "expiredAt",
authorised_database_assets.user_id as "userId", users.nickname as "userName",
authorised_database_assets.department_id as "departmentId", departments.name as "departmentName",
authorised_database_assets.database_asset_id as "databaseAssetId", database_assets.name as "databaseAssetName",
authorised_database_assets.strategy_id as "strategyId", strategies.name as "strategyName"`).
Joins("left join users on users.id = authorised_database_assets.user_id").
Joins("left join departments on departments.id = authorised_database_assets.department_id").
Joins("left join database_assets on database_assets.id = authorised_database_assets.database_asset_id").
Joins("left join strategies on strategies.id = authorised_database_assets.strategy_id")
dbCounter := r.GetDB(c).Model(&model.AuthorisedDatabaseAsset{})
if userId != "" {
db = db.Where("authorised_database_assets.user_id = ?", userId)
dbCounter = dbCounter.Where("user_id = ?", userId)
}
if departmentId != "" {
db = db.Where("authorised_database_assets.department_id = ?", departmentId)
dbCounter = dbCounter.Where("department_id = ?", departmentId)
}
if databaseAssetId != "" {
db = db.Where("authorised_database_assets.database_asset_id = ?", databaseAssetId)
dbCounter = dbCounter.Where("database_asset_id = ?", databaseAssetId)
}
err = dbCounter.Count(&total).Error
if err != nil {
return
}
err = db.Order("authorised_database_assets.created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
return
}
var AuthorisedWebsiteRepository = new(authorisedWebsiteRepository)
type authorisedWebsiteRepository struct {
baseRepository
}
func (r authorisedWebsiteRepository) Find(c context.Context, pageIndex, pageSize int, userId, departmentId, websiteId string) (o []model.AuthorisedWebsite, total int64, err error) {
db := r.GetDB(c).Model(&model.AuthorisedWebsite{})
if userId != "" {
db = db.Where("user_id = ?", userId)
}
if departmentId != "" {
db = db.Where("department_id = ?", departmentId)
}
if websiteId != "" {
db = db.Where("website_id = ?", websiteId)
}
err = db.Count(&total).Error
if err != nil {
return
}
err = db.Order("created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
return
}
func (r authorisedWebsiteRepository) FindById(c context.Context, id string) (o model.AuthorisedWebsite, err error) {
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
return
}
func (r authorisedWebsiteRepository) Create(c context.Context, o *model.AuthorisedWebsite) error {
o.Created = common.NowJsonTime()
return r.GetDB(c).Create(o).Error
}
func (r authorisedWebsiteRepository) CreateInBatches(c context.Context, items []model.AuthorisedWebsite) error {
for i := range items {
items[i].Created = common.NowJsonTime()
}
return r.GetDB(c).CreateInBatches(items, 100).Error
}
func (r authorisedWebsiteRepository) DeleteById(c context.Context, id string) error {
return r.GetDB(c).Where("id = ?", id).Delete(&model.AuthorisedWebsite{}).Error
}
func (r authorisedWebsiteRepository) FindWithDetails(c context.Context, pageIndex, pageSize int, userId, departmentId, websiteId string) (o []map[string]interface{}, total int64, err error) {
db := r.GetDB(c).Table("authorised_websites").
Select(`authorised_websites.id,
strftime('%s', authorised_websites.created) * 1000 as "createdAt",
authorised_websites.expired_at as "expiredAt",
authorised_websites.user_id as "userId", users.nickname as "userName",
authorised_websites.department_id as "departmentId", departments.name as "departmentName",
authorised_websites.website_id as "websiteId", websites.name as "websiteName",
authorised_websites.strategy_id as "strategyId", strategies.name as "strategyName"`).
Joins("left join users on users.id = authorised_websites.user_id").
Joins("left join departments on departments.id = authorised_websites.department_id").
Joins("left join websites on websites.id = authorised_websites.website_id").
Joins("left join strategies on strategies.id = authorised_websites.strategy_id")
dbCounter := r.GetDB(c).Model(&model.AuthorisedWebsite{})
if userId != "" {
db = db.Where("authorised_websites.user_id = ?", userId)
dbCounter = dbCounter.Where("user_id = ?", userId)
}
if departmentId != "" {
db = db.Where("authorised_websites.department_id = ?", departmentId)
dbCounter = dbCounter.Where("department_id = ?", departmentId)
}
if websiteId != "" {
db = db.Where("authorised_websites.website_id = ?", websiteId)
dbCounter = dbCounter.Where("website_id = ?", websiteId)
}
err = dbCounter.Count(&total).Error
if err != nil {
return
}
err = db.Order("authorised_websites.created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
return
}