@@ -38,7 +38,8 @@ var _ IProposalService = (*ProposalService)(nil)
3838type IProposalService interface {
3939 CreateProposal (ctx context.Context , req * dto.CreateProposalReq ) (* dto.CreateProposalResp , error )
4040 ListProposals (ctx context.Context , req * dto.ListProposalReq ) (* dto.ListProposalResp , error )
41- GetProposal (ctx context.Context , req * dto.GetProposalReq ) (resp * dto.GetProposalResp , err error )
41+ GetProposal (ctx context.Context , req * dto.GetProposalReq ) (* dto.GetProposalResp , error )
42+ DeleteProposal (ctx context.Context , req * dto.DeleteProposalReq ) (* dto.DeleteProposalResp , error )
4243 UpdateProposal (ctx context.Context , req * dto.UpdateProposalReq ) (* dto.UpdateProposalResp , error )
4344}
4445
@@ -49,6 +50,7 @@ type ProposalService struct {
4950 ProposalAssembler * assembler.ProposalAssembler
5051 LikeRepo * repo.LikeRepo
5152 LikeCache * cache.LikeCache
53+ UserRepo * repo.UserRepo
5254}
5355
5456var ProposalServiceSet = wire .NewSet (
@@ -185,7 +187,7 @@ func (s *ProposalService) ListProposals(ctx context.Context, req *dto.ListPropos
185187}
186188
187189// GetProposal 获取提案详情
188- func (s * ProposalService ) GetProposal (ctx context.Context , req * dto.GetProposalReq ) (resp * dto.GetProposalResp , err error ) {
190+ func (s * ProposalService ) GetProposal (ctx context.Context , req * dto.GetProposalReq ) (* dto.GetProposalResp , error ) {
189191 // 鉴权
190192 userId , ok := ctx .Value (consts .CtxUserID ).(string )
191193 if ! ok || userId == "" {
@@ -218,13 +220,68 @@ func (s *ProposalService) GetProposal(ctx context.Context, req *dto.GetProposalR
218220 }, nil
219221}
220222
223+ // DeleteProposal 删除提案
224+ func (s * ProposalService ) DeleteProposal (ctx context.Context , req * dto.DeleteProposalReq ) (* dto.DeleteProposalResp , error ) {
225+ // 鉴权
226+ userId , ok := ctx .Value (consts .CtxUserID ).(string )
227+ if ! ok || userId == "" {
228+ return nil , errorx .New (errno .ErrUserNotLogin )
229+ }
230+
231+ proposalId := req .ProposalID
232+
233+ // 检查提案是否存在
234+ proposal , err := s .ProposalRepo .FindByID (ctx , proposalId )
235+ if err != nil {
236+ logs .CtxErrorf (ctx , "[ProposalRepo] [FindByID] error: %v, proposalId: %s" , err , proposalId )
237+ return nil , errorx .WrapByCode (err , errno .ErrProposalFindFailed )
238+ }
239+ if proposal == nil {
240+ logs .CtxWarnf (ctx , "[ProposalRepo] [FindByID] proposal not found, proposalId: %s" , proposalId )
241+ return nil , errorx .New (errno .ErrProposalNotFound , errorx .KV ("key" , consts .ReqProposalID ), errorx .KV ("value" , proposalId ))
242+ }
243+
244+ //权限检查:非管理员只能删除自己的提案
245+ if proposal .UserID != userId {
246+ // 查询用户是否是管理员
247+ isAdmin , err := s .UserRepo .IsAdminByID (ctx , userId )
248+ if err != nil {
249+ logs .CtxErrorf (ctx , "[UserRepo] [GetByID] error: %v, userId: %s" , err , userId )
250+ return nil , errorx .New (errno .ErrUserNotAdmin ,
251+ errorx .KV ("id" , userId ))
252+ }
253+
254+ if ! isAdmin {
255+ return nil , errorx .New (errno .ErrUserNotOwner ,
256+ errorx .KV ("id" , userId ))
257+ }
258+ }
259+
260+ //执行删除提案
261+ err = s .ProposalRepo .DeleteProposal (ctx , proposalId , userId )
262+ if err != nil {
263+ logs .CtxErrorf (ctx , "[ProposalRepo] [Delete] error: %v" , err )
264+ return nil , errorx .WrapByCode (err , errno .ErrProposalDeleteFailed ,
265+ errorx .KV ("proposal_id" , proposalId ))
266+ }
267+
268+ return & dto.DeleteProposalResp {
269+ Resp : dto .Success (),
270+ ProposalID : req .ProposalID ,
271+ DeletedAt : time .Now (),
272+ OperatorID : userId ,
273+ Deleted : true ,
274+ }, nil
275+ }
276+
221277// UpdateProposal 更新提案
222278func (s * ProposalService ) UpdateProposal (ctx context.Context , req * dto.UpdateProposalReq ) (* dto.UpdateProposalResp , error ) {
223- //鉴权
279+ // 鉴权
224280 userId , ok := ctx .Value (consts .CtxUserID ).(string )
225281 if ! ok || userId == "" {
226282 return nil , errorx .New (errno .ErrUserNotLogin )
227283 }
284+
228285 //查询提案
229286 proposal , err := s .ProposalRepo .FindByID (ctx , req .ProposalID )
230287 if err != nil {
0 commit comments