Rainbond/db/mysql/dao/tenant_application.go

71 lines
1.9 KiB
Go
Raw Normal View History

2020-09-17 15:45:46 +08:00
package dao
import (
"github.com/goodrain/rainbond/db/model"
"github.com/jinzhu/gorm"
)
// TenantApplicationDaoImpl -
type TenantApplicationDaoImpl struct {
DB *gorm.DB
}
//AddModel -
func (a *TenantApplicationDaoImpl) 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
}
return nil
}
//UpdateModel -
func (a *TenantApplicationDaoImpl) UpdateModel(mo model.Interface) error {
2020-09-18 11:39:05 +08:00
updateReq := mo.(*model.Application)
var oldApp model.Application
if err := a.DB.Where("tenant_id = ? AND app_id = ?", updateReq.TenantID, updateReq.AppID).Find(&oldApp).Error; err != nil {
return err
}
return a.DB.Model(&oldApp).Update("app_name", updateReq.AppName).Error
2020-09-17 15:45:46 +08:00
}
2020-09-17 16:43:53 +08:00
// ListApps -
func (a *TenantApplicationDaoImpl) ListApps(tenantID string, page, pageSize int) ([]*model.Application, int64, error) {
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-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 -
func (a *TenantApplicationDaoImpl) GetAppByID(appID string) (*model.Application, error) {
var app model.Application
if err := a.DB.Where("app_id=?", appID).Find(&app).Error; err != nil {
return nil, err
}
return &app, nil
}
2020-09-18 18:08:32 +08:00
// DeleteApp Delete application By appID -
func (a *TenantApplicationDaoImpl) DeleteApp(appID string) error {
var app model.Application
if err := a.DB.Where("app_id=?", appID).Find(&app).Error; err != nil {
return err
}
return a.DB.Delete(&app).Error
}