2020-09-17 15:45:46 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-09-17 16:43:53 +08:00
|
|
|
"strconv"
|
2020-09-17 15:45:46 +08:00
|
|
|
|
2020-09-17 16:43:53 +08:00
|
|
|
"github.com/go-chi/chi"
|
2020-09-17 15:45:46 +08:00
|
|
|
"github.com/goodrain/rainbond/api/handler"
|
|
|
|
"github.com/goodrain/rainbond/api/middleware"
|
|
|
|
"github.com/goodrain/rainbond/api/model"
|
|
|
|
dbmodel "github.com/goodrain/rainbond/db/model"
|
|
|
|
httputil "github.com/goodrain/rainbond/util/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TenantAppStruct -
|
|
|
|
type TenantAppStruct struct{}
|
|
|
|
|
|
|
|
// CreateApp -
|
|
|
|
func (a *TenantAppStruct) CreateApp(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var tenantReq model.Application
|
|
|
|
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &tenantReq, nil) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// get current tenant
|
|
|
|
tenant := r.Context().Value(middleware.ContextKey("tenant")).(*dbmodel.Tenants)
|
|
|
|
tenantReq.TenantID = tenant.UUID
|
|
|
|
|
|
|
|
// create app
|
|
|
|
app, err := handler.GetTenantApplicationHandler().CreateApp(&tenantReq)
|
2020-09-18 11:39:05 +08:00
|
|
|
if err != nil {
|
2020-09-20 01:09:29 +08:00
|
|
|
httputil.ReturnBcodeError(r, w, err)
|
2020-09-18 11:39:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httputil.ReturnSuccess(r, w, app)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateApp -
|
|
|
|
func (a *TenantAppStruct) UpdateApp(w http.ResponseWriter, r *http.Request) {
|
2020-09-19 20:11:52 +08:00
|
|
|
var updateAppReq model.UpdateAppRequest
|
2020-09-18 11:39:05 +08:00
|
|
|
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &updateAppReq, nil) {
|
|
|
|
return
|
|
|
|
}
|
2020-09-19 20:11:52 +08:00
|
|
|
app := r.Context().Value(middleware.ContextKey("application")).(*dbmodel.Application)
|
2020-09-18 11:39:05 +08:00
|
|
|
|
2020-09-19 20:11:52 +08:00
|
|
|
// update app
|
|
|
|
app, err := handler.GetTenantApplicationHandler().UpdateApp(app, updateAppReq)
|
2020-09-17 15:45:46 +08:00
|
|
|
if err != nil {
|
2020-09-20 01:09:29 +08:00
|
|
|
httputil.ReturnBcodeError(r, w, err)
|
2020-09-17 15:45:46 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httputil.ReturnSuccess(r, w, app)
|
|
|
|
}
|
2020-09-17 16:43:53 +08:00
|
|
|
|
|
|
|
// ListApps -
|
|
|
|
func (a *TenantAppStruct) ListApps(w http.ResponseWriter, r *http.Request) {
|
2020-09-20 02:07:10 +08:00
|
|
|
query := r.URL.Query()
|
|
|
|
appName := query.Get("app_name")
|
|
|
|
pageQuery := query.Get("page")
|
|
|
|
pageSizeQuery := query.Get("pageSize")
|
|
|
|
|
|
|
|
page, _ := strconv.Atoi(pageQuery)
|
2020-09-17 16:43:53 +08:00
|
|
|
if page == 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
2020-09-20 02:07:10 +08:00
|
|
|
pageSize, _ := strconv.Atoi(pageSizeQuery)
|
2020-09-17 16:43:53 +08:00
|
|
|
if pageSize == 0 {
|
|
|
|
pageSize = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
// get current tenantID
|
2020-09-19 21:17:40 +08:00
|
|
|
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
|
2020-09-17 16:43:53 +08:00
|
|
|
|
|
|
|
// List apps
|
2020-09-20 02:07:10 +08:00
|
|
|
resp, err := handler.GetTenantApplicationHandler().ListApps(tenantID, appName, page, pageSize)
|
2020-09-17 16:43:53 +08:00
|
|
|
if err != nil {
|
2020-09-21 09:52:56 +08:00
|
|
|
httputil.ReturnBcodeError(r, w, err)
|
2020-09-17 16:43:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httputil.ReturnSuccess(r, w, resp)
|
|
|
|
}
|
2020-09-18 17:02:28 +08:00
|
|
|
|
|
|
|
// ListServices -
|
|
|
|
func (a *TenantAppStruct) ListServices(w http.ResponseWriter, r *http.Request) {
|
|
|
|
appID := chi.URLParam(r, "app_id")
|
2020-09-20 02:07:10 +08:00
|
|
|
query := r.URL.Query()
|
|
|
|
pageQuery := query.Get("page")
|
|
|
|
pageSizeQuery := query.Get("pageSize")
|
|
|
|
|
|
|
|
page, _ := strconv.Atoi(pageQuery)
|
2020-09-18 17:02:28 +08:00
|
|
|
if page == 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
2020-09-20 02:07:10 +08:00
|
|
|
pageSize, _ := strconv.Atoi(pageSizeQuery)
|
2020-09-18 17:02:28 +08:00
|
|
|
if pageSize == 0 {
|
|
|
|
pageSize = 10
|
|
|
|
}
|
|
|
|
|
2020-09-20 01:09:29 +08:00
|
|
|
// List services
|
2020-09-19 22:06:53 +08:00
|
|
|
resp, err := handler.GetServiceManager().GetServicesByAppID(appID, page, pageSize)
|
2020-09-18 17:02:28 +08:00
|
|
|
if err != nil {
|
2020-09-21 09:52:56 +08:00
|
|
|
httputil.ReturnBcodeError(r, w, err)
|
2020-09-18 17:02:28 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httputil.ReturnSuccess(r, w, resp)
|
|
|
|
}
|
2020-09-18 18:08:32 +08:00
|
|
|
|
|
|
|
// DeleteApp -
|
|
|
|
func (a *TenantAppStruct) DeleteApp(w http.ResponseWriter, r *http.Request) {
|
|
|
|
appID := chi.URLParam(r, "app_id")
|
|
|
|
|
|
|
|
// Delete application
|
2020-09-19 22:06:53 +08:00
|
|
|
err := handler.GetTenantApplicationHandler().DeleteApp(appID)
|
2020-09-18 18:08:32 +08:00
|
|
|
if err != nil {
|
2020-09-20 01:09:29 +08:00
|
|
|
httputil.ReturnBcodeError(r, w, err)
|
2020-09-18 18:08:32 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
httputil.ReturnSuccess(r, w, nil)
|
|
|
|
}
|