lesson.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package mint
  2. import (
  3. "net/http"
  4. "io/ioutil"
  5. /*"strings"*/
  6. "bytes"
  7. "encoding/json"
  8. "strconv"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/go-pg/pg/v10"
  12. "time"
  13. )
  14. type Lesson struct {
  15. Id int `form:"id" json:"id" binding:"required"`
  16. CourseId int `form:"course_id" json:"course_id" binding:"required"`
  17. Cover string
  18. Title string `form:"title" json:"title" binding:"required"`
  19. Subtitle string `form:"subtitle" json:"subtitle" binding:"required"`
  20. Summary string `form:"summary" json:"summary" binding:"required"`
  21. Teacher int `form:"teacher" json:"teacher" binding:"required"`
  22. Lang string `form:"lang" json:"lang" binding:"required"`
  23. Speech_lang string `form:"speech_lang" json:"speech_lang" binding:"required"`
  24. Status int `form:"status" json:"status" binding:"required"`
  25. Content string `form:"content" json:"content" binding:"required"`
  26. Creator int
  27. StartDate time.Time `form:"date" json:"date" binding:"required"`
  28. Duration int64 `form:"duration" json:"duration" binding:"required"`
  29. Version int64
  30. CreatedAt time.Time
  31. UpdatedAt time.Time
  32. }
  33. //查询
  34. func GetLesson(db *pg.DB) gin.HandlerFunc {
  35. return func(c *gin.Context) {
  36. lid,err := strconv.ParseInt(c.Param("lid"),10,64)
  37. if err != nil {
  38. panic(err)
  39. }
  40. fmt.Println("get lesson")
  41. // TODO 在这里进行db操作
  42. // Select user by primary key.
  43. lesson := &Lesson{Id: int(lid)}
  44. err = db.Model(lesson).WherePK().Select()
  45. if err != nil {
  46. panic(err)
  47. }
  48. c.JSON(http.StatusOK, gin.H{
  49. "message": "lesson-"+lesson.Title,
  50. })
  51. }
  52. }
  53. //查询
  54. func GetLessonByTitle(db *pg.DB) gin.HandlerFunc {
  55. return func(c *gin.Context) {
  56. title:= c.Param("ltitle")
  57. // TODO 在这里进行db操作
  58. // Select user by primary key.
  59. var lessons []Lesson
  60. err := db.Model(&lessons).Column("id","title","subtitle").Where("title like ?",title+"%").Select()
  61. if err != nil {
  62. panic(err)
  63. }
  64. c.JSON(http.StatusOK, gin.H{
  65. "message": lessons,
  66. })
  67. }
  68. }
  69. /*新建
  70. 新建课以后,查询这个course 里有几个lesson 然后更新 courese 的 lesson_num
  71. */
  72. //PUT http://127.0.0.1:8080/api/lesson?title=lesson-one&course_id=1&status=10
  73. func PutLesson(db *pg.DB) gin.HandlerFunc{
  74. return func(c *gin.Context){
  75. title := c.Query("title")
  76. courseId,err := strconv.Atoi(c.Query("course_id"))
  77. status1,err := strconv.Atoi(c.Query("status"))
  78. if err != nil {
  79. panic(err)
  80. }
  81. newLesson := &Lesson{
  82. Title: title,
  83. CourseId: courseId,
  84. Status: status1,
  85. Teacher:0,
  86. Creator:1,
  87. }
  88. _, err = db.Model(newLesson).Insert()
  89. if err != nil {
  90. panic(err)
  91. }
  92. //修改 course 的 lesson_num
  93. courseMessage := _updateLessonCount(db,courseId)
  94. c.JSON(http.StatusOK,gin.H{
  95. "message":courseMessage,
  96. })
  97. }
  98. }
  99. func _updateLessonCount(db *pg.DB,courseId int) string{
  100. //查询这个course 里面有几个课程
  101. countLesson, err := db.Model((*Lesson)(nil)).Where("course_id = ?",courseId).Count()
  102. if err != nil {
  103. panic(err)
  104. }
  105. //修改course lesson number
  106. url := "http://127.0.0.1:8080/api/course/lessonnum"
  107. values := Course{
  108. Id : courseId,
  109. LessonNum : countLesson,
  110. }
  111. json_data, err := json.Marshal(values);
  112. if err != nil {
  113. panic(err)
  114. }
  115. client := &http.Client{}
  116. req, err := http.NewRequest("PATCH",url, bytes.NewBuffer(json_data))
  117. if err != nil {
  118. panic(err)
  119. }
  120. req.Header.Set("Content-Type","application/json")
  121. resp,err := client.Do(req)
  122. if err != nil {
  123. panic(err)
  124. }
  125. defer resp.Body.Close()
  126. b, err := ioutil.ReadAll(resp.Body)
  127. if err != nil {
  128. panic(err)
  129. }
  130. return(string(b))
  131. }
  132. //改
  133. func PostLesson(db *pg.DB) gin.HandlerFunc{
  134. return func(c *gin.Context){
  135. var form Lesson
  136. if err := c.ShouldBindJSON(&form); err != nil {
  137. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  138. return
  139. }
  140. _,err := db.Model(&form).Table("lessons").Column("title","subtitle","summary","teacher","tag","lang","speech_lang","status","content","start_date","duration").WherePK().Update()
  141. if err != nil {
  142. panic(err)
  143. }
  144. c.JSON(http.StatusOK,gin.H{
  145. "message":"update ok",
  146. })
  147. }
  148. }
  149. //删
  150. func DeleteLesson(db *pg.DB) gin.HandlerFunc{
  151. return func(c *gin.Context){
  152. id,err := strconv.ParseInt(c.Param("lid"),10,64)
  153. if err != nil {
  154. panic(err)
  155. }
  156. lesson := &Lesson{
  157. Id:int(id),
  158. CourseId: int(0) ,
  159. }
  160. //删之前获取 course_id
  161. err = db.Model(lesson).Column("course_id").WherePK().Select()
  162. if err != nil {
  163. panic(err)
  164. }
  165. course_id := lesson.CourseId
  166. _, err = db.Model(lesson).WherePK().Delete()
  167. if err != nil {
  168. panic(err)
  169. }
  170. //修改 course 的 lesson_num
  171. courseMessage := _updateLessonCount(db,course_id)
  172. c.JSON(http.StatusOK,gin.H{
  173. "message":"delete "+c.Param("lid"),
  174. "lesson_num":courseMessage,
  175. })
  176. }
  177. }