| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package daytask
- import (
- "app/commons/model/entity"
- "app/commons/services"
- "github.com/gin-gonic/gin"
- )
- // MaterialCategories 素材分类列表
- func (s *Server) MaterialCategories(c *gin.Context) {
- ctx := s.FromContext(c)
- db := s.DB()
- var categories []entity.DtMaterialCategory
- db.Where("status = ?", 1).
- Order("sort ASC, id ASC").
- Find(&categories)
- ctx.OK(gin.H{
- "list": categories,
- })
- }
- // MaterialList 素材列表
- func (s *Server) MaterialList(c *gin.Context) {
- ctx := s.FromContext(c)
- db := s.DB()
- categoryId := ctx.QueryInt64("categoryId", 0)
- paging := &services.Pagination{
- Current: ctx.QueryInt64("current", 1),
- Size: ctx.QueryInt64("size", 20),
- }
- query := db.Model(&entity.DtMaterial{}).Where("status = ?", 1)
- if categoryId > 0 {
- query = query.Where("category_id = ?", categoryId)
- }
- query.Count(&paging.Total)
- paging.Computer()
- type MaterialInfo struct {
- Id int64 `json:"id"`
- Title string `json:"title"`
- TitleVi string `json:"titleVi"`
- Type string `json:"type"`
- Icon string `json:"icon"`
- Images string `json:"images"`
- Videos string `json:"videos"`
- TextContent string `json:"textContent"`
- ViewCount int `json:"viewCount"`
- CreatedAt int64 `json:"createdAt"`
- }
- materials := make([]*MaterialInfo, 0)
- query.Select("id, title, title_vi, type, icon, images, videos, text_content, view_count, created_at").
- Order("sort ASC, id DESC").
- Offset(int(paging.Start)).
- Limit(int(paging.Size)).
- Scan(&materials)
- ctx.OK(gin.H{
- "list": materials,
- "paging": paging,
- })
- }
- // MaterialDetail 素材详情
- func (s *Server) MaterialDetail(c *gin.Context) {
- ctx := s.FromContext(c)
- db := s.DB()
- type Req struct {
- Id int64 `json:"id" form:"id" binding:"required"`
- }
- var req Req
- if err := c.ShouldBind(&req); err != nil {
- ctx.Fail("invalid_params")
- return
- }
- var material entity.DtMaterial
- if err := db.Where("id = ? AND status = ?", req.Id, 1).First(&material).Error; err != nil {
- ctx.Fail("material_not_found")
- return
- }
- // 更新浏览次数
- db.Model(&entity.DtMaterial{}).Where("id = ?", req.Id).
- Update("view_count", material.ViewCount+1)
- ctx.OK(gin.H{
- "id": material.Id,
- "title": material.Title,
- "titleVi": material.TitleVi,
- "type": material.Type,
- "icon": material.Icon,
- "images": material.Images,
- "videos": material.Videos,
- "textContent": material.TextContent,
- "viewCount": material.ViewCount + 1,
- "createdAt": material.CreatedAt,
- })
- }
- // MaterialLike 素材点赞 - 由于实体没有likeCount字段,改为增加浏览次数
- func (s *Server) MaterialLike(c *gin.Context) {
- ctx := s.FromContext(c)
- db := s.DB()
- type Req struct {
- Id int64 `json:"id" form:"id" binding:"required"`
- }
- var req Req
- if err := c.ShouldBind(&req); err != nil {
- ctx.Fail("invalid_params")
- return
- }
- var material entity.DtMaterial
- if err := db.Where("id = ? AND status = ?", req.Id, 1).First(&material).Error; err != nil {
- ctx.Fail("material_not_found")
- return
- }
- // 更新浏览次数(用作点赞计数)
- db.Model(&entity.DtMaterial{}).Where("id = ?", req.Id).
- Update("view_count", material.ViewCount+1)
- ctx.OK(gin.H{
- "viewCount": material.ViewCount + 1,
- })
- }
|