course.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package mint
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "github.com/go-pg/pg/v10"
  10. "github.com/go-redis/redis/v8"
  11. )
  12. type Course struct {
  13. Id int `form:"id" json:"id" binding:"required"`
  14. Uid string `form:"uid" json:"uid"`
  15. ParentId int `form:"parent_id" json:"parent_id"`
  16. PrParentId int `form:"pr_parent_id" json:"pr_parent_id"`
  17. Cover string
  18. Title string `form:"title" json:"title"`
  19. Subtitle string `form:"subtitle" json:"subtitle"`
  20. Summary string `form:"summary" json:"summary"`
  21. TeacherId int `form:"teacher" json:"teacher"`
  22. Lang string `form:"lang" json:"lang"`
  23. Speech_lang string `form:"speech_lang" json:"speech_lang"`
  24. LessonNum int `form:"lesson_num" json:"lesson_num"`
  25. StartAt time.Time `form:"start_at" json:"start_at"`
  26. EndAt time.Time `form:"end_at" json:"end_at"`
  27. Content string `form:"content" json:"content"`
  28. ContentType string `form:"content" json:"content_type"`
  29. Status string `form:"status" json:"status"`
  30. EditorId int `json:"editor_id"`
  31. StudioId int `json:"studio_id"`
  32. CreatorId int `json:"creator_id"`
  33. Version int
  34. DeletedAt time.Time
  35. CreatedAt time.Time
  36. UpdatedAt time.Time
  37. }
  38. func panicIf(err error) {
  39. if err != nil {
  40. panic(err)
  41. }
  42. }
  43. //查询display a list of all Courses
  44. func CoursesIndex(db *pg.DB) gin.HandlerFunc {
  45. return func(c *gin.Context) {
  46. //按照标题搜索,或者按照course列出子课程
  47. view := c.Query("view")
  48. studio := c.DefaultQuery("studio", "")
  49. status := c.DefaultQuery("status", "public")
  50. columns := "id , parent_id , title , subtitle , updated_at"
  51. var courses []Course
  52. var err error
  53. if studio == "" {
  54. switch view {
  55. case "title":
  56. title := c.Query("title")
  57. err = db.Model(&courses).ColumnExpr(columns).Where("title like ?", title+"%").Where("status = ?", "public").Select()
  58. case "course":
  59. err = db.Model(&courses).ColumnExpr(columns).Where("parent_id = 0").Where("status = ?", "public").Select()
  60. case "lessson":
  61. iCourseId := c.Query("courseid")
  62. err = db.Model(&courses).ColumnExpr(columns).Where("parent_id = ?", iCourseId).Where("status = ?", "public").Select()
  63. }
  64. } else {
  65. if status == "all" {
  66. // 列出 studio course 全部数据 需要有 member权限
  67. //TODO studio 鉴权
  68. switch view {
  69. case "title":
  70. title := c.Query("title")
  71. err = db.Model(&courses).ColumnExpr(columns).Where("title like ?", title+"%").Where("studio_id = ?", c.Query("studio")).Select()
  72. case "course":
  73. err = db.Model(&courses).ColumnExpr(columns).Where("parent_id = 0").Where("studio_id = ?", c.Query("studio")).Select()
  74. case "lessson":
  75. err = db.Model(&courses).ColumnExpr(columns).Where("parent_id = ?", c.Query("courseid")).Where("studio_id = ?", c.Query("studio")).Select()
  76. }
  77. } else {
  78. // 列出 studio course 全部公开数据
  79. switch view {
  80. case "title":
  81. title := c.Query("title")
  82. err = db.Model(&courses).ColumnExpr(columns).Where("title like ?", title+"%").Where("studio_id = ?", c.Query("studio")).Where("status = ?", "public").Select()
  83. case "course":
  84. err = db.Model(&courses).ColumnExpr(columns).Where("parent_id = 0").Where("studio_id = ?", c.Query("studio")).Where("status = ?", "public").Select()
  85. case "lessson":
  86. err = db.Model(&courses).ColumnExpr(columns).Where("parent_id = ?", c.Query("courseid")).Where("studio_id = ?", c.Query("studio")).Where("status = ?", "public").Select()
  87. }
  88. }
  89. }
  90. if err != nil {
  91. panic(err)
  92. }
  93. c.JSON(http.StatusOK, gin.H{
  94. "ok": true,
  95. "data": courses,
  96. })
  97. }
  98. }
  99. //return an HTML form for creating a new Courses
  100. func CoursesNew(db *pg.DB) gin.HandlerFunc {
  101. return func(c *gin.Context) {
  102. //TODO 业务逻辑
  103. c.HTML(http.StatusOK, "courses/new.html", gin.H{
  104. "message": "ok",
  105. })
  106. }
  107. }
  108. //新建create a new Courses
  109. func CoursesCreate(db *pg.DB) gin.HandlerFunc {
  110. return func(c *gin.Context) {
  111. var form Course
  112. if err := c.ShouldBindJSON(&form); err != nil {
  113. c.JSON(http.StatusBadRequest, gin.H{
  114. "ok": false,
  115. "error": err.Error(),
  116. })
  117. return
  118. }
  119. //TODO studio 鉴权
  120. if form.Title == "" {
  121. c.JSON(http.StatusBadRequest, gin.H{
  122. "ok": false,
  123. "error": ":no-title",
  124. })
  125. return
  126. }
  127. if form.ParentId != 0 {
  128. //子课程
  129. //先查询母课程是否存在
  130. countCourse, err := db.Model((*Course)(nil)).Where("id = ?", form.ParentId).Count()
  131. if err != nil {
  132. panic(err)
  133. }
  134. if countCourse == 0 {
  135. c.JSON(http.StatusBadRequest, gin.H{
  136. "ok": false,
  137. "error": ":no-parent-course",
  138. })
  139. return
  140. }
  141. }
  142. //TODO 获取 userid
  143. form.EditorId = 1
  144. form.CreatorId = 1
  145. _, err := db.Model(&form).Column("title", "status", "studio_id", "editor_id", "creator_id").Insert()
  146. panicIf(err)
  147. //子课程更新母课程LessonNum
  148. if form.ParentId != 0 {
  149. //获取课程数量
  150. countLesson, err := db.Model((*Course)(nil)).Where("parent_id = ?", form.ParentId).Count()
  151. if err != nil {
  152. panic(err)
  153. }
  154. parentCourse := Course{Id: form.ParentId, LessonNum: countLesson}
  155. _, err = db.Model(&parentCourse).Column("lesson_num").WherePK().Update()
  156. if err != nil {
  157. panic(err)
  158. }
  159. }
  160. c.JSON(http.StatusOK, gin.H{
  161. "ok": true,
  162. "data": form,
  163. })
  164. }
  165. }
  166. //查询display a specific Courses
  167. func CoursesShow(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  168. return func(c *gin.Context) {
  169. id, err := strconv.Atoi(c.Param("id"))
  170. if err != nil {
  171. panic(err)
  172. }
  173. rkey := "course/:id"
  174. n, err := rdb.HExists(ctx, rkey, c.Param("id")).Result()
  175. if err == nil && n {
  176. val, err := rdb.HGet(ctx, rkey, c.Param("id")).Result()
  177. if err == nil {
  178. var redisData Course
  179. json.Unmarshal([]byte(val), &redisData)
  180. c.JSON(http.StatusOK, gin.H{
  181. "status": "success",
  182. "data": redisData,
  183. })
  184. return
  185. } else {
  186. //有错误
  187. fmt.Println("redis error")
  188. }
  189. } else {
  190. fmt.Println("redis error or key not exist")
  191. }
  192. // Select by primary key.
  193. course := &Course{Id: id}
  194. err = db.Model(course).WherePK().First()
  195. if err != nil {
  196. panic(err)
  197. }
  198. c.JSON(http.StatusOK, gin.H{
  199. "ok": true,
  200. "data": course,
  201. })
  202. //写入redis
  203. jsonData, err := json.Marshal(course)
  204. if err == nil {
  205. rdb.HSet(ctx, rkey, c.Param("id"), string(jsonData))
  206. }
  207. }
  208. }
  209. //return an HTML form for edit a Courses
  210. func CoursesEdit(db *pg.DB) gin.HandlerFunc {
  211. return func(c *gin.Context) {
  212. //TODO 业务逻辑
  213. c.HTML(http.StatusOK, "courses/edit.html", gin.H{
  214. "message": "ok",
  215. })
  216. }
  217. }
  218. //update a specific Courses
  219. func CoursesUpdate(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  220. return func(c *gin.Context) {
  221. var form Course
  222. if err := c.ShouldBindJSON(&form); err != nil {
  223. c.JSON(http.StatusBadRequest, gin.H{
  224. "ok": false,
  225. "error": err.Error(),
  226. })
  227. return
  228. }
  229. _, err := db.Model(&form).Column("title", "subtitle", "summary", "teacher_id", "lang", "speech_lang", "status", "content", "content_type", "start_at", "end_at").WherePK().Update()
  230. if err != nil {
  231. panic(err)
  232. }
  233. c.JSON(http.StatusOK, gin.H{
  234. "ok": true,
  235. "data": form,
  236. })
  237. //delete redis
  238. rkey := "course/:id"
  239. rdb.HDel(ctx, rkey, c.Param("id"))
  240. }
  241. }
  242. //删
  243. //delete a specific Courses
  244. func CoursesDestroy(db *pg.DB, rdb *redis.Client) gin.HandlerFunc {
  245. return func(c *gin.Context) {
  246. id, err := strconv.Atoi(c.Param("id"))
  247. if err != nil {
  248. panic(err)
  249. }
  250. course := &Course{
  251. Id: id,
  252. }
  253. _, err = db.Model(course).WherePK().Where("parent_id = ?", id).Delete()
  254. if err != nil {
  255. panic(err)
  256. }
  257. rkey := "course/:id"
  258. rdb.HDel(ctx, rkey, c.Param("id"))
  259. c.JSON(http.StatusOK, gin.H{
  260. "ok": true,
  261. "data": course,
  262. })
  263. }
  264. }