group.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package mint
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "github.com/go-pg/pg/v10"
  10. "github.com/go-redis/redis/v8"
  11. "github.com/google/uuid"
  12. )
  13. type Group struct {
  14. Id int `form:"id" json:"id" `
  15. Uid string `form:"uid" json:"uid" `
  16. Name string `form:"name" json:"name"`
  17. Description string `form:"description" json:"description"`
  18. DescriptionType string `form:"description_type" json:"description_type"`
  19. Setting string `form:"setting" json:"setting"`
  20. Status string `form:"status" json:"status"`
  21. OwnerId int
  22. Version int
  23. DeletedAt time.Time
  24. CreatedAt time.Time
  25. UpdatedAt time.Time
  26. }
  27. //display a list of all groups
  28. func GroupsIndex(db *pg.DB) gin.HandlerFunc {
  29. return func(c *gin.Context) {
  30. view := c.Query("view")
  31. switch view {
  32. case "owner":
  33. case "write":
  34. case "read":
  35. }
  36. // TODO 补充业务逻辑
  37. var groups []Group
  38. err := db.Model(&groups).Column("id", "uid", "name").Where("owner_id = ?", 1).Select()
  39. if err != nil {
  40. panic(err)
  41. }
  42. c.JSON(http.StatusOK, gin.H{
  43. "status": "success",
  44. "data": groups,
  45. })
  46. }
  47. }
  48. //return an HTML form for creating a new group
  49. func GroupsNew(db *pg.DB) gin.HandlerFunc {
  50. return func(c *gin.Context) {
  51. //TODO 业务逻辑
  52. c.HTML(http.StatusOK, "group_new.html", gin.H{
  53. "message": "ok",
  54. })
  55. }
  56. }
  57. //create a new group
  58. func GroupsCreate(db *pg.DB) gin.HandlerFunc {
  59. return func(c *gin.Context) {
  60. name := c.Query("name")
  61. status := c.DefaultQuery("status", "private")
  62. newGroup := &Group{
  63. Uid: uuid.New().String(),
  64. Name: name,
  65. Status: status,
  66. OwnerId: 1, //TODO user_id
  67. }
  68. _, err := db.Model(newGroup).Column("name", "status").Insert()
  69. if err != nil {
  70. panic(err)
  71. }
  72. //建立成功
  73. c.JSON(http.StatusOK, gin.H{
  74. "status": "success",
  75. "data": newGroup,
  76. })
  77. }
  78. }
  79. //display a specific Group
  80. func GroupsShow(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  81. return func(c *gin.Context) {
  82. id, err := strconv.Atoi(c.Param("id"))
  83. if err != nil {
  84. panic(err)
  85. }
  86. rkey := "group://id"
  87. n, err := rdb.HExists(ctx, rkey, c.Param("id")).Result()
  88. if err == nil && n {
  89. val, err := rdb.HGet(ctx, rkey, c.Param("id")).Result()
  90. if err == nil {
  91. var redisData Group
  92. json.Unmarshal([]byte(val), &redisData)
  93. c.JSON(http.StatusOK, gin.H{
  94. "status": "success",
  95. "data": redisData,
  96. })
  97. return
  98. } else {
  99. fmt.Println(err)
  100. }
  101. } else {
  102. fmt.Println(err)
  103. }
  104. group := &Group{Id: id}
  105. err = db.Model(group).Column("id", "uid", "name", "description", "description_type", "owner_id", "setting", "status", "version", "updated_at").WherePK().First()
  106. if err != nil {
  107. if err.Error() == pg.ErrNoRows.Error() {
  108. c.JSON(http.StatusOK, gin.H{
  109. "status": "fail",
  110. "message": "no-rows",
  111. })
  112. } else {
  113. panic(err)
  114. }
  115. } else {
  116. c.JSON(http.StatusOK, gin.H{
  117. "status": "sucess",
  118. "data": group,
  119. })
  120. //写入redis
  121. jsonData, err := json.Marshal(group)
  122. if err == nil {
  123. rdb.HSet(ctx, rkey, c.Param("id"), string(jsonData))
  124. }
  125. }
  126. }
  127. }
  128. //return an HTML form for edit a group
  129. func GroupsEdit(db *pg.DB) gin.HandlerFunc {
  130. return func(c *gin.Context) {
  131. //TODO 业务逻辑
  132. c.HTML(http.StatusOK, "groups_edit.html", gin.H{
  133. "name": "ok",
  134. })
  135. }
  136. }
  137. //update a specific group
  138. func GroupsUpdate(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  139. return func(c *gin.Context) {
  140. var form Group
  141. if err := c.ShouldBindJSON(&form); err != nil {
  142. c.JSON(http.StatusBadRequest, gin.H{
  143. "status": "fail",
  144. "message": err.Error(),
  145. })
  146. return
  147. }
  148. //补充业务逻辑
  149. _, err := db.Model(&form).Column("name", "description", "description_type", "status", "setting").WherePK().Update()
  150. if err != nil {
  151. panic(err)
  152. }
  153. c.JSON(http.StatusOK, gin.H{
  154. "status": "sucess",
  155. "data": form,
  156. })
  157. //delete redis
  158. rkey := "group://id"
  159. rdb.HDel(ctx, rkey, c.Param("id"))
  160. }
  161. }
  162. //delete a specific group
  163. func GroupsDestroy(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  164. return func(c *gin.Context) {
  165. id, err := strconv.Atoi(c.Param("id"))
  166. if err != nil {
  167. panic(err)
  168. }
  169. group := &Group{
  170. Id: id,
  171. }
  172. _, err = db.Model(group).WherePK().Delete()
  173. if err != nil {
  174. panic(err)
  175. }
  176. c.JSON(http.StatusOK, gin.H{
  177. "status": "sucess",
  178. "data": c.Param("id"),
  179. })
  180. rkey := "group://id"
  181. rdb.Del(ctx, rkey, c.Param("id"))
  182. }
  183. }