81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package http
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gitlink.org.cn/cloudream/common/consts/errorcode"
|
|
"gitlink.org.cn/cloudream/common/models"
|
|
"gitlink.org.cn/cloudream/common/pkgs/logger"
|
|
)
|
|
|
|
type JobSetService struct {
|
|
*Server
|
|
}
|
|
|
|
func (s *Server) JobSetSvc() *JobSetService {
|
|
return &JobSetService{
|
|
Server: s,
|
|
}
|
|
}
|
|
|
|
type JobSetSubmitResp struct {
|
|
JobSetID string `json:"jobSetID"`
|
|
}
|
|
|
|
func (s *JobSetService) Submit(ctx *gin.Context) {
|
|
log := logger.WithField("HTTP", "JobSet.Submit")
|
|
|
|
bodyData, err := io.ReadAll(ctx.Request.Body)
|
|
if err != nil {
|
|
log.Warnf("reading request body: %s", err.Error())
|
|
ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "read request body failed"))
|
|
return
|
|
}
|
|
|
|
jobSetInfo, err := models.JobSetInfoFromJSON(bodyData)
|
|
if err != nil {
|
|
log.Warnf("parsing request body: %s", err.Error())
|
|
ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "parse request body failed"))
|
|
return
|
|
}
|
|
|
|
jobSetID, err := s.svc.JobSetSvc().Submit(*jobSetInfo)
|
|
if err != nil {
|
|
log.Warnf("submitting jobset: %s", err.Error())
|
|
ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "submit jobset failed"))
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, OK(JobSetSubmitResp{
|
|
JobSetID: jobSetID,
|
|
}))
|
|
}
|
|
|
|
type JobSetSetLocalFileReq struct {
|
|
JobSetID string `json:"jobSetID" binding:"required"`
|
|
LocalPath string `json:"localPath" binding:"required"`
|
|
PackageID *int64 `json:"packageID" binding:"required"`
|
|
}
|
|
|
|
func (s *JobSetService) SetLocalFile(ctx *gin.Context) {
|
|
log := logger.WithField("HTTP", "JobSet.SetLocalFile")
|
|
|
|
var req JobSetSetLocalFileReq
|
|
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.JobSetSvc().SetLocalFile(req.JobSetID, req.LocalPath, *req.PackageID)
|
|
if err != nil {
|
|
log.Warnf("setting local file: %s", err.Error())
|
|
ctx.JSON(http.StatusBadRequest, Failed(errorcode.OperationFailed, "set local file failed"))
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, OK(nil))
|
|
}
|