122 lines
3.0 KiB
Go
122 lines
3.0 KiB
Go
package apis
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"gitlink.org.cn/JointCloud/pcm-openi/common"
|
|
"gitlink.org.cn/JointCloud/pcm-openi/model"
|
|
"gitlink.org.cn/JointCloud/pcm-openi/service"
|
|
"net/http"
|
|
)
|
|
|
|
type repoApi struct {
|
|
*Api
|
|
*service.RepoService
|
|
}
|
|
|
|
var RepoApi = repoApi{
|
|
Api: BaseApi,
|
|
RepoService: service.NewRepoService(),
|
|
}
|
|
|
|
func GetRepos(ctx *gin.Context) {
|
|
token := ctx.Query(common.ACCESSTOKEN)
|
|
|
|
reqUrl := common.OPENIPREFIX + common.REPO
|
|
|
|
var resp []model.Repo
|
|
|
|
req := common.GetRestyRequest(common.TIMEOUT)
|
|
_, err := req.
|
|
SetQueryParam(common.ACCESSTOKEN, token).
|
|
SetResult(&resp).
|
|
Get(reqUrl)
|
|
|
|
if err != nil {
|
|
model.Response(ctx, 500, common.INVOKEERROR, err)
|
|
return
|
|
}
|
|
|
|
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
|
}
|
|
|
|
func CreateRepo(ctx *gin.Context) {
|
|
var param model.RepoCreateParam
|
|
if err := ctx.BindJSON(¶m); err != nil {
|
|
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
|
return
|
|
}
|
|
|
|
token := ctx.Query(common.ACCESSTOKEN)
|
|
|
|
reqUrl := common.OPENIPREFIX + common.REPO
|
|
|
|
var resp model.Repo
|
|
|
|
req := common.GetRestyRequest(common.TIMEOUT)
|
|
_, err := req.
|
|
SetQueryParam(common.ACCESSTOKEN, token).
|
|
SetBody(¶m).
|
|
SetResult(&resp).
|
|
Post(reqUrl)
|
|
|
|
if err != nil {
|
|
model.Response(ctx, 500, common.INVOKEERROR, err)
|
|
return
|
|
}
|
|
|
|
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
|
}
|
|
|
|
// UploadFile2Repo 上传
|
|
func (r repoApi) UploadFile2Repo(ctx *gin.Context) {
|
|
var param model.RepoUploadFileParam
|
|
if err := ctx.BindJSON(¶m); err != nil {
|
|
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, translate(err))
|
|
return
|
|
}
|
|
|
|
token := ctx.Query(common.ACCESSTOKEN)
|
|
// 调用服务层处理文件上传
|
|
if resp, err := r.RepoService.UploadFile(token, param); err != nil {
|
|
model.Response(ctx, http.StatusInternalServerError, err.Error(), resp)
|
|
} else {
|
|
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
|
}
|
|
}
|
|
|
|
// UpdateFile2Repo 更新项目文件内容
|
|
func (r repoApi) UpdateFile2Repo(ctx *gin.Context) {
|
|
var param model.RepoUpdateFileParam
|
|
if err := ctx.BindJSON(¶m); err != nil {
|
|
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, translate(err))
|
|
return
|
|
}
|
|
|
|
token := ctx.Query(common.ACCESSTOKEN)
|
|
if resp, err := r.RepoService.UpdateFile(token, param); err != nil {
|
|
model.Response(ctx, http.StatusInternalServerError, err.Error(), resp)
|
|
} else {
|
|
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
|
}
|
|
}
|
|
|
|
// QueryRepoFilesContent 查询文件详情
|
|
func (r repoApi) QueryRepoFilesContent(ctx *gin.Context) {
|
|
var param model.QueryFilesContentParam
|
|
if err := ctx.ShouldBind(¶m); err != nil {
|
|
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, translate(err))
|
|
return
|
|
}
|
|
token := ctx.Query(common.ACCESSTOKEN)
|
|
if token == "" {
|
|
model.Response(ctx, 401, "access_token为必填字段", nil)
|
|
return
|
|
}
|
|
if resp, err := r.RepoService.QueryFilesContent(token, ¶m); err != nil {
|
|
model.Response(ctx, 500, err.Error(), nil)
|
|
} else {
|
|
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
|
}
|
|
return
|
|
}
|