@@ -19,22 +19,25 @@ import (
1919
2020 "github.com/Boyuan-IT-Club/Meowpick-Backend/application/dto"
2121 "github.com/Boyuan-IT-Club/Meowpick-Backend/infra/model"
22+ "github.com/Boyuan-IT-Club/Meowpick-Backend/infra/repo"
2223 "github.com/Boyuan-IT-Club/Meowpick-Backend/infra/util/mapping"
24+ "github.com/Boyuan-IT-Club/Meowpick-Backend/types/consts"
2325 "github.com/Boyuan-IT-Club/go-kit/logs"
2426 "github.com/google/wire"
2527)
2628
2729var _ IProposalAssembler = (* ProposalAssembler )(nil )
2830
2931type IProposalAssembler interface {
30- ToProposalVO (ctx context.Context , db * model.Proposal ) (* dto.ProposalVO , error )
31- ToProposalVOArray (ctx context.Context , dbs []* model.Proposal ) ([]* dto.ProposalVO , error )
32+ ToProposalVO (ctx context.Context , db * model.Proposal , userId string ) (* dto.ProposalVO , error )
33+ ToProposalVOArray (ctx context.Context , dbs []* model.Proposal , userId string ) ([]* dto.ProposalVO , error )
3234 ToProposalDB (ctx context.Context , vo * dto.ProposalVO ) (* model.Proposal , error )
3335 ToProposalDBArray (ctx context.Context , vos []* dto.ProposalVO ) ([]* model.Proposal , error )
3436}
3537
3638type ProposalAssembler struct {
3739 CourseAssembler * CourseAssembler
40+ LikeRepo * repo.LikeRepo
3841}
3942
4043var ProposalAssemblerSet = wire .NewSet (
@@ -43,7 +46,7 @@ var ProposalAssemblerSet = wire.NewSet(
4346)
4447
4548// ToProposalVO 单个ProposalDB转ProposalVO (DB to VO)
46- func (a * ProposalAssembler ) ToProposalVO (ctx context.Context , db * model.Proposal ) (* dto.ProposalVO , error ) {
49+ func (a * ProposalAssembler ) ToProposalVO (ctx context.Context , db * model.Proposal , userId string ) (* dto.ProposalVO , error ) {
4750 var courseVO * dto.CourseVO
4851 if db .Course != nil {
4952 var err error
@@ -54,37 +57,100 @@ func (a *ProposalAssembler) ToProposalVO(ctx context.Context, db *model.Proposal
5457 }
5558 }
5659
60+ // 获得点赞目标类型
61+ targetType := mapping .Data .GetLikeTargetTypeIDByName (consts .LikeTargetTypeProposal )
62+
63+ // 获取点赞信息
64+ likeCnt , err := a .LikeRepo .CountByTarget (ctx , db .ID , targetType )
65+ if err != nil {
66+ logs .CtxErrorf (ctx , "[LikeRepo] [CountByID] error: %v" , err )
67+ return nil , err
68+ }
69+
70+ // 这里的userId是查看评论的用户
71+ active , err := a .LikeRepo .IsLike (ctx , userId , db .ID , targetType )
72+ if err != nil {
73+ logs .CtxErrorf (ctx , "[LikeRepo] [IsLike] error: %v" , err )
74+ return nil , err
75+ }
76+
5777 return & dto.ProposalVO {
58- ID : db .ID ,
59- UserID : db .UserID ,
60- Title : db .Title ,
61- Content : db .Content ,
62- Course : courseVO ,
63- Status : mapping .Data .GetProposalStatusNameByID (db .Status ),
64- Deleted : db .Deleted ,
65- AgreeCnt : db .AgreeCnt ,
78+ ID : db .ID ,
79+ UserID : db .UserID ,
80+ Title : db .Title ,
81+ Content : db .Content ,
82+ Course : courseVO ,
83+ Status : mapping .Data .GetProposalStatusNameByID (db .Status ),
84+ Deleted : db .Deleted ,
85+ LikeVO : & dto.LikeVO {
86+ Like : active ,
87+ LikeCnt : likeCnt ,
88+ },
6689 CreatedAt : db .CreatedAt ,
6790 UpdatedAt : db .UpdatedAt ,
6891 }, nil
6992}
7093
7194// ToProposalVOArray ProposalDB数组转ProposalVO数组 (DB Array to VO Array)
72- func (a * ProposalAssembler ) ToProposalVOArray (ctx context.Context , dbs []* model.Proposal ) ([]* dto.ProposalVO , error ) {
95+ func (a * ProposalAssembler ) ToProposalVOArray (ctx context.Context , dbs []* model.Proposal , userId string ) ([]* dto.ProposalVO , error ) {
7396 if len (dbs ) == 0 {
7497 logs .CtxWarnf (ctx , "[ProposalAssembler] [ToProposalVOArray] empty proposal db array" )
7598 return []* dto.ProposalVO {}, nil
7699 }
77100
101+ // 提取所有 proposalIds
102+ ids := make ([]string , len (dbs ))
103+ for i , db := range dbs {
104+ ids [i ] = db .ID
105+ }
106+
107+ // 获得点赞目标类型
108+ targetType := mapping .Data .GetLikeTargetTypeIDByName (consts .LikeTargetTypeProposal )
109+
110+ // 批量获取点赞数
111+ likeCntMap , err := a .LikeRepo .CountByTargets (ctx , ids , targetType )
112+ if err != nil {
113+ logs .CtxErrorf (ctx , "[LikeRepo] [CountByTargets] error: %v" , err )
114+ return nil , err
115+ }
116+
117+ // 批量获取点赞状态
118+ likeStatusMap , err := a .LikeRepo .GetLikesByUserIDAndTargets (ctx , userId , ids , targetType )
119+ if err != nil {
120+ logs .CtxErrorf (ctx , "[LikeRepo] [GetLikesByUserIDAndTargets] error: %v" , err )
121+ return nil , err
122+ }
123+
124+ // 构建结果
78125 vos := make ([]* dto.ProposalVO , 0 , len (dbs ))
79126 for _ , db := range dbs {
80- vo , err := a .ToProposalVO (ctx , db )
81- if err != nil {
82- logs .CtxErrorf (ctx , "[ProposalAssembler] [ToProposalVO] error: %v" , err )
83- return nil , err
127+ // 从批量查询结果中获取点赞信息
128+ likeCnt := likeCntMap [db .ID ] // 如果不存在则为0
129+ active := likeStatusMap [db .ID ] // 如果不存在则为false
130+ var courseVO * dto.CourseVO
131+ if db .Course != nil {
132+ courseVO , err = a .CourseAssembler .ToCourseVO (ctx , db .Course )
133+ if err != nil {
134+ logs .CtxErrorf (ctx , "[CourseAssembler] [ToCourseVO] error: %v" , err )
135+ return nil , err
136+ }
84137 }
85- if vo != nil {
86- vos = append (vos , vo )
138+ proposalVO := & dto.ProposalVO {
139+ ID : db .ID ,
140+ Content : db .Content ,
141+ Title : db .Title ,
142+ UserID : db .UserID ,
143+ Status : mapping .Data .GetProposalStatusNameByID (db .Status ),
144+ Deleted : db .Deleted ,
145+ LikeVO : & dto.LikeVO {
146+ Like : active ,
147+ LikeCnt : likeCnt ,
148+ },
149+ Course : courseVO ,
150+ CreatedAt : db .CreatedAt ,
151+ UpdatedAt : db .UpdatedAt ,
87152 }
153+ vos = append (vos , proposalVO )
88154 }
89155
90156 return vos , nil
@@ -110,7 +176,6 @@ func (a *ProposalAssembler) ToProposalDB(ctx context.Context, vo *dto.ProposalVO
110176 Course : courseDB ,
111177 Status : mapping .Data .GetProposalStatusIDByName (vo .Status ),
112178 Deleted : vo .Deleted ,
113- AgreeCnt : vo .AgreeCnt ,
114179 CreatedAt : vo .CreatedAt ,
115180 UpdatedAt : vo .UpdatedAt ,
116181 }, nil
0 commit comments