collection.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. "time"
  9. )
  10. type Collection struct {
  11. Id int `form:"id" json:"id" `
  12. Uid string `form:"uid" json:"uid" `
  13. ParentId int `form:"parent_id" json:"parent_id"`
  14. PrParentId int `form:"pr_parent_id" json:"pr_parent_id" `
  15. Title string `form:"title" json:"title" `
  16. Subtitle string `form:"subtitle" json:"subtitle" `
  17. Summary string `form:"summary" json:"summary"`
  18. ArticleList string `form:"article_list" json:"article_list" `
  19. Lang string `form:"lang" json:"lang" `
  20. Setting string `form:"setting" json:"setting" `
  21. Status string `form:"status" json:"status" `
  22. OwnerId int
  23. Version int
  24. DeletedAt time.Time
  25. CreatedAt time.Time
  26. UpdatedAt time.Time
  27. }
  28. //查询
  29. //display a list of all collections
  30. func CollectionsIndex(db *pg.DB) gin.HandlerFunc {
  31. return func(c *gin.Context) {
  32. title:= c.Param("ctitle")
  33. // TODO 在这里进行db操作
  34. // Select user by primary key.
  35. var collections []Collection
  36. err := db.Model(&collections).Column("id","title","subtitle").Where("title like ?",title+"%").Select()
  37. if err != nil {
  38. panic(err)
  39. }
  40. c.JSON(http.StatusOK, gin.H{
  41. "message": collections,
  42. })
  43. }
  44. }
  45. //查询
  46. //display a specific collections
  47. func CollectionsShow(db *pg.DB) gin.HandlerFunc {
  48. return func(c *gin.Context) {
  49. id,err := strconv.Atoi(c.Param("id"))
  50. if err != nil {
  51. panic(err)
  52. }
  53. // TODO 在这里进行db操作
  54. // Select user by primary key.
  55. collection := &Collection{Id: id}
  56. err = db.Model(collection).Column("title","subtitle","summary","status").WherePK().Select()
  57. if err != nil {
  58. panic(err)
  59. }
  60. c.JSON(http.StatusOK, gin.H{
  61. "message": collection,
  62. })
  63. }
  64. }
  65. //return an HTML form for creating a new Courses
  66. func CollectionsNew(db *pg.DB) gin.HandlerFunc {
  67. return func(c *gin.Context) {
  68. //TODO 业务逻辑
  69. c.HTML(http.StatusOK,"collections/new.html",gin.H{
  70. "message":"ok",
  71. })
  72. }
  73. }
  74. //新建-
  75. //create a new collections
  76. func CollectionsCreate(db *pg.DB) gin.HandlerFunc{
  77. return func(c *gin.Context){
  78. title := c.Query("title")
  79. status :=c.Query("status")
  80. newOne := &Collection{
  81. Title: title,
  82. Status: status,
  83. OwnerId:1,
  84. }
  85. _, err := db.Model(newOne).Insert()
  86. if err != nil {
  87. panic(err)
  88. }
  89. //修改完毕
  90. c.JSON(http.StatusOK,gin.H{
  91. "message":"insert ok",
  92. })
  93. }
  94. }
  95. //return an HTML form for edit a Courses
  96. func CollectionsEdit(db *pg.DB) gin.HandlerFunc {
  97. return func(c *gin.Context) {
  98. //TODO 业务逻辑
  99. c.HTML(http.StatusOK,"collections/edit.html",gin.H{
  100. "message":"ok",
  101. })
  102. }
  103. }
  104. //改
  105. //update a specific collections
  106. func CollectionsUpdate(db *pg.DB) gin.HandlerFunc{
  107. return func(c *gin.Context){
  108. var form Collection
  109. if err := c.ShouldBindJSON(&form); err != nil {
  110. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  111. return
  112. }
  113. _,err := db.Model(&form).Column("title","subtitle","summary","status","article_list").WherePK().Update()
  114. if err != nil {
  115. panic(err)
  116. }
  117. c.JSON(http.StatusOK,gin.H{
  118. "message":form,
  119. })
  120. }
  121. }
  122. //删
  123. //delete a specific collections
  124. func CollectionsDestroy(db *pg.DB) gin.HandlerFunc{
  125. return func(c *gin.Context){
  126. id,err := strconv.Atoi(c.Param("id"))
  127. if err != nil {
  128. panic(err)
  129. }
  130. collection := &Collection{
  131. Id:int(id),
  132. }
  133. _, err = db.Model(collection).WherePK().Where("parent_id = ?",id).Where("pr_parent_id = ?",id).Delete()
  134. if err != nil {
  135. panic(err)
  136. }
  137. //TODO redis delete
  138. c.JSON(http.StatusOK,gin.H{
  139. "message":c.Param("id"),
  140. })
  141. }
  142. }