authorization.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package mint
  2. import (
  3. "net/http"
  4. "strconv"
  5. "time"
  6. "github.com/gin-gonic/gin"
  7. "github.com/go-pg/pg/v10"
  8. )
  9. type Authorization struct {
  10. Id int `form:"id" json:"id" binding:"required"`
  11. ResourceId int `form:"resource_id" json:"resource_id"`
  12. ResourceType string `form:"resource_type" json:"resource_type"`
  13. UserId string `form:"user_id" json:"user_id"`
  14. UserType string `form:"user_type" json:"user_type"`
  15. ResRight string `form:"res_right" json:"res_right"`
  16. OwnerId int `form:"owner_id" json:"owner_id"`
  17. ExpiredAt time.Time `form:"expired_at" json:"expired_at"`
  18. AcceptedAt time.Time `form:"accepted_at" json:"accepted_at"`
  19. Version int
  20. CreatedAt time.Time
  21. UpdatedAt time.Time
  22. }
  23. //查询display a list of all Authorizations
  24. func AuthorizationsIndex(db *pg.DB) gin.HandlerFunc {
  25. return func(c *gin.Context) {
  26. //按照标题搜索,或者按照authorization列出子课程
  27. resource_id := c.Query("resource_id")
  28. resource_type := c.Query("resource_type")
  29. // TODO 在这里进行db操作
  30. // Select user by primary key.
  31. var authorizations []Authorization
  32. err := db.Model(&authorizations).Column("id", "resource_id", "resource_type", "user_id", "user_type", "res_right", "expired_at", "accepted_at").Where("resource_id = ?", resource_id).Where("resource_type = ?", resource_type).Select()
  33. if err != nil {
  34. panic(err)
  35. }
  36. c.JSON(http.StatusOK, gin.H{
  37. "message": authorizations,
  38. })
  39. }
  40. }
  41. //return an HTML form for creating a new Authorizations
  42. func AuthorizationsNew(db *pg.DB) gin.HandlerFunc {
  43. return func(c *gin.Context) {
  44. //TODO 业务逻辑
  45. c.HTML(http.StatusOK, "authorizations/new.html", gin.H{
  46. "message": "ok",
  47. })
  48. }
  49. }
  50. //新建create a new Authorizations
  51. func AuthorizationsCreate(db *pg.DB) gin.HandlerFunc {
  52. return func(c *gin.Context) {
  53. var form Channel
  54. if err := c.ShouldBindJSON(&form); err != nil {
  55. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  56. return
  57. }
  58. _, err := db.Model(&form).Insert()
  59. if err != nil {
  60. panic(err)
  61. }
  62. c.JSON(http.StatusOK, gin.H{
  63. "status": "sucess",
  64. "data": form,
  65. })
  66. }
  67. }
  68. //查询display a specific Authorizations
  69. func AuthorizationsShow(db *pg.DB) gin.HandlerFunc {
  70. return func(c *gin.Context) {
  71. id, err := strconv.Atoi(c.Param("id"))
  72. if err != nil {
  73. panic(err)
  74. }
  75. authorization := Authorization{Id: id}
  76. err = db.Model(&authorization).WherePK().Select()
  77. if err != nil {
  78. panic(err)
  79. }
  80. c.JSON(http.StatusOK, gin.H{
  81. "status": "sucess",
  82. "data": authorization,
  83. })
  84. }
  85. }
  86. //return an HTML form for edit a Authorizations
  87. func AuthorizationsEdit(db *pg.DB) gin.HandlerFunc {
  88. return func(c *gin.Context) {
  89. //TODO 业务逻辑
  90. c.HTML(http.StatusOK, "authorizations/edit.html", gin.H{
  91. "message": "ok",
  92. })
  93. }
  94. }
  95. //update a specific Authorizations
  96. func AuthorizationsUpdate(db *pg.DB) gin.HandlerFunc {
  97. return func(c *gin.Context) {
  98. var form Authorization
  99. if err := c.ShouldBindJSON(&form); err != nil {
  100. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  101. return
  102. }
  103. _, err := db.Model(&form).Column("res_right").WherePK().Update()
  104. if err != nil {
  105. panic(err)
  106. }
  107. c.JSON(http.StatusOK, gin.H{
  108. "message": "update ok",
  109. })
  110. }
  111. }
  112. //删
  113. //delete a specific Authorizations
  114. func AuthorizationsDestroy(db *pg.DB) gin.HandlerFunc {
  115. return func(c *gin.Context) {
  116. id, err := strconv.Atoi(c.Param("id"))
  117. if err != nil {
  118. panic(err)
  119. }
  120. authorization := &Authorization{
  121. Id: id,
  122. }
  123. _, err = db.Model(authorization).WherePK().Delete()
  124. if err != nil {
  125. panic(err)
  126. }
  127. c.JSON(http.StatusOK, gin.H{
  128. "message": c.Param("id"),
  129. })
  130. }
  131. }