comment.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 Comment struct {
  14. Id int `form:"id" json:"id" `
  15. Uid string `form:"uid" json:"uid" `
  16. ParentId int `form:"parent_id" json:"parent_id"`
  17. ParentType string `form:"parent_type" json:"parent_type"`
  18. Title string `form:"title" json:"title"`
  19. Content string `form:"content" json:"content"`
  20. ContentType string `form:"content_type" json:"content_type"`
  21. Status string `form:"status" json:"status"`
  22. OwnerId int
  23. Version int
  24. CreatedAt time.Time
  25. UpdatedAt time.Time
  26. }
  27. //display a list of all comments
  28. func CommentsIndex(db *pg.DB) gin.HandlerFunc {
  29. return func(c *gin.Context) {
  30. parentid := c.Query("parentid")
  31. parenttype := c.Query("parenttype")
  32. // TODO 补充业务逻辑
  33. var comments []Comment
  34. err := db.Model(&comments).Column("id", "title", "content", "content_type", "status").Where("parent_id = ?", parentid).Where("parent_type = ?", parenttype).Where("status = checked").Select()
  35. if err != nil {
  36. panic(err)
  37. }
  38. c.JSON(http.StatusOK, gin.H{
  39. "status": "success",
  40. "data": comments,
  41. })
  42. }
  43. }
  44. //return an HTML form for creating a new comment
  45. func CommentsNew(db *pg.DB) gin.HandlerFunc {
  46. return func(c *gin.Context) {
  47. //TODO 业务逻辑
  48. c.HTML(http.StatusOK, "comments_new.html", gin.H{
  49. "message": "ok",
  50. })
  51. }
  52. }
  53. //create a new comment
  54. func CommentsCreate(db *pg.DB) gin.HandlerFunc {
  55. return func(c *gin.Context) {
  56. var newComment Comment
  57. if err := c.ShouldBindJSON(&newComment); err != nil {
  58. c.JSON(http.StatusBadRequest, gin.H{
  59. "status": "fail",
  60. "message": err.Error(),
  61. })
  62. return
  63. }
  64. newComment.Uid = uuid.New().String()
  65. newComment.Status = "checking"
  66. newComment.OwnerId = 1
  67. _, err := db.Model(&newComment).Column("id", "uid", "parent_id", "parent_type", "title", "content", "content_type", "status", "owner_id").Insert()
  68. if err != nil {
  69. panic(err)
  70. }
  71. //建立成功
  72. c.JSON(http.StatusOK, gin.H{
  73. "status": "success",
  74. "data": newComment,
  75. })
  76. }
  77. }
  78. //display a specific Comment
  79. func CommentsShow(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  80. return func(c *gin.Context) {
  81. id, err := strconv.Atoi(c.Param("id"))
  82. if err != nil {
  83. panic(err)
  84. }
  85. rkey := "comment://id"
  86. n, err := rdb.HExists(ctx, rkey, c.Param("id")).Result()
  87. if err == nil && n {
  88. val, err := rdb.HGet(ctx, rkey, c.Param("id")).Result()
  89. if err == nil {
  90. var redisData Comment
  91. json.Unmarshal([]byte(val), &redisData)
  92. c.JSON(http.StatusOK, gin.H{
  93. "status": "success",
  94. "data": redisData,
  95. })
  96. return
  97. } else {
  98. fmt.Println(err)
  99. }
  100. } else {
  101. fmt.Println(err)
  102. }
  103. comment := &Comment{Id: id}
  104. err = db.Model(comment).Column("id", "uid", "parent_id", "parent_type", "title", "content", "content_type", "status", "owner_id").WherePK().First()
  105. if err != nil {
  106. if err.Error() == pg.ErrNoRows.Error() {
  107. c.JSON(http.StatusOK, gin.H{
  108. "status": "fail",
  109. "message": "no-rows",
  110. })
  111. } else {
  112. panic(err)
  113. }
  114. } else {
  115. c.JSON(http.StatusOK, gin.H{
  116. "status": "sucess",
  117. "data": comment,
  118. })
  119. //写入redis
  120. jsonData, err := json.Marshal(comment)
  121. if err == nil {
  122. rdb.HSet(ctx, rkey, c.Param("id"), string(jsonData))
  123. }
  124. }
  125. }
  126. }
  127. //return an HTML form for edit a comment
  128. func CommentsEdit(db *pg.DB) gin.HandlerFunc {
  129. return func(c *gin.Context) {
  130. //TODO 业务逻辑
  131. c.HTML(http.StatusOK, "comment_edit.html", gin.H{
  132. "name": "ok",
  133. })
  134. }
  135. }
  136. //update a specific comment
  137. func CommentsUpdate(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  138. return func(c *gin.Context) {
  139. var form Comment
  140. if err := c.ShouldBindJSON(&form); err != nil {
  141. c.JSON(http.StatusBadRequest, gin.H{
  142. "status": "fail",
  143. "message": err.Error(),
  144. })
  145. return
  146. }
  147. //补充业务逻辑
  148. _, err := db.Model(&form).Column("title", "content", "content_type", "status").WherePK().Update()
  149. if err != nil {
  150. panic(err)
  151. }
  152. c.JSON(http.StatusOK, gin.H{
  153. "status": "sucess",
  154. "data": form,
  155. })
  156. //delete redis
  157. rkey := "comment://id"
  158. rdb.HDel(ctx, rkey, c.Param("id"))
  159. }
  160. }
  161. //delete a specific comment
  162. func CommentsDestroy(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  163. return func(c *gin.Context) {
  164. id, err := strconv.Atoi(c.Param("id"))
  165. if err != nil {
  166. panic(err)
  167. }
  168. comment := &Comment{
  169. Id: int(id),
  170. }
  171. _, err = db.Model(comment).WherePK().Delete()
  172. if err != nil {
  173. panic(err)
  174. }
  175. rkey := "comment://id"
  176. rdb.HDel(ctx, rkey, c.Param("id"))
  177. c.JSON(http.StatusOK, gin.H{
  178. "status": "sucess",
  179. "data": c.Param("id"),
  180. })
  181. }
  182. }