term.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 Term struct {
  12. Id int `form:"id" json:"id" `
  13. PrParentId int `form:"pr_parent_id" json:"pr_parent_id" `
  14. Word string `form:"word" json:"word" `
  15. WordEn string `form:"word_en" json:"word_en"`
  16. Tag string `form:"tag" json:"tag"`
  17. ChannelId string `form:"channel_id" json:"channel_id"`
  18. Meaning string `form:"meaning" json:"meaning"`
  19. Meaning2 string `form:"meaning2" json:"meaning2"`
  20. Note string `form:"note" json:"note"`
  21. Lang string `form:"lang" json:"lang"`
  22. Sourse string `form:"sourse" json:"sourse"`
  23. Confidence int `form:"confidence" json:"confidence"`
  24. OwnerId int
  25. Version int
  26. CreatedAt time.Time
  27. UpdatedAt time.Time
  28. }
  29. //display a list of all terms
  30. func TermsIndex(db *pg.DB) gin.HandlerFunc {
  31. return func(c *gin.Context) {
  32. word:= c.DefaultQuery("word","")
  33. // TODO 补充业务逻辑
  34. var terms []Term
  35. err := db.Model(&terms).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": terms,
  42. })
  43. }
  44. }
  45. //return an HTML form for creating a new term
  46. func TermsNew(db *pg.DB) gin.HandlerFunc{
  47. return func(c *gin.Context){
  48. //TODO 业务逻辑
  49. c.HTML(http.StatusOK,"terms/new.html",gin.H{
  50. "message":"ok",
  51. })
  52. }
  53. }
  54. //create a new term
  55. func TermsCreate(db *pg.DB) gin.HandlerFunc{
  56. return func(c *gin.Context){
  57. var form Term
  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 Term
  76. func TermsShow(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 term id=" + c.Param("id"))
  83. rkey := "term://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. term := &Term{Id: id}
  104. err = db.Model(term).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",term.Id)
  110. rdb.HSet(ctx,rkey,"pr_parent_id",term.PrParentId)
  111. rdb.HSet(ctx,rkey,"word",term.Word)
  112. rdb.HSet(ctx,rkey,"word_en",term.WordEn)
  113. rdb.HSet(ctx,rkey,"tag",term.Tag)
  114. rdb.HSet(ctx,rkey,"channel_id",term.ChannelId)
  115. rdb.HSet(ctx,rkey,"meaning",term.Meaning)
  116. rdb.HSet(ctx,rkey,"meaning2",term.Meaning2)
  117. rdb.HSet(ctx,rkey,"note",term.Note)
  118. rdb.HSet(ctx,rkey,"lang",term.Lang)
  119. rdb.HSet(ctx,rkey,"sourse",term.Sourse)
  120. rdb.HSet(ctx,rkey,"confidence",term.Confidence)
  121. rdb.HSet(ctx,rkey,"owner_id",term.OwnerId)
  122. rdb.HSet(ctx,rkey,"version",term.Version)
  123. rdb.HSet(ctx,rkey,"updated_at",term.UpdatedAt)
  124. c.JSON(http.StatusOK, gin.H{
  125. "message":"success",
  126. "data": term,
  127. })
  128. }
  129. }
  130. //return an HTML form for edit a term
  131. func TermsEdit(db *pg.DB) gin.HandlerFunc{
  132. return func(c *gin.Context){
  133. //TODO 业务逻辑
  134. c.HTML(http.StatusOK,"terms/edit.html",gin.H{
  135. "name":"ok",
  136. })
  137. }
  138. }
  139. //update a specific term
  140. func TermsUpdate(db *pg.DB,rdb *redis.Client) gin.HandlerFunc{
  141. return func(c *gin.Context){
  142. var form Term
  143. if err := c.ShouldBindJSON(&form); err != nil {
  144. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  145. return
  146. }
  147. //补充业务逻辑
  148. _,err := db.Model(&form).Column("type","grammar","base","meaning","note","factors","factors_meaning","lang","sourse").WherePK().Update()
  149. if err != nil {
  150. panic(err)
  151. }
  152. //delete redis
  153. rkey := "term://id/"+strconv.Itoa(form.Id)
  154. rdb.Del(ctx,rkey)
  155. c.JSON(http.StatusOK,gin.H{
  156. "message":"success",
  157. "data":form,
  158. })
  159. }
  160. }
  161. //delete a specific term
  162. func TermsDestroy(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. term := &Term{
  169. Id:int(id),
  170. }
  171. _, err = db.Model(term).WherePK().Delete()
  172. if err != nil {
  173. panic(err)
  174. }
  175. rkey := "term://id/"+c.Param("id")
  176. rdb.Del(ctx,rkey)
  177. c.JSON(http.StatusOK,gin.H{
  178. "message": "success",
  179. "data":c.Param("id"),
  180. })
  181. }
  182. }