answer/internal/service/object_info/object_info.go

227 lines
6.2 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package object_info
import (
"context"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/schema"
answercommon "github.com/answerdev/answer/internal/service/answer_common"
"github.com/answerdev/answer/internal/service/comment_common"
questioncommon "github.com/answerdev/answer/internal/service/question_common"
tagcommon "github.com/answerdev/answer/internal/service/tag_common"
"github.com/answerdev/answer/pkg/obj"
2023-03-24 12:32:39 +08:00
"github.com/answerdev/answer/pkg/uid"
2022-09-27 17:59:05 +08:00
"github.com/segmentfault/pacman/errors"
)
// ObjService user service
type ObjService struct {
answerRepo answercommon.AnswerRepo
questionRepo questioncommon.QuestionRepo
commentRepo comment_common.CommentCommonRepo
tagRepo tagcommon.TagCommonRepo
2022-11-23 18:10:34 +08:00
tagCommon *tagcommon.TagCommonService
2022-09-27 17:59:05 +08:00
}
// NewObjService new object service
func NewObjService(
answerRepo answercommon.AnswerRepo,
questionRepo questioncommon.QuestionRepo,
commentRepo comment_common.CommentCommonRepo,
2022-11-23 18:10:34 +08:00
tagRepo tagcommon.TagCommonRepo,
tagCommon *tagcommon.TagCommonService,
) *ObjService {
2022-09-27 17:59:05 +08:00
return &ObjService{
answerRepo: answerRepo,
questionRepo: questionRepo,
commentRepo: commentRepo,
tagRepo: tagRepo,
2022-11-23 18:10:34 +08:00
tagCommon: tagCommon,
2022-09-27 17:59:05 +08:00
}
}
2022-11-23 18:10:34 +08:00
func (os *ObjService) GetUnreviewedRevisionInfo(ctx context.Context, objectID string) (objInfo *schema.UnreviewedRevisionInfoInfo, err error) {
objectType, err := obj.GetObjectTypeStrByObjectID(objectID)
if err != nil {
return nil, err
}
switch objectType {
case constant.QuestionObjectType:
questionInfo, exist, err := os.questionRepo.GetQuestion(ctx, objectID)
if err != nil {
return nil, err
}
if handler.GetEnableShortID(ctx) {
questionInfo.ID = uid.EnShortID(questionInfo.ID)
}
2022-11-23 18:10:34 +08:00
if !exist {
break
}
taglist, err := os.tagCommon.GetObjectEntityTag(ctx, objectID)
if err != nil {
return nil, err
}
os.tagCommon.TagsFormatRecommendAndReserved(ctx, taglist)
tags, err := os.tagCommon.TagFormat(ctx, taglist)
if err != nil {
return nil, err
}
objInfo = &schema.UnreviewedRevisionInfoInfo{
ObjectID: questionInfo.ID,
Title: questionInfo.Title,
Content: questionInfo.OriginalText,
Html: questionInfo.ParsedText,
Tags: tags,
}
case constant.AnswerObjectType:
answerInfo, exist, err := os.answerRepo.GetAnswer(ctx, objectID)
if err != nil {
return nil, err
}
if !exist {
break
}
questionInfo, exist, err := os.questionRepo.GetQuestion(ctx, answerInfo.QuestionID)
if err != nil {
return nil, err
}
if !exist {
break
}
if handler.GetEnableShortID(ctx) {
questionInfo.ID = uid.EnShortID(questionInfo.ID)
}
2022-11-23 18:10:34 +08:00
objInfo = &schema.UnreviewedRevisionInfoInfo{
ObjectID: answerInfo.ID,
Title: questionInfo.Title,
Content: answerInfo.OriginalText,
Html: answerInfo.ParsedText,
}
case constant.TagObjectType:
tagInfo, exist, err := os.tagRepo.GetTagByID(ctx, objectID, true)
2022-11-23 18:10:34 +08:00
if err != nil {
return nil, err
}
if !exist {
break
}
objInfo = &schema.UnreviewedRevisionInfoInfo{
ObjectID: tagInfo.ID,
Title: tagInfo.SlugName,
Content: tagInfo.OriginalText,
Html: tagInfo.ParsedText,
}
}
if objInfo == nil {
err = errors.BadRequest(reason.ObjectNotFound)
}
return objInfo, err
}
2022-09-27 17:59:05 +08:00
// GetInfo get object simple information
func (os *ObjService) GetInfo(ctx context.Context, objectID string) (objInfo *schema.SimpleObjectInfo, err error) {
objectType, err := obj.GetObjectTypeStrByObjectID(objectID)
if err != nil {
return nil, err
}
switch objectType {
case constant.QuestionObjectType:
questionInfo, exist, err := os.questionRepo.GetQuestion(ctx, objectID)
if err != nil {
return nil, err
}
if !exist {
break
}
objInfo = &schema.SimpleObjectInfo{
2022-11-24 16:11:43 +08:00
ObjectID: questionInfo.ID,
ObjectCreatorUserID: questionInfo.UserID,
QuestionID: questionInfo.ID,
2023-05-19 15:58:56 +08:00
QuestionStatus: questionInfo.Status,
2022-11-24 16:11:43 +08:00
ObjectType: objectType,
Title: questionInfo.Title,
Content: questionInfo.ParsedText, // todo trim
2022-09-27 17:59:05 +08:00
}
case constant.AnswerObjectType:
answerInfo, exist, err := os.answerRepo.GetAnswer(ctx, objectID)
if err != nil {
return nil, err
}
if !exist {
break
}
questionInfo, exist, err := os.questionRepo.GetQuestion(ctx, answerInfo.QuestionID)
if err != nil {
return nil, err
}
if !exist {
break
}
2022-09-27 17:59:05 +08:00
objInfo = &schema.SimpleObjectInfo{
2022-11-24 16:11:43 +08:00
ObjectID: answerInfo.ID,
ObjectCreatorUserID: answerInfo.UserID,
QuestionID: answerInfo.QuestionID,
2023-05-19 15:58:56 +08:00
QuestionStatus: questionInfo.Status,
2022-11-24 16:11:43 +08:00
AnswerID: answerInfo.ID,
ObjectType: objectType,
Title: questionInfo.Title, // this should be question title
Content: answerInfo.ParsedText, // todo trim
2022-09-27 17:59:05 +08:00
}
case constant.CommentObjectType:
commentInfo, exist, err := os.commentRepo.GetComment(ctx, objectID)
if err != nil {
return nil, err
}
if !exist {
break
}
objInfo = &schema.SimpleObjectInfo{
2022-11-24 16:11:43 +08:00
ObjectID: commentInfo.ID,
ObjectCreatorUserID: commentInfo.UserID,
ObjectType: objectType,
Content: commentInfo.ParsedText, // todo trim
CommentID: commentInfo.ID,
2022-09-27 17:59:05 +08:00
}
if len(commentInfo.QuestionID) > 0 {
questionInfo, exist, err := os.questionRepo.GetQuestion(ctx, commentInfo.QuestionID)
if err != nil {
return nil, err
}
if exist {
objInfo.QuestionID = questionInfo.ID
2023-05-19 15:58:56 +08:00
objInfo.QuestionStatus = questionInfo.Status
2022-09-27 17:59:05 +08:00
objInfo.Title = questionInfo.Title
}
answerInfo, exist, err := os.answerRepo.GetAnswer(ctx, commentInfo.ObjectID)
if err != nil {
return nil, err
}
if exist {
objInfo.AnswerID = answerInfo.ID
}
}
case constant.TagObjectType:
tagInfo, exist, err := os.tagRepo.GetTagByID(ctx, objectID, true)
2022-09-27 17:59:05 +08:00
if err != nil {
return nil, err
}
if !exist {
break
}
objInfo = &schema.SimpleObjectInfo{
ObjectID: tagInfo.ID,
TagID: tagInfo.ID,
ObjectType: objectType,
Title: tagInfo.ParsedText,
Content: tagInfo.ParsedText, // todo trim
}
}
if objInfo == nil {
err = errors.BadRequest(reason.ObjectNotFound)
}
return objInfo, err
}