material.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package daytask
  2. import (
  3. "app/commons/model/entity"
  4. "app/commons/services"
  5. "github.com/gin-gonic/gin"
  6. )
  7. // MaterialCategories 素材分类列表
  8. func (s *Server) MaterialCategories(c *gin.Context) {
  9. ctx := s.FromContext(c)
  10. db := s.DB()
  11. var categories []entity.DtMaterialCategory
  12. db.Where("status = ?", 1).
  13. Order("sort ASC, id ASC").
  14. Find(&categories)
  15. ctx.OK(gin.H{
  16. "list": categories,
  17. })
  18. }
  19. // MaterialList 素材列表
  20. func (s *Server) MaterialList(c *gin.Context) {
  21. ctx := s.FromContext(c)
  22. db := s.DB()
  23. categoryId := ctx.QueryInt64("categoryId", 0)
  24. paging := &services.Pagination{
  25. Current: ctx.QueryInt64("current", 1),
  26. Size: ctx.QueryInt64("size", 20),
  27. }
  28. query := db.Model(&entity.DtMaterial{}).Where("status = ?", 1)
  29. if categoryId > 0 {
  30. query = query.Where("category_id = ?", categoryId)
  31. }
  32. query.Count(&paging.Total)
  33. paging.Computer()
  34. type MaterialInfo struct {
  35. Id int64 `json:"id"`
  36. Title string `json:"title"`
  37. TitleVi string `json:"titleVi"`
  38. Type string `json:"type"`
  39. Icon string `json:"icon"`
  40. Images string `json:"images"`
  41. Videos string `json:"videos"`
  42. TextContent string `json:"textContent"`
  43. ViewCount int `json:"viewCount"`
  44. CreatedAt int64 `json:"createdAt"`
  45. }
  46. materials := make([]*MaterialInfo, 0)
  47. query.Select("id, title, title_vi, type, icon, images, videos, text_content, view_count, created_at").
  48. Order("sort ASC, id DESC").
  49. Offset(int(paging.Start)).
  50. Limit(int(paging.Size)).
  51. Scan(&materials)
  52. ctx.OK(gin.H{
  53. "list": materials,
  54. "paging": paging,
  55. })
  56. }
  57. // MaterialDetail 素材详情
  58. func (s *Server) MaterialDetail(c *gin.Context) {
  59. ctx := s.FromContext(c)
  60. db := s.DB()
  61. type Req struct {
  62. Id int64 `json:"id" form:"id" binding:"required"`
  63. }
  64. var req Req
  65. if err := c.ShouldBind(&req); err != nil {
  66. ctx.Fail("invalid_params")
  67. return
  68. }
  69. var material entity.DtMaterial
  70. if err := db.Where("id = ? AND status = ?", req.Id, 1).First(&material).Error; err != nil {
  71. ctx.Fail("material_not_found")
  72. return
  73. }
  74. // 更新浏览次数
  75. db.Model(&entity.DtMaterial{}).Where("id = ?", req.Id).
  76. Update("view_count", material.ViewCount+1)
  77. ctx.OK(gin.H{
  78. "id": material.Id,
  79. "title": material.Title,
  80. "titleVi": material.TitleVi,
  81. "type": material.Type,
  82. "icon": material.Icon,
  83. "images": material.Images,
  84. "videos": material.Videos,
  85. "textContent": material.TextContent,
  86. "viewCount": material.ViewCount + 1,
  87. "createdAt": material.CreatedAt,
  88. })
  89. }
  90. // MaterialLike 素材点赞 - 由于实体没有likeCount字段,改为增加浏览次数
  91. func (s *Server) MaterialLike(c *gin.Context) {
  92. ctx := s.FromContext(c)
  93. db := s.DB()
  94. type Req struct {
  95. Id int64 `json:"id" form:"id" binding:"required"`
  96. }
  97. var req Req
  98. if err := c.ShouldBind(&req); err != nil {
  99. ctx.Fail("invalid_params")
  100. return
  101. }
  102. var material entity.DtMaterial
  103. if err := db.Where("id = ? AND status = ?", req.Id, 1).First(&material).Error; err != nil {
  104. ctx.Fail("material_not_found")
  105. return
  106. }
  107. // 更新浏览次数(用作点赞计数)
  108. db.Model(&entity.DtMaterial{}).Where("id = ?", req.Id).
  109. Update("view_count", material.ViewCount+1)
  110. ctx.OK(gin.H{
  111. "viewCount": material.ViewCount + 1,
  112. })
  113. }