(ctx context.Context, req *schema.AnswerUpdateReq)
| 348 | } |
| 349 | |
| 350 | func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq) (string, error) { |
| 351 | var canUpdate bool |
| 352 | _, existUnreviewed, err := as.revisionService.ExistUnreviewedByObjectID(ctx, req.ID) |
| 353 | if err != nil { |
| 354 | return "", err |
| 355 | } |
| 356 | if existUnreviewed { |
| 357 | return "", errors.BadRequest(reason.AnswerCannotUpdate) |
| 358 | } |
| 359 | |
| 360 | answerInfo, exist, err := as.answerRepo.GetByID(ctx, req.ID) |
| 361 | if err != nil { |
| 362 | return "", err |
| 363 | } |
| 364 | if !exist { |
| 365 | return "", errors.BadRequest(reason.AnswerNotFound) |
| 366 | } |
| 367 | if answerInfo.Status == entity.AnswerStatusDeleted { |
| 368 | return "", errors.BadRequest(reason.AnswerCannotUpdate) |
| 369 | } |
| 370 | |
| 371 | questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, answerInfo.QuestionID) |
| 372 | if err != nil { |
| 373 | return "", err |
| 374 | } |
| 375 | if !exist { |
| 376 | return "", errors.BadRequest(reason.QuestionNotFound) |
| 377 | } |
| 378 | |
| 379 | // If the content is the same, ignore it |
| 380 | if answerInfo.OriginalText == req.Content { |
| 381 | return "", nil |
| 382 | } |
| 383 | |
| 384 | insertData := &entity.Answer{} |
| 385 | insertData.ID = req.ID |
| 386 | insertData.UserID = answerInfo.UserID |
| 387 | insertData.QuestionID = questionInfo.ID |
| 388 | insertData.OriginalText = req.Content |
| 389 | insertData.ParsedText = req.HTML |
| 390 | insertData.UpdatedAt = time.Now() |
| 391 | insertData.LastEditUserID = "0" |
| 392 | if answerInfo.UserID != req.UserID { |
| 393 | insertData.LastEditUserID = req.UserID |
| 394 | } |
| 395 | |
| 396 | revisionDTO := &schema.AddRevisionDTO{ |
| 397 | UserID: req.UserID, |
| 398 | ObjectID: req.ID, |
| 399 | Log: req.EditSummary, |
| 400 | } |
| 401 | |
| 402 | if req.NoNeedReview || answerInfo.UserID == req.UserID { |
| 403 | canUpdate = true |
| 404 | } |
| 405 | |
| 406 | if !canUpdate { |
| 407 | revisionDTO.Status = entity.RevisionUnreviewedStatus |
no test coverage detected