@@ -230,15 +230,13 @@ func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) (
230230}
231231
232232func (as * AnswerService ) Update (ctx context.Context , req * schema.AnswerUpdateReq ) (string , error ) {
233- //req.NoNeedReview //true 不需要审核
234233 var canUpdate bool
235234 _ , existUnreviewed , err := as .revisionService .ExistUnreviewedByObjectID (ctx , req .ID )
236235 if err != nil {
237236 return "" , err
238237 }
239238 if existUnreviewed {
240- err = errors .BadRequest (reason .AnswerCannotUpdate )
241- return "" , err
239+ return "" , errors .BadRequest (reason .AnswerCannotUpdate )
242240 }
243241
244242 questionInfo , exist , err := as .questionRepo .GetQuestion (ctx , req .QuestionID )
@@ -254,28 +252,25 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
254252 return "" , err
255253 }
256254 if ! exist {
257- return "" , nil
255+ return "" , errors . BadRequest ( reason . AnswerNotFound )
258256 }
259257
260258 if answerInfo .Status == entity .AnswerStatusDeleted {
261- err = errors .BadRequest (reason .AnswerCannotUpdate )
262- return "" , err
259+ return "" , errors .BadRequest (reason .AnswerCannotUpdate )
263260 }
264261
265262 //If the content is the same, ignore it
266263 if answerInfo .OriginalText == req .Content {
267264 return "" , nil
268265 }
269266
270- now := time .Now ()
271- insertData := new (entity.Answer )
267+ insertData := & entity.Answer {}
272268 insertData .ID = req .ID
273269 insertData .UserID = answerInfo .UserID
274270 insertData .QuestionID = req .QuestionID
275271 insertData .OriginalText = req .Content
276272 insertData .ParsedText = req .HTML
277- insertData .UpdatedAt = now
278-
273+ insertData .UpdatedAt = time .Now ()
279274 insertData .LastEditUserID = "0"
280275 if answerInfo .UserID != req .UserID {
281276 insertData .LastEditUserID = req .UserID
@@ -284,7 +279,6 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
284279 revisionDTO := & schema.AddRevisionDTO {
285280 UserID : req .UserID ,
286281 ObjectID : req .ID ,
287- Title : "" ,
288282 Log : req .EditSummary ,
289283 }
290284
@@ -314,7 +308,7 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
314308 }
315309 if canUpdate {
316310 as .activityQueueService .Send (ctx , & schema.ActivityMsg {
317- UserID : insertData .UserID ,
311+ UserID : req .UserID ,
318312 ObjectID : insertData .ID ,
319313 OriginalObjectID : insertData .ID ,
320314 ActivityTypeKey : constant .ActAnswerEdited ,
@@ -325,65 +319,55 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
325319 return insertData .ID , nil
326320}
327321
328- // UpdateAccepted
329- func (as * AnswerService ) UpdateAccepted (ctx context.Context , req * schema.AnswerAcceptedReq ) error {
330- if req .AnswerID == "" {
331- req .AnswerID = "0"
332- }
333- if req .UserID == "" {
334- return nil
335- }
336-
337- newAnswerInfo := & entity.Answer {}
338- newAnswerInfoexist := false
339- var err error
340-
341- if req .AnswerID != "0" {
342- newAnswerInfo , newAnswerInfoexist , err = as .answerRepo .GetByID (ctx , req .AnswerID )
343- if err != nil {
344- return err
345- }
346- newAnswerInfo .ID = uid .DeShortID (newAnswerInfo .ID )
347- if ! newAnswerInfoexist {
348- return errors .BadRequest (reason .AnswerNotFound )
349- }
350- }
351-
322+ // AcceptAnswer accept answer
323+ func (as * AnswerService ) AcceptAnswer (ctx context.Context , req * schema.AcceptAnswerReq ) (err error ) {
324+ // find question
352325 questionInfo , exist , err := as .questionRepo .GetQuestion (ctx , req .QuestionID )
353326 if err != nil {
354327 return err
355328 }
356- questionInfo .ID = uid .DeShortID (questionInfo .ID )
357329 if ! exist {
358330 return errors .BadRequest (reason .QuestionNotFound )
359331 }
360- // if questionInfo.UserID != req.UserID {
361- // return fmt.Errorf("no permission to set answer")
362- // }
332+ questionInfo .ID = uid .DeShortID (questionInfo .ID )
363333 if questionInfo .AcceptedAnswerID == req .AnswerID {
364334 return nil
365335 }
366336
367- var oldAnswerInfo * entity.Answer
368- if len (questionInfo .AcceptedAnswerID ) > 0 && questionInfo .AcceptedAnswerID != "0" {
369- oldAnswerInfo , _ , err = as .answerRepo .GetByID (ctx , questionInfo .AcceptedAnswerID )
337+ // find answer
338+ var acceptedAnswerInfo * entity.Answer
339+ if len (req .AnswerID ) > 1 {
340+ acceptedAnswerInfo , exist , err = as .answerRepo .GetByID (ctx , req .AnswerID )
370341 if err != nil {
371342 return err
372343 }
373- oldAnswerInfo .ID = uid .DeShortID (oldAnswerInfo .ID )
344+ if ! exist {
345+ return errors .BadRequest (reason .AnswerNotFound )
346+ }
347+ acceptedAnswerInfo .ID = uid .DeShortID (acceptedAnswerInfo .ID )
374348 }
375349
376- err = as . answerRepo . UpdateAccepted ( ctx , req . AnswerID , req . QuestionID )
377- if err != nil {
350+ // update answers status
351+ if err = as . answerRepo . UpdateAcceptedStatus ( ctx , req . AnswerID , req . QuestionID ); err != nil {
378352 return err
379353 }
380354
355+ // update question status
381356 err = as .questionCommon .UpdateAccepted (ctx , req .QuestionID , req .AnswerID )
382357 if err != nil {
383358 log .Error ("UpdateLastAnswer error" , err .Error ())
384359 }
385360
386- as .updateAnswerRank (ctx , req .UserID , questionInfo , newAnswerInfo , oldAnswerInfo )
361+ var oldAnswerInfo * entity.Answer
362+ if len (questionInfo .AcceptedAnswerID ) > 1 {
363+ oldAnswerInfo , _ , err = as .answerRepo .GetByID (ctx , questionInfo .AcceptedAnswerID )
364+ if err != nil {
365+ return err
366+ }
367+ oldAnswerInfo .ID = uid .DeShortID (oldAnswerInfo .ID )
368+ }
369+
370+ as .updateAnswerRank (ctx , req .UserID , questionInfo , acceptedAnswerInfo , oldAnswerInfo )
387371 return nil
388372}
389373
@@ -398,9 +382,9 @@ func (as *AnswerService) updateAnswerRank(ctx context.Context, userID string,
398382 log .Error (err )
399383 }
400384 }
401- if newAnswerInfo . ID != "" {
385+ if newAnswerInfo != nil {
402386 err := as .answerActivityService .AcceptAnswer (ctx , userID , newAnswerInfo .ID ,
403- questionInfo .ID , questionInfo .UserID , newAnswerInfo .UserID , newAnswerInfo .UserID == userID )
387+ questionInfo .ID , questionInfo .UserID , newAnswerInfo .UserID , newAnswerInfo .UserID == questionInfo . UserID )
404388 if err != nil {
405389 log .Error (err )
406390 }
0 commit comments