article.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. "context"
  10. "time"
  11. )
  12. var ctx = context.Background()
  13. /*
  14. id SERIAL PRIMARY KEY,
  15. uuid VARCHAR (36) ,
  16. title VARCHAR (32) NOT NULL,
  17. subtitle VARCHAR (32),
  18. summary VARCHAR (255),
  19. content TEXT,
  20. owner_id INTEGER NOT NULL,
  21. owner VARCHAR (36),
  22. setting JSON,
  23. status INTEGER NOT NULL DEFAULT (10),
  24. version INTEGER NOT NULL DEFAULT (1),
  25. deleted_at TIMESTAMP,
  26. created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  27. updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
  28. */
  29. type Article struct {
  30. Id int `form:"id" json:"id" binding:"required"`
  31. Title string `form:"title" json:"title" binding:"required"`
  32. Subtitle string `form:"subtitle" json:"subtitle"`
  33. Summary string `form:"summary" json:"summary"`
  34. Content string `form:"content" json:"content"`
  35. OwnerId int
  36. Setting string `form:"setting" json:"setting"`
  37. Status int `form:"status" json:"status"`
  38. Version int
  39. DeletedAt time.Time
  40. CreatedAt time.Time
  41. UpdatedAt time.Time
  42. }
  43. //查询
  44. func GetArticle(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  45. return func(c *gin.Context) {
  46. lid,err := strconv.ParseInt(c.Param("aid"),10,64)
  47. if err != nil {
  48. panic(err)
  49. }
  50. fmt.Println("get article")
  51. rkey := "article://"+c.Param("aid")
  52. n, err := rdb.Exists(ctx,rkey).Result()
  53. if err != nil {
  54. fmt.Println(err)
  55. }else if n == 0 {
  56. fmt.Println("redis key not exist")
  57. }else{
  58. fmt.Println("redis key exist")
  59. val, err := rdb.HGetAll(ctx, rkey).Result()
  60. if err != nil || val == nil {
  61. //有错误或者没查到
  62. fmt.Println("redis error")
  63. }else{
  64. fmt.Println("redis no error")
  65. c.JSON(http.StatusOK, gin.H{
  66. "data": val,
  67. })
  68. return
  69. }
  70. }
  71. article := &Article{Id: int(lid)}
  72. err = db.Model(article).Column("id","title","subtitle","content","owner_id","setting","status","version","updated_at").WherePK().Select()
  73. if err != nil {
  74. panic(err)
  75. }
  76. c.JSON(http.StatusOK, gin.H{
  77. "data": article,
  78. })
  79. //写入redis
  80. rdb.HSet(ctx,rkey,"id",article.Id)
  81. rdb.HSet(ctx,rkey,"title",article.Title)
  82. rdb.HSet(ctx,rkey,"subtitle",article.Subtitle)
  83. rdb.HSet(ctx,rkey,"content",article.Content)
  84. rdb.HSet(ctx,rkey,"owner_id",article.OwnerId)
  85. rdb.HSet(ctx,rkey,"setting",article.Setting)
  86. rdb.HSet(ctx,rkey,"status",article.Status)
  87. rdb.HSet(ctx,rkey,"version",article.Version)
  88. rdb.HSet(ctx,rkey,"updated_at",article.UpdatedAt)
  89. }
  90. }
  91. //查询
  92. func GetArticleByTitle(db *pg.DB) gin.HandlerFunc {
  93. return func(c *gin.Context) {
  94. title:= c.Param("ltitle")
  95. // TODO 在这里进行db操作
  96. // Select user by primary key.
  97. var articles []Article
  98. err := db.Model(&articles).Column("id","title","subtitle").Where("title like ?",title+"%").Select()
  99. if err != nil {
  100. panic(err)
  101. }
  102. c.JSON(http.StatusOK, gin.H{
  103. "message": articles,
  104. })
  105. }
  106. }
  107. //新建-
  108. //PUT http://127.0.0.1:8080/api/lesson?title=lesson-one&status=10
  109. func PutArticle(db *pg.DB) gin.HandlerFunc{
  110. return func(c *gin.Context){
  111. title := c.Query("title")
  112. status1,err := strconv.ParseInt(c.Query("status"),10,64)
  113. if err != nil {
  114. panic(err)
  115. }
  116. newArticle := &Article{
  117. Title: title,
  118. Status: int(status1),
  119. OwnerId:1,
  120. }
  121. _, err = db.Model(newArticle).Insert()
  122. if err != nil {
  123. panic(err)
  124. }
  125. //修改完毕
  126. c.JSON(http.StatusOK,gin.H{
  127. "message":"",
  128. })
  129. }
  130. }
  131. //修改
  132. func PostAritcle(db *pg.DB,rdb *redis.Client) gin.HandlerFunc{
  133. return func(c *gin.Context){
  134. var form Article
  135. if err := c.ShouldBindJSON(&form); err != nil {
  136. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  137. return
  138. }
  139. _,err := db.Model(&form).Column("title","subtitle","summary","status","content").WherePK().Update()
  140. if err != nil {
  141. panic(err)
  142. }
  143. c.JSON(http.StatusOK,gin.H{
  144. "message":"update ok",
  145. })
  146. rkey := "article://"+strconv.Itoa(form.Id)
  147. rdb.Del(ctx,rkey)
  148. }
  149. }
  150. //删
  151. func DeleteArticle(db *pg.DB ,rdb *redis.Client) gin.HandlerFunc{
  152. return func(c *gin.Context){
  153. id,err := strconv.Atoi(c.Param("aid"))
  154. if err != nil {
  155. panic(err)
  156. }
  157. article := &Article{
  158. Id:int(id),
  159. }
  160. //删之前获取 course_id
  161. _, err = db.Model(article).WherePK().Delete()
  162. if err != nil {
  163. panic(err)
  164. }
  165. //TODO 删除article_list表相关项目
  166. c.JSON(http.StatusOK,gin.H{
  167. "message":"delete "+c.Param("lid"),
  168. })
  169. rkey := "article://"+c.Param("aid")
  170. rdb.Del(ctx,rkey)
  171. }
  172. }