Rainbond/api/controller/tenant_application.go

116 lines
2.9 KiB
Go
Raw Normal View History

2020-09-17 15:45:46 +08:00
package controller
import (
"fmt"
"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) {
page, _ := strconv.Atoi(chi.URLParam(r, "page"))
if page == 0 {
page = 1
}
pageSize, _ := strconv.Atoi(chi.URLParam(r, "pageSize"))
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-19 21:17:40 +08:00
resp, err := handler.GetTenantApplicationHandler().ListApps(tenantID, page, pageSize)
2020-09-17 16:43:53 +08:00
if err != nil {
httputil.ReturnError(r, w, http.StatusInternalServerError, fmt.Sprintf("List apps failure : %v", err))
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")
page, _ := strconv.Atoi(chi.URLParam(r, "page"))
if page == 0 {
page = 1
}
pageSize, _ := strconv.Atoi(chi.URLParam(r, "pageSize"))
if pageSize == 0 {
pageSize = 10
}
2020-09-20 01:09:29 +08:00
// List services
resp, err := handler.GetServiceManager().GetServicesByAppID(appID, page, pageSize)
2020-09-18 17:02:28 +08:00
if err != nil {
httputil.ReturnError(r, w, http.StatusInternalServerError, fmt.Sprintf("List apps failure : %v", err))
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
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)
}