article.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /*
  11. id SERIAL PRIMARY KEY,
  12. uuid VARCHAR (36) ,
  13. title VARCHAR (32) NOT NULL,
  14. subtitle VARCHAR (32),
  15. summary VARCHAR (255),
  16. content TEXT,
  17. owner_id INTEGER NOT NULL,
  18. owner VARCHAR (36),
  19. setting JSON,
  20. status INTEGER NOT NULL DEFAULT (10),
  21. version INTEGER NOT NULL DEFAULT (1),
  22. deleted_at TIMESTAMP,
  23. created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  24. updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
  25. */
  26. type Article struct {
  27. Id int `form:"id" json:"id" binding:"required"`
  28. Title string `form:"title" json:"title" binding:"required"`
  29. Subtitle string `form:"subtitle" json:"subtitle" binding:"required"`
  30. Summary string `form:"summary" json:"summary" binding:"required"`
  31. Content string `form:"content" json:"content" binding:"required"`
  32. OwnerId int
  33. Setting string `form:"setting" json:"setting" binding:"required"`
  34. Status int `form:"status" json:"status" binding:"required"`
  35. Version int
  36. DeletedAt time.Time
  37. CreatedAt time.Time
  38. UpdatedAt time.Time
  39. }
  40. //查询
  41. func GetArticle(db *pg.DB) gin.HandlerFunc {
  42. return func(c *gin.Context) {
  43. lid,err := strconv.ParseInt(c.Param("aid"),10,64)
  44. if err != nil {
  45. panic(err)
  46. }
  47. fmt.Println("get lesson")
  48. // TODO 在这里进行db操作
  49. // Select user by primary key.
  50. article := &Article{Id: int(lid)}
  51. err = db.Model(article).WherePK().Select()
  52. if err != nil {
  53. panic(err)
  54. }
  55. c.JSON(http.StatusOK, gin.H{
  56. "message": "article-"+article.Title,
  57. })
  58. }
  59. }
  60. //查询
  61. func GetArticleByTitle(db *pg.DB) gin.HandlerFunc {
  62. return func(c *gin.Context) {
  63. title:= c.Param("ltitle")
  64. // TODO 在这里进行db操作
  65. // Select user by primary key.
  66. var articles []Article
  67. err := db.Model(&articles).Column("id","title","subtitle").Where("title like ?",title+"%").Select()
  68. if err != nil {
  69. panic(err)
  70. }
  71. c.JSON(http.StatusOK, gin.H{
  72. "message": articles,
  73. })
  74. }
  75. }
  76. //新建-
  77. //PUT http://127.0.0.1:8080/api/lesson?title=lesson-one&status=10
  78. func PutArticle(db *pg.DB) gin.HandlerFunc{
  79. return func(c *gin.Context){
  80. title := c.Query("title")
  81. status1,err := strconv.ParseInt(c.Query("status"),10,64)
  82. if err != nil {
  83. panic(err)
  84. }
  85. newArticle := &Article{
  86. Title: title,
  87. Status: int(status1),
  88. OwnerId:1,
  89. }
  90. _, err = db.Model(newArticle).Insert()
  91. if err != nil {
  92. panic(err)
  93. }
  94. //修改完毕
  95. c.JSON(http.StatusOK,gin.H{
  96. "message":"",
  97. })
  98. }
  99. }
  100. //修改
  101. func PostAritcle(db *pg.DB) gin.HandlerFunc{
  102. return func(c *gin.Context){
  103. var form Article
  104. if err := c.ShouldBindJSON(&form); err != nil {
  105. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  106. return
  107. }
  108. _,err := db.Model(&form).Column("title","subtitle","summary","status","content").WherePK().Update()
  109. if err != nil {
  110. panic(err)
  111. }
  112. c.JSON(http.StatusOK,gin.H{
  113. "message":"update ok",
  114. })
  115. }
  116. }
  117. //删
  118. func DeleteArticle(db *pg.DB) gin.HandlerFunc{
  119. return func(c *gin.Context){
  120. id,err := strconv.ParseInt(c.Param("aid"),10,64)
  121. if err != nil {
  122. panic(err)
  123. }
  124. article := &Article{
  125. Id:int(id),
  126. }
  127. //删之前获取 course_id
  128. _, err = db.Model(article).WherePK().Delete()
  129. if err != nil {
  130. panic(err)
  131. }
  132. c.JSON(http.StatusOK,gin.H{
  133. "message":"delete "+c.Param("lid"),
  134. })
  135. }
  136. }