book.go 3.8 KB

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