version.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package api
  2. import (
  3. "cr-svc/db/model"
  4. "cr-svc/db/repo"
  5. "cr-svc/log"
  6. "errors"
  7. "net/http"
  8. "github.com/gin-gonic/gin"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/bson/primitive"
  11. )
  12. func Version(r *GinRouter) {
  13. r.GET("/versions", GetVersion)
  14. r.GET("/version/latest", VersionLatest)
  15. r.POSTJWT("/version/update", UpdateVersion)
  16. r.POSTJWT("/version/delete/:id", DeleteArticle)
  17. }
  18. func GetVersion(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  19. page, size, query := UtilQueryPageSize(c)
  20. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  21. CollectName: repo.CollectionVersions,
  22. Page: page,
  23. Size: size,
  24. Query: query,
  25. })
  26. }
  27. func VersionLatest(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  28. out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  29. CollectName: repo.CollectionVersions,
  30. Page: 1,
  31. Size: 1,
  32. Sort: bson.D{bson.E{Key: "createTime", Value: -1}},
  33. })
  34. if err != nil {
  35. return map[string]interface{}{}, nil
  36. }
  37. if len(out.List) < 1 {
  38. return map[string]interface{}{}, nil
  39. }
  40. c.JSON(http.StatusOK, out.List[0])
  41. return nil, nil
  42. }
  43. func UpdateVersion(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  44. var version model.Versions
  45. err := c.ShouldBindJSON(&version)
  46. if err != nil {
  47. log.Error(err)
  48. return nil, err
  49. }
  50. if version.Id.IsZero() {
  51. return nil, errors.New("id错误")
  52. }
  53. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionVersions, version.Id.Hex(), &version)
  54. }
  55. func DeleteArticle(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  56. _id := c.Param("id")
  57. id, _ := primitive.ObjectIDFromHex(_id)
  58. if id.IsZero() {
  59. return nil, errors.New("id错误")
  60. }
  61. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionVersions, _id)
  62. }