123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package api
- import (
- "cr-svc/db/model"
- "cr-svc/db/repo"
- "cr-svc/log"
- "errors"
- "net/http"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- func Version(r *GinRouter) {
- r.GET("/versions", GetVersion)
- r.GET("/version/latest", VersionLatest)
- r.POSTJWT("/version/update", UpdateVersion)
- r.POSTJWT("/version/delete/:id", DeleteArticle)
- }
- func GetVersion(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
- CollectName: repo.CollectionVersions,
- Page: page,
- Size: size,
- Query: query,
- })
- }
- func VersionLatest(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
- CollectName: repo.CollectionVersions,
- Page: 1,
- Size: 1,
- Sort: bson.D{bson.E{Key: "createTime", Value: -1}},
- })
- if err != nil {
- return map[string]interface{}{}, nil
- }
- if len(out.List) < 1 {
- return map[string]interface{}{}, nil
- }
- c.JSON(http.StatusOK, out.List[0])
- return nil, nil
- }
- func UpdateVersion(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var version model.Versions
- err := c.ShouldBindJSON(&version)
- if err != nil {
- log.Error(err)
- return nil, err
- }
- if version.Id.IsZero() {
- return nil, errors.New("id错误")
- }
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionVersions, version.Id.Hex(), &version)
- }
- func DeleteArticle(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- _id := c.Param("id")
- id, _ := primitive.ObjectIDFromHex(_id)
- if id.IsZero() {
- return nil, errors.New("id错误")
- }
- return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionVersions, _id)
- }
|