Rainbond/api/handler/tenant_application_handler.go

82 lines
2.4 KiB
Go
Raw Normal View History

2020-09-17 15:45:46 +08:00
package handler
import (
"github.com/goodrain/rainbond/api/model"
"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
)
// TenantApplicationAction -
type TenantApplicationAction struct{}
// TenantApplicationHandler defines handler methods to TenantApplication.
type TenantApplicationHandler interface {
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-19 21:17:40 +08:00
ListApps(tenantID 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-17 15:45:46 +08:00
}
// NewTenantApplicationHandler creates a new Tenant Application Handler.
func NewTenantApplicationHandler() TenantApplicationHandler {
return &TenantApplicationAction{}
}
// CreateApp -
func (a *TenantApplicationAction) CreateApp(req *model.Application) (*model.Application, error) {
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-17 15:45:46 +08:00
if err := db.GetManager().TenantApplicationDao().AddModel(appReq); err != nil {
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-19 20:11:52 +08:00
func (a *TenantApplicationAction) UpdateApp(srcApp *dbmodel.Application, req model.UpdateAppRequest) (*dbmodel.Application, error) {
srcApp.AppName = req.AppName
if err := db.GetManager().TenantApplicationDao().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-19 21:17:40 +08:00
func (a *TenantApplicationAction) ListApps(tenantID string, page, pageSize int) (*model.ListAppResponse, error) {
var resp model.ListAppResponse
2020-09-17 16:43:53 +08:00
apps, total, err := db.GetManager().TenantApplicationDao().ListApps(tenantID, 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 -
func (a *TenantApplicationAction) GetAppByID(appID string) (*dbmodel.Application, error) {
app, err := db.GetManager().TenantApplicationDao().GetAppByID(appID)
if err != nil {
return nil, err
}
return app, nil
}
2020-09-18 18:08:32 +08:00
// DeleteApp -
func (a *TenantApplicationAction) DeleteApp(appID string) error {
return db.GetManager().TenantApplicationDao().DeleteApp(appID)
2020-09-18 18:08:32 +08:00
}