Repository: answerdev/answer
File: internal/service/importer/importer_service.go (Go)

Code before:

func (ipfunc *ImporterFunc) AddQuestion(ctx context.Context, questionInfo plugin.QuestionImporterInfo) (err error) {
	ipfunc.importerService.ImportQuestion(ctx, questionInfo)
	return nil
}

func (ip *ImporterService) NewImporterFunc() plugin.ImporterFunc {
	return &ImporterFunc{importerService: ip}
}

func (ip *ImporterService) ImportQuestion(ctx context.Context, questionInfo plugin.QuestionImporterInfo) (err error) {
	req := &schema.QuestionAdd{}
	errFields := make([]*validator.FormErrorField, 0)
	// To limit rate, remove the following code from comment: Part 1/2
	// reject, rejectKey := ipc.rateLimitMiddleware.DuplicateRequestRejection(ctx, req)
	// if reject {
	// 	return
	// }
	user_info, exist, err := ip.userCommon.GetByEmail(ctx, questionInfo.UserEmail)
	if err != nil {
		log.Errorf("error: %v", err)
		return err
	}
	if !exist {
		log.Errorf("error: User Email not found")
		return err
	}

	// To limit rate, remove the following code from comment: Part 2/2
	// defer func() {
	// 	// If status is not 200 means that the bad request has been returned, so the record should be cleared
	// 	if ctx.Writer.Status() != http.StatusOK {
	// 		ipc.rateLimitMiddleware.DuplicateRequestClear(ctx, rejectKey)
	// 	}
	// }()
	req.UserID = user_info.ID
	req.Title = questionInfo.Title
	req.Content = questionInfo.Content
	req.HTML = "<p>" + questionInfo.Content + "</p>"
	req.Tags = make([]*schema.TagItem, len(questionInfo.Tags))
	for i, tag := range questionInfo.Tags {
		req.Tags[i] = &schema.TagItem{
			SlugName:    tag,
			DisplayName: tag,
		}
	}
	canList, requireRanks, err := ip.rankService.CheckOperationPermissionsForRanks(ctx, req.UserID, []string{
		permission.QuestionAdd,
		permission.QuestionEdit,
		permission.QuestionDelete,
		permission.QuestionClose,

Review comment (suggestion):
The `err` is nil. You need to return the `fmt.Errorf("...")`

Code after:
}

func (ipfunc *ImporterFunc) AddQuestion(ctx context.Context, questionInfo plugin.QuestionImporterInfo) (err error) {
	ipfunc.importerService.ImportQuestion(ctx, questionInfo)
	return nil
}

func (ip *ImporterService) NewImporterFunc() plugin.ImporterFunc {
	return &ImporterFunc{importerService: ip}
}

func (ip *ImporterService) ImportQuestion(ctx context.Context, questionInfo plugin.QuestionImporterInfo) (err error) {
	req := &schema.QuestionAdd{}
	errFields := make([]*validator.FormErrorField, 0)
	// To limit rate, remove the following code from comment: Part 1/2
	// reject, rejectKey := ipc.rateLimitMiddleware.DuplicateRequestRejection(ctx, req)
	// if reject {
	// 	return
	// }
	userInfo, exist, err := ip.userCommon.GetByEmail(ctx, questionInfo.UserEmail)
	if err != nil {
		log.Errorf("error: %v", err)
		return err
	}
	if !exist {
		return fmt.Errorf("User not found")
	}

	// To limit rate, remove the following code from comment: Part 2/2
	// defer func() {
	// 	// If status is not 200 means that the bad request has been returned, so the record should be cleared
	// 	if ctx.Writer.Status() != http.StatusOK {
	// 		ipc.rateLimitMiddleware.DuplicateRequestClear(ctx, rejectKey)
	// 	}
	// }()
	req.UserID = userInfo.ID
	req.Title = questionInfo.Title
	req.Content = questionInfo.Content
	req.HTML = "<p>" + questionInfo.Content + "</p>"
	req.Tags = make([]*schema.TagItem, len(questionInfo.Tags))
	for i, tag := range questionInfo.Tags {
		req.Tags[i] = &schema.TagItem{
			SlugName:    tag,
			DisplayName: tag,
		}
	}
	canList, requireRanks, err := ip.rankService.CheckOperationPermissionsForRanks(ctx, req.UserID, []string{
		permission.QuestionAdd,
		permission.QuestionEdit,
		permission.QuestionDelete,
		permission.QuestionClose,