dictionaries.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 Dictionary struct {
  12. Id int `form:"id" json:"id" `
  13. Word string `form:"word" json:"word" `
  14. Type string `form:"type" json:"type"`
  15. Grammar string `form:"grammar" json:"grammar"`
  16. Base string `form:"base" json:"base"`
  17. Meaning string `form:"meaning" json:"meaning"`
  18. Note string `form:"note" json:"note"`
  19. Factors string `form:"factors" json:"factors"`
  20. FactorsMeaning string `form:"factors_meaning" json:"factors_meaning"`
  21. Lang string `form:"lang" json:"lang"`
  22. Sourse string `form:"sourse" json:"sourse"`
  23. OwnerId int
  24. Version int
  25. DeletedAt time.Time
  26. CreatedAt time.Time
  27. UpdatedAt time.Time
  28. }
  29. //display a list of all dictionaries
  30. func DictionariesIndex(db *pg.DB) gin.HandlerFunc {
  31. return func(c *gin.Context) {
  32. word:= c.DefaultQuery("word","")
  33. // TODO 补充业务逻辑
  34. var dictionaries []Dictionary
  35. err := db.Model(&dictionaries).Column("id","word","type","grammar","meaning").Where("word = ? ",word).Select()
  36. if err != nil {
  37. panic(err)
  38. }
  39. c.JSON(http.StatusOK, gin.H{
  40. "message":"success",
  41. "data": dictionaries,
  42. })
  43. }
  44. }
  45. //return an HTML form for creating a new dictionary
  46. func DictionariesNew(db *pg.DB) gin.HandlerFunc{
  47. return func(c *gin.Context){
  48. //TODO 业务逻辑
  49. c.HTML(http.StatusOK,"dictionaries/new.html",gin.H{
  50. "message":"ok",
  51. })
  52. }
  53. }
  54. //create a new dictionary
  55. func DictionariesCreate(db *pg.DB) gin.HandlerFunc{
  56. return func(c *gin.Context){
  57. var form Dictionary
  58. if err := c.ShouldBindJSON(&form); err != nil {
  59. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  60. return
  61. }
  62. form.OwnerId = 1
  63. //TODO补充业务逻辑
  64. _, err := db.Model(&form).Insert()
  65. if err != nil {
  66. panic(err)
  67. }
  68. //建立成功
  69. c.JSON(http.StatusOK,gin.H{
  70. "message":"success",
  71. "data":form,
  72. })
  73. }
  74. }
  75. //display a specific Dictionary
  76. func DictionariesShow(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  77. return func(c *gin.Context) {
  78. id,err := strconv.Atoi(c.Param("id"))
  79. if err != nil {
  80. panic(err)
  81. }
  82. fmt.Println("get dictionary id=" + c.Param("id"))
  83. rkey := "dictionary://id/"+c.Param("id")
  84. n, err := rdb.Exists(ctx,rkey).Result()
  85. if err != nil {
  86. fmt.Println(err)
  87. }else if n == 0 {
  88. fmt.Println("redis key not exist")
  89. }else{
  90. fmt.Println("redis key exist")
  91. val, err := rdb.HGetAll(ctx, rkey).Result()
  92. if err != nil || val == nil {
  93. //有错误或者没查到
  94. fmt.Println("redis error")
  95. }else{
  96. fmt.Println("redis no error")
  97. c.JSON(http.StatusOK, gin.H{
  98. "data": val,
  99. })
  100. return
  101. }
  102. }
  103. dictionary := &Dictionary{Id: id}
  104. err = db.Model(dictionary).Column("id","uid","name","description","description_type","owner_id","setting","status","version","updated_at").WherePK().Select()
  105. if err != nil {
  106. panic(err)
  107. }
  108. //写入redis
  109. rdb.HSet(ctx,rkey,"id",dictionary.Id)
  110. rdb.HSet(ctx,rkey,"word",dictionary.Word)
  111. rdb.HSet(ctx,rkey,"type",dictionary.Type)
  112. rdb.HSet(ctx,rkey,"grammar",dictionary.Grammar)
  113. rdb.HSet(ctx,rkey,"base",dictionary.Base)
  114. rdb.HSet(ctx,rkey,"meaning",dictionary.Meaning)
  115. rdb.HSet(ctx,rkey,"note",dictionary.Note)
  116. rdb.HSet(ctx,rkey,"factors",dictionary.Factors)
  117. rdb.HSet(ctx,rkey,"factors_meaning",dictionary.FactorsMeaning)
  118. rdb.HSet(ctx,rkey,"lang",dictionary.Lang)
  119. rdb.HSet(ctx,rkey,"sourse",dictionary.Sourse)
  120. rdb.HSet(ctx,rkey,"updated_at",dictionary.UpdatedAt)
  121. c.JSON(http.StatusOK, gin.H{
  122. "message":"success",
  123. "data": dictionary,
  124. })
  125. }
  126. }
  127. //return an HTML form for edit a dictionary
  128. func DictionariesEdit(db *pg.DB) gin.HandlerFunc{
  129. return func(c *gin.Context){
  130. //TODO 业务逻辑
  131. c.HTML(http.StatusOK,"dictionaries/edit.html",gin.H{
  132. "name":"ok",
  133. })
  134. }
  135. }
  136. //update a specific dictionary
  137. func DictionariesUpdate(db *pg.DB,rdb *redis.Client) gin.HandlerFunc{
  138. return func(c *gin.Context){
  139. var form Dictionary
  140. if err := c.ShouldBindJSON(&form); err != nil {
  141. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  142. return
  143. }
  144. //补充业务逻辑
  145. _,err := db.Model(&form).Column("type","grammar","base","meaning","note","factors","factors_meaning","lang","sourse").WherePK().Update()
  146. if err != nil {
  147. panic(err)
  148. }
  149. //delete redis
  150. rkey := "dictionary://id/"+strconv.Itoa(form.Id)
  151. rdb.Del(ctx,rkey)
  152. c.JSON(http.StatusOK,gin.H{
  153. "message":"success",
  154. "data":form,
  155. })
  156. }
  157. }
  158. //delete a specific dictionary
  159. func DictionariesDestroy(db *pg.DB ,rdb *redis.Client) gin.HandlerFunc{
  160. return func(c *gin.Context){
  161. id,err := strconv.Atoi(c.Param("id"))
  162. if err != nil {
  163. panic(err)
  164. }
  165. dictionary := &Dictionary{
  166. Id:int(id),
  167. }
  168. _, err = db.Model(dictionary).WherePK().Delete()
  169. if err != nil {
  170. panic(err)
  171. }
  172. rkey := "dictionary://id/"+c.Param("id")
  173. rdb.Del(ctx,rkey)
  174. c.JSON(http.StatusOK,gin.H{
  175. "message": "success",
  176. "data":c.Param("id"),
  177. })
  178. }
  179. }