JCS-pub/client/internal/http/user_space.go

87 lines
2.4 KiB
Go

package http
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"gitlink.org.cn/cloudream/common/consts/errorcode"
"gitlink.org.cn/cloudream/common/pkgs/logger"
cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api"
)
type UserSpaceService struct {
*Server
}
func (s *Server) UserSpace() *UserSpaceService {
return &UserSpaceService{
Server: s,
}
}
func (s *UserSpaceService) LoadPackage(ctx *gin.Context) {
log := logger.WithField("HTTP", "UserSpace.LoadPackage")
var req cliapi.UserSpaceLoadPackageReq
if err := ctx.ShouldBindJSON(&req); err != nil {
log.Warnf("binding body: %s", err.Error())
ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
return
}
err := s.svc.UserSpaceSvc().LoadPackage(req.PackageID, req.UserSpaceID, req.RootPath)
if err != nil {
log.Warnf("loading package: %s", err.Error())
ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "loading package failed"))
return
}
ctx.JSON(http.StatusOK, OK(cliapi.UserSpaceLoadPackageResp{}))
}
func (s *UserSpaceService) CreatePackage(ctx *gin.Context) {
log := logger.WithField("HTTP", "UserSpace.CreatePackage")
var req cliapi.UserSpaceCreatePackageReq
if err := ctx.ShouldBindJSON(&req); err != nil {
log.Warnf("binding body: %s", err.Error())
ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
return
}
pkg, err := s.svc.UserSpaceSvc().UserSpaceCreatePackage(
req.BucketID, req.Name, req.UserSpaceID, req.Path, req.SpaceAffinity)
if err != nil {
log.Warnf("userspace create package: %s", err.Error())
ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("userspace create package: %v", err)))
return
}
ctx.JSON(http.StatusOK, OK(cliapi.UserSpaceCreatePackageResp{
Package: pkg,
}))
}
func (s *UserSpaceService) Get(ctx *gin.Context) {
log := logger.WithField("HTTP", "UserSpace.Get")
var req cliapi.UserSpaceGet
if err := ctx.ShouldBindQuery(&req); err != nil {
log.Warnf("binding query: %s", err.Error())
ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
return
}
info, err := s.svc.UserSpaceSvc().Get(req.UserSpaceID)
if err != nil {
log.Warnf("getting info: %s", err.Error())
ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get userspace inf failed"))
return
}
ctx.JSON(http.StatusOK, OK(cliapi.UserSpaceGetResp{
UserSpace: info,
}))
}