programController.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/middleware"
  5. "iot_manager_service/app/multimedia/model"
  6. "iot_manager_service/app/multimedia/service"
  7. "iot_manager_service/util/common"
  8. "math"
  9. "net/http"
  10. "strconv"
  11. )
  12. var Program = new(programCtl)
  13. type programCtl struct{}
  14. func (c *programCtl) Detail(ctx *gin.Context) {
  15. id, e := strconv.Atoi(ctx.Query("id"))
  16. if e != nil {
  17. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  18. return
  19. }
  20. Program, err := service.ProgramService.Get(id)
  21. if err != nil {
  22. ctx.JSON(http.StatusOK, err)
  23. return
  24. }
  25. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, Program))
  26. }
  27. func (c *programCtl) List(ctx *gin.Context) {
  28. searchValue := ctx.Query("searchValue")
  29. current, _ := strconv.Atoi(ctx.Query("current"))
  30. size, _ := strconv.Atoi(ctx.Query("size"))
  31. if current == 0 {
  32. current = 1
  33. }
  34. if size <= 0 || size > 100 {
  35. size = 10
  36. }
  37. value, _ := ctx.Get(middleware.Authorization)
  38. claims := value.(*middleware.Claims)
  39. libraries, err := service.ProgramService.List(claims.TenantId, searchValue, current, size)
  40. if err != nil {
  41. ctx.JSON(http.StatusOK, err)
  42. return
  43. }
  44. pages := math.Ceil(float64(len(libraries)) / float64(size))
  45. rsp := model.RsqProgramList{
  46. Current: current,
  47. Size: size,
  48. Total: len(libraries),
  49. Pages: int(pages),
  50. Records: libraries,
  51. }
  52. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, rsp))
  53. }
  54. func (c *programCtl) Remove(ctx *gin.Context) {
  55. ids := ctx.Query("ids")
  56. id, e := strconv.Atoi(ids)
  57. if e != nil {
  58. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  59. return
  60. }
  61. value, _ := ctx.Get(middleware.Authorization)
  62. claims := value.(*middleware.Claims)
  63. err := service.ProgramService.Remove(claims.UserId, claims.TenantId, id)
  64. if err != nil {
  65. ctx.JSON(http.StatusOK, err)
  66. return
  67. }
  68. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
  69. }
  70. func (c *programCtl) GetList(ctx *gin.Context) {
  71. name := ctx.Query("name")
  72. value, _ := ctx.Get(middleware.Authorization)
  73. claims := value.(*middleware.Claims)
  74. Program, err := service.ProgramService.GetList(claims.TenantId, name)
  75. if err != nil {
  76. ctx.JSON(http.StatusOK, err)
  77. return
  78. }
  79. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, Program))
  80. }
  81. func (c *programCtl) Submit(ctx *gin.Context) {
  82. var req model.ReqProgramSubmit
  83. if err := ctx.ShouldBindJSON(&req); err != nil {
  84. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  85. return
  86. }
  87. value, _ := ctx.Get(middleware.Authorization)
  88. claims := value.(*middleware.Claims)
  89. e := service.ProgramService.Submit(claims.TenantId, claims.UserId, req)
  90. if e != nil {
  91. ctx.JSON(http.StatusOK, e)
  92. return
  93. }
  94. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
  95. }
  96. func (c *programCtl) GetLibraryList(ctx *gin.Context) {
  97. id := ctx.Query("id")
  98. programId, e := strconv.Atoi(id)
  99. if e != nil {
  100. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
  101. return
  102. }
  103. value, _ := ctx.Get(middleware.Authorization)
  104. claims := value.(*middleware.Claims)
  105. libraries, err := service.ProgramService.GetLibraryList(claims.TenantId, programId)
  106. if err != nil {
  107. ctx.JSON(http.StatusOK, e)
  108. return
  109. }
  110. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, libraries))
  111. }