channel.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package mint
  2. import (
  3. "net/http"
  4. "strconv"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/go-pg/pg/v10"
  8. "github.com/go-redis/redis/v8"
  9. "time"
  10. )
  11. type Channel struct {
  12. Id int `form:"id" json:"id" `
  13. Uid string `form:"uid" json:"uid" `
  14. Title string `form:"title" json:"title"`
  15. Summary string `form:"summary" json:"summary"`
  16. Lang string `lang:"summary" json:"lang"`
  17. Setting string `form:"setting" json:"setting"`
  18. Status string `form:"status" json:"status"`
  19. OwnerId int
  20. Version int
  21. DeletedAt time.Time
  22. CreatedAt time.Time
  23. UpdatedAt time.Time
  24. }
  25. //display a list of all channels
  26. func ChannelsIndex(db *pg.DB) gin.HandlerFunc {
  27. return func(c *gin.Context) {
  28. name:= c.DefaultQuery("name","")
  29. // TODO 补充业务逻辑
  30. var channels []Channel
  31. err := db.Model(&channels).Column("id","name").Where("name like ?",name+"%").Select()
  32. if err != nil {
  33. panic(err)
  34. }
  35. c.JSON(http.StatusOK, gin.H{
  36. "data": channels,
  37. })
  38. }
  39. }
  40. //return an HTML form for creating a new channel
  41. func ChannelsNew(db *pg.DB) gin.HandlerFunc{
  42. return func(c *gin.Context){
  43. //TODO 业务逻辑
  44. c.HTML(http.StatusOK,"channels/new.html",gin.H{
  45. "message":"ok",
  46. })
  47. }
  48. }
  49. //create a new channel
  50. func ChannelsCreate(db *pg.DB) gin.HandlerFunc{
  51. return func(c *gin.Context){
  52. name := c.Query("title")
  53. status := c.DefaultQuery("status","private")
  54. newChannel := &Channel{
  55. Title: name,
  56. Status: status,
  57. OwnerId:1,//TODO user_id
  58. }
  59. _, err := db.Model(newChannel).Insert()
  60. if err != nil {
  61. panic(err)
  62. }
  63. //建立成功
  64. c.JSON(http.StatusOK,gin.H{
  65. "message":"ok",
  66. })
  67. }
  68. }
  69. //display a specific Channel
  70. func ChannelsShow(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  71. return func(c *gin.Context) {
  72. id,err := strconv.Atoi(c.Param("id"))
  73. if err != nil {
  74. panic(err)
  75. }
  76. fmt.Println("get channel id=" + c.Param("id"))
  77. rkey := "channel://id/"+c.Param("id")
  78. n, err := rdb.Exists(ctx,rkey).Result()
  79. if err != nil {
  80. fmt.Println(err)
  81. }else if n == 0 {
  82. fmt.Println("redis key not exist")
  83. }else{
  84. fmt.Println("redis key exist")
  85. val, err := rdb.HGetAll(ctx, rkey).Result()
  86. if err != nil || val == nil {
  87. //有错误或者没查到
  88. fmt.Println("redis error")
  89. }else{
  90. fmt.Println("redis no error")
  91. c.JSON(http.StatusOK, gin.H{
  92. "data": val,
  93. })
  94. return
  95. }
  96. }
  97. channel := &Channel{Id: id}
  98. err = db.Model(channel).Column("id","uid","name","description","description_type","owner_id","setting","status","version","updated_at").WherePK().Select()
  99. if err != nil {
  100. panic(err)
  101. }
  102. c.JSON(http.StatusOK, gin.H{
  103. "data": channel,
  104. })
  105. //写入redis
  106. rdb.HSet(ctx,rkey,"id",channel.Id)
  107. rdb.HSet(ctx,rkey,"uid",channel.Uid)
  108. rdb.HSet(ctx,rkey,"title",channel.Title)
  109. rdb.HSet(ctx,rkey,"summary",channel.Summary)
  110. rdb.HSet(ctx,rkey,"lang",channel.Lang)
  111. rdb.HSet(ctx,rkey,"owner_id",channel.OwnerId)
  112. rdb.HSet(ctx,rkey,"setting",channel.Setting)
  113. rdb.HSet(ctx,rkey,"status",channel.Status)
  114. rdb.HSet(ctx,rkey,"version",channel.Version)
  115. rdb.HSet(ctx,rkey,"updated_at",channel.UpdatedAt)
  116. rdb.HSet(ctx,rkey,"created_at",channel.CreatedAt)
  117. }
  118. }
  119. //return an HTML form for edit a channel
  120. func ChannelsEdit(db *pg.DB) gin.HandlerFunc{
  121. return func(c *gin.Context){
  122. //TODO 业务逻辑
  123. c.HTML(http.StatusOK,"channels/edit.html",gin.H{
  124. "name":"ok",
  125. })
  126. }
  127. }
  128. //update a specific channel
  129. func ChannelsUpdate(db *pg.DB,rdb *redis.Client) gin.HandlerFunc{
  130. return func(c *gin.Context){
  131. var form Channel
  132. if err := c.ShouldBindJSON(&form); err != nil {
  133. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  134. return
  135. }
  136. //补充业务逻辑
  137. _,err := db.Model(&form).Column("name","description","description_type","status","setting").WherePK().Update()
  138. if err != nil {
  139. panic(err)
  140. }
  141. c.JSON(http.StatusOK,gin.H{
  142. "message":form,
  143. })
  144. //delete redis
  145. rkey := "channel://id/"+strconv.Itoa(form.Id)
  146. rdb.Del(ctx,rkey)
  147. }
  148. }
  149. //delete a specific channel
  150. func ChannelsDestroy(db *pg.DB ,rdb *redis.Client) gin.HandlerFunc{
  151. return func(c *gin.Context){
  152. id,err := strconv.Atoi(c.Param("id"))
  153. if err != nil {
  154. panic(err)
  155. }
  156. channel := &Channel{
  157. Id:int(id),
  158. }
  159. _, err = db.Model(channel).WherePK().Delete()
  160. if err != nil {
  161. panic(err)
  162. }
  163. rkey := "channel://id/"+c.Param("id")
  164. rdb.Del(ctx,rkey)
  165. c.JSON(http.StatusOK,gin.H{
  166. "message": c.Param("id"),
  167. })
  168. }
  169. }