Rainbond/db/mysql/dao/application.go

120 lines
3.5 KiB
Go
Raw Normal View History

2020-09-17 15:45:46 +08:00
package dao
import (
2020-09-20 01:09:29 +08:00
"github.com/goodrain/rainbond/api/util/bcode"
2020-09-17 15:45:46 +08:00
"github.com/goodrain/rainbond/db/model"
"github.com/jinzhu/gorm"
2021-07-23 08:27:00 +08:00
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
2020-09-17 15:45:46 +08:00
)
2020-09-23 15:30:31 +08:00
// ApplicationDaoImpl -
type ApplicationDaoImpl struct {
2020-09-17 15:45:46 +08:00
DB *gorm.DB
}
//AddModel -
2020-09-23 15:30:31 +08:00
func (a *ApplicationDaoImpl) AddModel(mo model.Interface) error {
2020-09-17 16:43:53 +08:00
appReq, _ := mo.(*model.Application)
2020-09-17 15:45:46 +08:00
var oldApp model.Application
2020-09-17 17:49:29 +08:00
if err := a.DB.Where("tenant_id = ? AND app_id = ?", appReq.TenantID, appReq.AppID).Find(&oldApp).Error; err != nil {
2020-09-17 15:45:46 +08:00
if err == gorm.ErrRecordNotFound {
return a.DB.Create(appReq).Error
}
return err
}
2020-09-20 01:09:29 +08:00
return bcode.ErrApplicationExist
2020-09-17 15:45:46 +08:00
}
//UpdateModel -
2020-09-23 15:30:31 +08:00
func (a *ApplicationDaoImpl) UpdateModel(mo model.Interface) error {
2020-09-18 11:39:05 +08:00
updateReq := mo.(*model.Application)
2020-09-19 20:11:52 +08:00
return a.DB.Save(updateReq).Error
2020-09-17 15:45:46 +08:00
}
2020-09-17 16:43:53 +08:00
// ListApps -
2020-09-23 15:30:31 +08:00
func (a *ApplicationDaoImpl) ListApps(tenantID, appName string, page, pageSize int) ([]*model.Application, int64, error) {
2020-09-17 16:43:53 +08:00
var datas []*model.Application
offset := (page - 1) * pageSize
2020-09-17 17:49:29 +08:00
db := a.DB.Where("tenant_id=?", tenantID).Order("create_time desc")
2020-09-20 02:07:10 +08:00
if appName != "" {
db = db.Where("app_name like ?", "%"+appName+"%")
}
2020-09-17 16:43:53 +08:00
var total int64
if err := db.Model(&model.Application{}).Count(&total).Error; err != nil {
return nil, 0, err
}
if err := db.Limit(pageSize).Offset(offset).Find(&datas).Error; err != nil {
return nil, 0, err
}
return datas, total, nil
}
2020-09-18 11:26:01 +08:00
// GetAppByID -
2020-09-23 15:30:31 +08:00
func (a *ApplicationDaoImpl) GetAppByID(appID string) (*model.Application, error) {
2020-09-18 11:26:01 +08:00
var app model.Application
if err := a.DB.Where("app_id=?", appID).Find(&app).Error; err != nil {
2020-09-20 01:09:29 +08:00
if err == gorm.ErrRecordNotFound {
return nil, bcode.ErrApplicationNotFound
}
2020-09-18 11:26:01 +08:00
return nil, err
}
return &app, nil
}
2020-09-18 18:08:32 +08:00
2020-10-07 22:00:55 +08:00
// GetByServiceID -
func (a *ApplicationDaoImpl) GetByServiceID(sid string) (*model.Application, error) {
var app model.Application
2020-10-07 22:44:50 +08:00
if err := a.DB.Where("app_id = ?", a.DB.Table("tenant_services").Select("app_id").Where("service_id=?", sid).SubQuery()).Find(&app).Error; err != nil {
2020-10-07 22:00:55 +08:00
if err == gorm.ErrRecordNotFound {
return nil, bcode.ErrApplicationNotFound
}
return nil, err
}
return &app, nil
}
2020-09-18 18:08:32 +08:00
// DeleteApp Delete application By appID -
2020-09-23 15:30:31 +08:00
func (a *ApplicationDaoImpl) DeleteApp(appID string) error {
2020-09-18 18:08:32 +08:00
var app model.Application
if err := a.DB.Where("app_id=?", appID).Find(&app).Error; err != nil {
2020-09-20 01:09:29 +08:00
if err == gorm.ErrRecordNotFound {
return bcode.ErrApplicationNotFound
}
2020-09-18 18:08:32 +08:00
return err
}
return a.DB.Delete(&app).Error
}
2021-07-22 16:01:08 +08:00
// ListByAppIDs -
func (a *ApplicationDaoImpl) ListByAppIDs(appIDs []string) ([]*model.Application, error) {
var datas []*model.Application
if err := a.DB.Where("app_id in (?)", appIDs).Find(&datas).Error; err != nil {
2021-07-23 08:27:00 +08:00
return nil, errors.Wrap(err, "list app by app_ids")
2021-07-22 16:01:08 +08:00
}
return datas, nil
}
// IsK8sAppDuplicate Verify whether the k8s app under the same team are duplicate
func (a *ApplicationDaoImpl) IsK8sAppDuplicate(tenantID, AppID, k8sApp string) bool {
var count int64
if err := a.DB.Model(&model.Application{}).Where("tenant_id=? and app_id <>? and k8s_app=?", tenantID, AppID, k8sApp).Count(&count).Error; err != nil {
logrus.Errorf("judge K8s App Duplicate failed %v", err)
return true
}
return count > 0
}
2022-07-19 16:58:16 +08:00
//GetAppByName -
func (a *ApplicationDaoImpl) GetAppByName(tenantID, appName, k8sAppName string) (*model.Application, error) {
var app model.Application
if err := a.DB.Where("tenant_id=? and app_name=? and k8s_app=?", tenantID, appName, k8sAppName).Find(&app).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return nil, bcode.ErrApplicationNotFound
}
return nil, err
}
return &app, nil
}