Rainbond/api/controller/tenant_application.go

112 lines
3.0 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"
2020-09-18 13:16:20 +08:00
"github.com/jinzhu/gorm"
2020-09-17 15:45:46 +08:00
)
// TenantAppStruct -
type TenantAppStruct struct{}
2020-09-17 16:43:53 +08:00
// ListAppResponse -
type ListAppResponse struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
Total int64 `json:"total"`
Apps []*dbmodel.Application `json:"apps"`
}
2020-09-17 15:45:46 +08:00
// 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 {
httputil.ReturnError(r, w, http.StatusInternalServerError, fmt.Sprintf("Create app failed : %v", err))
return
}
httputil.ReturnSuccess(r, w, app)
}
// UpdateApp -
func (a *TenantAppStruct) UpdateApp(w http.ResponseWriter, r *http.Request) {
var updateAppReq model.Application
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &updateAppReq, nil) {
return
}
2020-09-18 13:16:20 +08:00
appID := chi.URLParam(r, "app_id")
tenantApp, err := handler.GetTenantApplicationHandler().GetAppByID(appID)
if err != nil {
if err.Error() == gorm.ErrRecordNotFound.Error() {
httputil.ReturnError(r, w, 404, "can't find application")
return
}
httputil.ReturnError(r, w, 500, "get assign tenant application failed")
return
}
2020-09-18 11:39:05 +08:00
// get current tenant
tenant := r.Context().Value(middleware.ContextKey("tenant")).(*dbmodel.Tenants)
updateAppReq.TenantID = tenant.UUID
2020-09-18 13:16:20 +08:00
updateAppReq.AppID = tenantApp.AppID
2020-09-18 11:39:05 +08:00
// create app
app, err := handler.GetTenantApplicationHandler().UpdateApp(&updateAppReq)
2020-09-17 15:45:46 +08:00
if err != nil {
httputil.ReturnError(r, w, http.StatusInternalServerError, fmt.Sprintf("Create app failed : %v", err))
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) {
var resp ListAppResponse
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
tenant := r.Context().Value(middleware.ContextKey("tenant")).(*dbmodel.Tenants)
// List apps
apps, total, err := handler.GetTenantApplicationHandler().ListApps(tenant.UUID, page, pageSize)
if err != nil {
httputil.ReturnError(r, w, http.StatusInternalServerError, fmt.Sprintf("List apps failure : %v", err))
return
}
if apps != nil {
resp.Apps = apps
} else {
resp.Apps = make([]*dbmodel.Application, 0)
}
resp.Page = page
resp.Total = total
resp.PageSize = pageSize
httputil.ReturnSuccess(r, w, resp)
}