2024-12-12 11:45:03 +08:00
|
|
|
package moto
|
|
|
|
|
2024-12-12 16:24:50 +08:00
|
|
|
import (
|
|
|
|
"git.nobla.cn/golang/kos"
|
|
|
|
"git.nobla.cn/golang/kos/entry/http"
|
2024-12-12 17:39:04 +08:00
|
|
|
"git.nobla.cn/golang/moto/common/db"
|
|
|
|
"git.nobla.cn/golang/moto/internal/organize"
|
|
|
|
"git.nobla.cn/golang/moto/internal/organize/passport"
|
|
|
|
"git.nobla.cn/golang/moto/internal/organize/types"
|
|
|
|
"git.nobla.cn/golang/moto/version"
|
|
|
|
"git.nobla.cn/golang/rest"
|
|
|
|
restTypes "git.nobla.cn/golang/rest/types"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"strconv"
|
2024-12-12 16:24:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (svr *Server) handleLogin(ctx *http.Context) (err error) {
|
|
|
|
var (
|
|
|
|
tk *types.Tokenize
|
|
|
|
req *passport.LoginRequest
|
|
|
|
)
|
|
|
|
req = &passport.LoginRequest{}
|
|
|
|
if err = ctx.Bind(req); err != nil {
|
|
|
|
return ctx.Error(http.ErrInvalidPayload, err.Error())
|
|
|
|
}
|
|
|
|
if tk, err = passport.Login(ctx.Context(), req); err != nil {
|
|
|
|
return ctx.Error(http.ErrPermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
return ctx.Success(tk)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svr *Server) handleLogout(ctx *http.Context) (err error) {
|
|
|
|
passport.Logout(ctx.Context(), ctx.User().Get("token"))
|
|
|
|
return ctx.Success("logout")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svr *Server) handleProfile(ctx *http.Context) (err error) {
|
|
|
|
var (
|
|
|
|
profile *types.Profile
|
|
|
|
)
|
2024-12-12 17:39:04 +08:00
|
|
|
if profile, err = organize.Profile(ctx.Context(), ctx.User().ID); err != nil {
|
2024-12-12 16:24:50 +08:00
|
|
|
return ctx.Error(http.ErrTemporaryUnavailable, err.Error())
|
|
|
|
}
|
|
|
|
return ctx.Success(profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svr *Server) handleGetConfigure(ctx *http.Context) (err error) {
|
|
|
|
return ctx.Success(map[string]string{})
|
|
|
|
}
|
|
|
|
|
2024-12-12 17:39:04 +08:00
|
|
|
func (svr *Server) handleListSchema(ctx *http.Context) (err error) {
|
|
|
|
var (
|
|
|
|
schemas []*restTypes.Schema
|
|
|
|
)
|
|
|
|
scenario := ctx.Query("scenario")
|
|
|
|
if scenario == "" {
|
|
|
|
schemas, err = rest.GetSchemas(
|
|
|
|
ctx.Request().Context(),
|
|
|
|
db.WithContext(ctx.Request().Context()),
|
|
|
|
ctx.User().Get("domain"),
|
|
|
|
version.ProductName,
|
|
|
|
ctx.Param("table"),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
schemas, err = rest.VisibleSchemas(
|
|
|
|
ctx.Request().Context(),
|
|
|
|
db.WithContext(ctx.Request().Context()),
|
|
|
|
ctx.User().Get("domain"),
|
|
|
|
version.ProductName,
|
|
|
|
ctx.Param("table"),
|
|
|
|
scenario,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Error(http.ErrResourceNotFound, err.Error())
|
|
|
|
} else {
|
|
|
|
return ctx.Success(schemas)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svr *Server) handleSaveSchema(ctx *http.Context) (err error) {
|
|
|
|
schemas := make([]*restTypes.Schema, 0)
|
|
|
|
if err = ctx.Bind(&schemas); err != nil {
|
|
|
|
return ctx.Error(http.ErrInvalidPayload, err.Error())
|
|
|
|
}
|
|
|
|
domainName := ctx.User().Get("domain")
|
|
|
|
for i, _ := range schemas {
|
|
|
|
schemas[i].Domain = domainName
|
|
|
|
}
|
|
|
|
if err = db.WithContext(ctx.Request().Context()).Transaction(func(tx *gorm.DB) (errTx error) {
|
|
|
|
for _, scm := range schemas {
|
|
|
|
if errTx = tx.Save(scm).Error; errTx != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}); err == nil {
|
|
|
|
return ctx.Success(map[string]interface{}{
|
|
|
|
"count": len(schemas),
|
|
|
|
"state": "success",
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return ctx.Error(http.ErrTemporaryUnavailable, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svr *Server) handleDeleteSchema(ctx *http.Context) (err error) {
|
|
|
|
id, _ := strconv.Atoi(ctx.Param("id"))
|
|
|
|
model := &restTypes.Schema{Id: uint64(id)}
|
|
|
|
if err = db.WithContext(ctx.Request().Context()).Delete(model).Error; err == nil {
|
|
|
|
return ctx.Success(map[string]any{
|
|
|
|
"id": id,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return ctx.Error(http.ErrResourceDelete, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-12 11:45:03 +08:00
|
|
|
func (svr *Server) routes() {
|
2024-12-12 17:39:04 +08:00
|
|
|
kos.Http().Use(organize.AuthMiddleware)
|
|
|
|
organize.AllowUri("/passport/login")
|
2024-12-12 11:45:03 +08:00
|
|
|
|
2024-12-12 16:24:50 +08:00
|
|
|
kos.Http().Handle(http.MethodPost, "/passport/login", svr.handleLogin)
|
|
|
|
kos.Http().Handle(http.MethodDelete, "/passport/logout", svr.handleLogout)
|
|
|
|
kos.Http().Handle(http.MethodGet, "/user/profile", svr.handleProfile)
|
|
|
|
kos.Http().Handle(http.MethodGet, "/user/configures", svr.handleGetConfigure)
|
2024-12-12 17:39:04 +08:00
|
|
|
|
|
|
|
kos.Http().Handle(http.MethodGet, "/rest/schema/:module/:table", svr.handleListSchema)
|
|
|
|
kos.Http().Handle(http.MethodPut, "/rest/schema/:module/:table", svr.handleSaveSchema)
|
|
|
|
kos.Http().Handle(http.MethodDelete, "/rest/schema/:id", svr.handleDeleteSchema)
|
|
|
|
|
2024-12-12 11:45:03 +08:00
|
|
|
}
|