Rainbond/api/handler/application_handler.go
2020-10-08 14:01:37 +08:00

103 lines
3.2 KiB
Go

package handler
import (
"github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/api/util/bcode"
"github.com/goodrain/rainbond/db"
dbmodel "github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/util"
)
// ApplicationAction -
type ApplicationAction struct{}
// ApplicationHandler defines handler methods to TenantApplication.
type ApplicationHandler interface {
CreateApp(req *model.Application) (*model.Application, error)
UpdateApp(srcApp *dbmodel.Application, req model.UpdateAppRequest) (*dbmodel.Application, error)
ListApps(tenantID, appName string, page, pageSize int) (*model.ListAppResponse, error)
GetAppByID(appID string) (*dbmodel.Application, error)
BatchBindService(appID string,req model.BindServiceRequest)error
DeleteApp(appID string) error
AddConfigGroup(appID string, req *model.ApplicationConfigGroup) (*model.ApplicationConfigGroupResp, error)
UpdateConfigGroup(appID, configGroupName string, req *model.UpdateAppConfigGroupReq) (*model.ApplicationConfigGroupResp, error)
DeleteConfigGroup(appID, configGroupName string) error
ListConfigGroups(appID string, page, pageSize int) (*model.ListApplicationConfigGroupResp, error)
}
// NewApplicationHandler creates a new Tenant Application Handler.
func NewApplicationHandler() ApplicationHandler {
return &ApplicationAction{}
}
// CreateApp -
func (a *ApplicationAction) CreateApp(req *model.Application) (*model.Application, error) {
appReq := &dbmodel.Application{
AppName: req.AppName,
AppID: util.NewUUID(),
TenantID: req.TenantID,
}
req.AppID = appReq.AppID
if err := db.GetManager().ApplicationDao().AddModel(appReq); err != nil {
return nil, err
}
return req, nil
}
// UpdateApp -
func (a *ApplicationAction) UpdateApp(srcApp *dbmodel.Application, req model.UpdateAppRequest) (*dbmodel.Application, error) {
srcApp.AppName = req.AppName
if err := db.GetManager().ApplicationDao().UpdateModel(srcApp); err != nil {
return nil, err
}
return srcApp, nil
}
// ListApps -
func (a *ApplicationAction) ListApps(tenantID, appName string, page, pageSize int) (*model.ListAppResponse, error) {
var resp model.ListAppResponse
apps, total, err := db.GetManager().ApplicationDao().ListApps(tenantID, appName, page, pageSize)
if err != nil {
return nil, err
}
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
}
// GetAppByID -
func (a *ApplicationAction) GetAppByID(appID string) (*dbmodel.Application, error) {
app, err := db.GetManager().ApplicationDao().GetAppByID(appID)
if err != nil {
return nil, err
}
return app, nil
}
// DeleteApp -
func (a *ApplicationAction) DeleteApp(appID string) error {
// Get the number of services under the application
total, err := db.GetManager().TenantServiceDao().CountServiceByAppID(appID)
if err != nil {
return err
}
if total != 0 {
return bcode.ErrDeleteDueToBindService
}
return db.GetManager().ApplicationDao().DeleteApp(appID)
}
//BatchBindService
func (a *ApplicationAction)BatchBindService(appID string,req model.BindServiceRequest)error{
if err := db.GetManager().TenantServiceDao().BindAppByServiceIDs(appID,req.ServiceIDs);err!=nil{
return err
}
return nil
}