2020-09-17 15:45:46 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/goodrain/rainbond/api/model"
|
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"
|
|
|
|
dbmodel "github.com/goodrain/rainbond/db/model"
|
2020-09-18 10:31:35 +08:00
|
|
|
"github.com/goodrain/rainbond/util"
|
2020-09-17 15:45:46 +08:00
|
|
|
)
|
|
|
|
|
2020-09-21 13:58:08 +08:00
|
|
|
// ApplicationAction -
|
|
|
|
type ApplicationAction struct{}
|
2020-09-17 15:45:46 +08:00
|
|
|
|
2020-09-21 13:58:08 +08:00
|
|
|
// ApplicationHandler defines handler methods to TenantApplication.
|
|
|
|
type ApplicationHandler interface {
|
2020-09-17 15:45:46 +08:00
|
|
|
CreateApp(req *model.Application) (*model.Application, error)
|
2020-09-19 20:11:52 +08:00
|
|
|
UpdateApp(srcApp *dbmodel.Application, req model.UpdateAppRequest) (*dbmodel.Application, error)
|
2020-09-20 02:07:10 +08:00
|
|
|
ListApps(tenantID, appName string, page, pageSize int) (*model.ListAppResponse, error)
|
2020-09-18 11:26:01 +08:00
|
|
|
GetAppByID(appID string) (*dbmodel.Application, error)
|
2020-09-18 18:08:32 +08:00
|
|
|
DeleteApp(appID string) error
|
2020-09-22 15:51:57 +08:00
|
|
|
AddConfigGroup(appID string, req *model.ApplicationConfigGroup) (*model.ApplicationConfigGroupResp, error)
|
2020-09-24 14:30:34 +08:00
|
|
|
UpdateConfigGroup(appID, configGroupName string, req *model.UpdateAppConfigGroupReq) (*model.ApplicationConfigGroupResp, error)
|
|
|
|
DeleteConfigGroup(appID, configGroupName string) error
|
|
|
|
ListConfigGroups(appID string, page, pageSize int) (*model.ListApplicationConfigGroupResp, error)
|
2020-09-17 15:45:46 +08:00
|
|
|
}
|
|
|
|
|
2020-09-21 13:58:08 +08:00
|
|
|
// NewApplicationHandler creates a new Tenant Application Handler.
|
|
|
|
func NewApplicationHandler() ApplicationHandler {
|
|
|
|
return &ApplicationAction{}
|
2020-09-17 15:45:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateApp -
|
2020-09-21 13:58:08 +08:00
|
|
|
func (a *ApplicationAction) CreateApp(req *model.Application) (*model.Application, error) {
|
2020-09-17 15:45:46 +08:00
|
|
|
appReq := &dbmodel.Application{
|
|
|
|
AppName: req.AppName,
|
2020-09-18 10:31:35 +08:00
|
|
|
AppID: util.NewUUID(),
|
2020-09-17 15:45:46 +08:00
|
|
|
TenantID: req.TenantID,
|
|
|
|
}
|
2020-09-18 14:05:48 +08:00
|
|
|
req.AppID = appReq.AppID
|
2020-09-23 15:30:31 +08:00
|
|
|
if err := db.GetManager().ApplicationDao().AddModel(appReq); err != nil {
|
2020-09-17 15:45:46 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return req, nil
|
|
|
|
}
|
2020-09-17 16:43:53 +08:00
|
|
|
|
2020-09-18 11:39:05 +08:00
|
|
|
// UpdateApp -
|
2020-09-21 13:58:08 +08:00
|
|
|
func (a *ApplicationAction) UpdateApp(srcApp *dbmodel.Application, req model.UpdateAppRequest) (*dbmodel.Application, error) {
|
2020-09-19 20:11:52 +08:00
|
|
|
srcApp.AppName = req.AppName
|
2020-09-23 15:30:31 +08:00
|
|
|
if err := db.GetManager().ApplicationDao().UpdateModel(srcApp); err != nil {
|
2020-09-18 11:39:05 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-19 20:11:52 +08:00
|
|
|
return srcApp, nil
|
2020-09-18 11:39:05 +08:00
|
|
|
}
|
|
|
|
|
2020-09-17 16:43:53 +08:00
|
|
|
// ListApps -
|
2020-09-21 13:58:08 +08:00
|
|
|
func (a *ApplicationAction) ListApps(tenantID, appName string, page, pageSize int) (*model.ListAppResponse, error) {
|
2020-09-19 21:17:40 +08:00
|
|
|
var resp model.ListAppResponse
|
2020-09-23 15:30:31 +08:00
|
|
|
apps, total, err := db.GetManager().ApplicationDao().ListApps(tenantID, appName, page, pageSize)
|
2020-09-19 21:17:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-17 16:43:53 +08:00
|
|
|
}
|
2020-09-19 21:17:40 +08:00
|
|
|
if apps != nil {
|
|
|
|
resp.Apps = apps
|
|
|
|
} else {
|
|
|
|
resp.Apps = make([]*dbmodel.Application, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Page = page
|
|
|
|
resp.Total = total
|
|
|
|
resp.PageSize = pageSize
|
|
|
|
return &resp, nil
|
2020-09-17 16:43:53 +08:00
|
|
|
}
|
2020-09-18 11:26:01 +08:00
|
|
|
|
|
|
|
// GetAppByID -
|
2020-09-21 13:58:08 +08:00
|
|
|
func (a *ApplicationAction) GetAppByID(appID string) (*dbmodel.Application, error) {
|
2020-09-23 15:30:31 +08:00
|
|
|
app, err := db.GetManager().ApplicationDao().GetAppByID(appID)
|
2020-09-18 11:26:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return app, nil
|
|
|
|
}
|
2020-09-18 18:08:32 +08:00
|
|
|
|
|
|
|
// DeleteApp -
|
2020-09-21 13:58:08 +08:00
|
|
|
func (a *ApplicationAction) DeleteApp(appID string) error {
|
2020-09-19 22:06:53 +08:00
|
|
|
// Get the number of services under the application
|
|
|
|
total, err := db.GetManager().TenantServiceDao().CountServiceByAppID(appID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if total != 0 {
|
2020-09-20 01:09:29 +08:00
|
|
|
return bcode.ErrDeleteDueToBindService
|
2020-09-19 22:06:53 +08:00
|
|
|
}
|
2020-09-23 15:30:31 +08:00
|
|
|
return db.GetManager().ApplicationDao().DeleteApp(appID)
|
2020-09-18 18:08:32 +08:00
|
|
|
}
|