Skip to content

Commit 40107da

Browse files
authored
feat: 获取提案详情 (#54)
1 parent c98ca6e commit 40107da

File tree

4 files changed

+88
-11
lines changed

4 files changed

+88
-11
lines changed

api/handler/proposal.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,22 @@ func ListProposals(c *gin.Context) {
6767

6868
// GetProposal 获取提案详情
6969
// @router /api/proposal/:id [GET]
70+
// @Summary 获取提案详情
71+
// @Description 根据提案ID查询提案完整信息
72+
// @Tags proposal
73+
// @Produce json
74+
// @Param id path string true "提案ID"
75+
// @Success 200 {object} dto.GetProposalDetailResp
7076
func GetProposal(c *gin.Context) {
71-
// TODO: not implemented
77+
var req dto.GetProposalReq
78+
var resp *dto.GetProposalResp
79+
var err error
80+
81+
req.TargetID = c.Param(consts.CtxProposalID)
82+
c.Set(consts.CtxUserID, token.GetUserID(c))
83+
84+
resp, err = provider.Get().ProposalService.GetProposal(c, &req)
85+
PostProcess(c, &req, resp, err)
7286
}
7387

7488
// ApproveProposal 审批提案

application/dto/proposal.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ type CreateProposalResp struct {
3131
}
3232

3333
type ProposalVO struct {
34-
ID string `json:"id"`
35-
UserID string `json:"userId"`
36-
Title string `json:"title"`
37-
Content string `json:"content"`
38-
Status string `json:"status"` // pending / approved / rejected
39-
Deleted bool `json:"deleted"`
40-
Course *CourseVO `json:"course"`
34+
ID string `json:"id"`
35+
UserID string `json:"userId"`
36+
Title string `json:"title"`
37+
Content string `json:"content"`
38+
Status string `json:"status"` // pending / approved / rejected
39+
Deleted bool `json:"deleted"`
40+
Course *CourseVO `json:"course"`
4141
CreatedAt time.Time `json:"createdAt"`
4242
UpdatedAt time.Time `json:"updatedAt"`
4343
}
@@ -51,6 +51,10 @@ type ToggleProposalReq struct {
5151
TargetID string `json:"targetId"`
5252
}
5353

54+
type GetProposalReq struct {
55+
TargetID string `json:"proposalId"`
56+
}
57+
5458
// ListProposalResp 对应 /api/proposal/list 的响应体
5559
type ListProposalResp struct {
5660
*Resp
@@ -63,3 +67,8 @@ type ToggleProposalResp struct {
6367
ProposalCnt int64 `json:"proposalCnt"`
6468
*Resp
6569
}
70+
71+
type GetProposalResp struct {
72+
*Resp
73+
Proposal *ProposalVO `json:"proposal"`
74+
}

application/service/proposal.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type IProposalService interface {
3939
CreateProposal(ctx context.Context, req *dto.CreateProposalReq) (*dto.CreateProposalResp, error)
4040
ToggleProposal(ctx context.Context, req *dto.ToggleProposalReq) (resp *dto.ToggleProposalResp, err error)
4141
ListProposals(ctx context.Context, req *dto.ListProposalReq) (*dto.ListProposalResp, error)
42+
GetProposal(ctx context.Context, req *dto.GetProposalReq) (resp *dto.GetProposalResp, err error)
4243
}
4344

4445
type ProposalService struct {
@@ -167,7 +168,7 @@ func (s *ProposalService) ToggleProposal(ctx context.Context, req *dto.TogglePro
167168
}
168169

169170
// 获取新的总投票数
170-
proposalCount, err := s.ProposalRepo.CountProposalByTarget(ctx, req.TargetID, consts.ProposalType)
171+
proposalCount, err := s.ProposalRepo.CountByTarget(ctx, req.TargetID, consts.ProposalType)
171172
if err != nil {
172173
logs.CtxWarnf(ctx, "[ProposalRepo] [CountByTarget] error: %v", err)
173174
return nil, errorx.WrapByCode(err, errno.ErrProposalCountFailed,
@@ -211,3 +212,36 @@ func (s *ProposalService) ListProposals(ctx context.Context, req *dto.ListPropos
211212
Proposals: vos,
212213
}, nil
213214
}
215+
216+
// GetProposalDetail 获取提案详情
217+
func (s *ProposalService) GetProposal(ctx context.Context, req *dto.GetProposalReq) (resp *dto.GetProposalResp, err error) {
218+
// 鉴权
219+
userId, ok := ctx.Value(consts.CtxUserID).(string)
220+
if !ok || userId == "" {
221+
return nil, errorx.New(errno.ErrUserNotLogin)
222+
}
223+
224+
// 查询提案详情
225+
proposalId := req.ProposalID
226+
proposal, err := s.ProposalRepo.FindByID(ctx, proposalId)
227+
if err != nil {
228+
logs.CtxErrorf(ctx, "[ProposalRepo] [FindProposalByID] error: %v, proposalId: %s", err, proposalId)
229+
return nil, errorx.WrapByCode(err, errno.ErrProposalFindFailed, errorx.KV("proposalId", proposalId))
230+
}
231+
if proposal == nil {
232+
return nil, errorx.WrapByCode(err, errno.ErrProposalFindFailed, errorx.KV("proposalId", proposalId))
233+
}
234+
235+
// 转换为VO
236+
vo, err := s.ProposalAssembler.ToProposalVO(ctx, proposal)
237+
if err != nil {
238+
logs.CtxErrorf(ctx, "[ProposalAssembler] [ToProposalVO] error: %v, proposalId: %s", err, proposalId)
239+
return nil, errorx.WrapByCode(err, errno.ErrProposalCvtFailed, errorx.KV("proposalId", proposalId))
240+
}
241+
242+
// 构造响应
243+
return &dto.GetProposalResp{
244+
Resp: dto.Success(),
245+
Proposal: vo,
246+
}, nil
247+
}

infra/repo/proposal.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ type IProposalRepo interface {
4343
FindMany(ctx context.Context, param *dto.PageParam) ([]*model.Proposal, int64, error)
4444
Toggle(ctx context.Context, userId, targetId string, targetType int32) (bool, error)
4545
IsProposal(ctx context.Context, userId, targetId string, targetType int32) (bool, error)
46-
CountProposalByTarget(ctx context.Context, targetId string, targetType int32) (int64, error)
46+
CountByTarget(ctx context.Context, targetId string, targetType int32) (int64, error)
47+
FindByID(ctx context.Context, proposalID string) (*model.Proposal, error) // 修改方法名
4748
}
4849

4950
type ProposalRepo struct {
@@ -150,9 +151,28 @@ func (r *ProposalRepo) IsProposal(ctx context.Context, userId, targetId string,
150151
}
151152

152153
// CountProposalByTarget 获得目标的总投票数
153-
func (r *ProposalRepo) CountProposalByTarget(ctx context.Context, targetId string, targetType int32) (int64, error) {
154+
func (r *ProposalRepo) CountByTarget(ctx context.Context, targetId string, targetType int32) (int64, error) {
154155
return r.conn.CountDocuments(ctx, bson.M{
155156
consts.TargetID: targetId,
156157
consts.Active: bson.M{"$ne": false},
157158
})
158159
}
160+
161+
// FindProposalByID 根据提案ID查询单个未删除的提案
162+
func (r *ProposalRepo) FindByID(ctx context.Context, proposalID string) (*model.Proposal, error) {
163+
var proposal model.Proposal
164+
165+
filter := bson.M{
166+
consts.ID: proposalID,
167+
consts.Deleted: bson.M{"$ne": true},
168+
}
169+
170+
err := r.conn.FindOneNoCache(ctx, &proposal, filter, nil)
171+
if err != nil {
172+
if err == monc.ErrNotFound {
173+
return nil, nil
174+
}
175+
return nil, err
176+
}
177+
return &proposal, nil
178+
}

0 commit comments

Comments
 (0)