course.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 Course struct {
  11. Id int `form:"id" json:"id" binding:"required"`
  12. Cover string
  13. Title string `form:"title" json:"title"`
  14. Subtitle string `form:"subtitle" json:"subtitle"`
  15. Summary string `form:"summary" json:"summary"`
  16. Teacher int `form:"teacher" json:"teacher"`
  17. Lang string `form:"lang" json:"lang"`
  18. Speech_lang string `form:"speech_lang" json:"speech_lang"`
  19. Status int `form:"status" json:"status"`
  20. Content string `form:"content" json:"content"`
  21. Creator int
  22. LessonNum int `form:"lesson_num" json:"lesson_num"`
  23. Version int
  24. CreatedAt time.Time
  25. UpdatedAt time.Time
  26. }
  27. //查询
  28. func GetCourse(db *pg.DB) gin.HandlerFunc {
  29. return func(c *gin.Context) {
  30. cid,err := strconv.Atoi(c.Param("cid"))
  31. if err != nil {
  32. panic(err)
  33. }
  34. fmt.Println("get course")
  35. // TODO 在这里进行db操作
  36. // Select user by primary key.
  37. course := &Course{Id: cid}
  38. err = db.Model(course).WherePK().Select()
  39. if err != nil {
  40. panic(err)
  41. }
  42. c.JSON(http.StatusOK, gin.H{
  43. "message": "course-"+course.Title,
  44. })
  45. }
  46. }
  47. //查询
  48. func GetCourseByTitle(db *pg.DB) gin.HandlerFunc {
  49. return func(c *gin.Context) {
  50. ctitle:= c.Param("ctitle")
  51. // TODO 在这里进行db操作
  52. // Select user by primary key.
  53. var courses []Course
  54. err := db.Model(&courses).Column("id","title","subtitle").Where("title like ?",ctitle+"%").Select()
  55. if err != nil {
  56. panic(err)
  57. }
  58. c.JSON(http.StatusOK, gin.H{
  59. "message": courses,
  60. })
  61. }
  62. }
  63. //新建
  64. func PutCourse(db *pg.DB) gin.HandlerFunc{
  65. return func(c *gin.Context){
  66. title := c.Query("title")
  67. status1,err := strconv.ParseInt(c.Query("status"),10,64)
  68. if err != nil {
  69. panic(err)
  70. }
  71. fmt.Println("title:"+title)
  72. newCouse := &Course{
  73. Title: title,
  74. Status: int(status1),
  75. Teacher:1,
  76. Creator:1,
  77. }
  78. _, err = db.Model(newCouse).Insert()
  79. if err != nil {
  80. panic(err)
  81. }
  82. c.JSON(http.StatusOK,gin.H{
  83. "message":"new-Ok- hello "+title,
  84. })
  85. }
  86. }
  87. //改
  88. func PostCourse(db *pg.DB) gin.HandlerFunc{
  89. return func(c *gin.Context){
  90. var form Course
  91. if err := c.ShouldBindJSON(&form); err != nil {
  92. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  93. return
  94. }
  95. _,err := db.Model(&form).Table("courses").Column("title","subtitle","summary","teacher","lang","speech_lang","status","content").WherePK().Update()
  96. if err != nil {
  97. panic(err)
  98. }
  99. c.JSON(http.StatusOK,gin.H{
  100. "message":"update ok",
  101. })
  102. }
  103. }
  104. //补
  105. func PatchLessonNumInCousrse(db *pg.DB) gin.HandlerFunc{
  106. return func(c *gin.Context){
  107. var form Course
  108. if err := c.ShouldBindJSON(&form); err != nil {
  109. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  110. return
  111. }
  112. _, err := db.Model(&form).Column("lesson_num").WherePK().Update()
  113. if err != nil {
  114. panic(err)
  115. }
  116. c.JSON(http.StatusOK,gin.H{
  117. "message":"patch ok",
  118. })
  119. }
  120. }
  121. //删
  122. func DeleteCourse(db *pg.DB) gin.HandlerFunc{
  123. return func(c *gin.Context){
  124. id,err := strconv.Atoi(c.Param("cid"))
  125. if err != nil {
  126. panic(err)
  127. }
  128. course := &Course{
  129. Id: id,
  130. }
  131. _, err = db.Model(course).WherePK().Delete()
  132. if err != nil {
  133. panic(err)
  134. }
  135. c.JSON(http.StatusOK,gin.H{
  136. "message":"delete "+c.Param("cid"),
  137. })
  138. }
  139. }