2022-09-27 17:59:05 +08:00
|
|
|
package answercommon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/entity"
|
|
|
|
"github.com/answerdev/answer/internal/schema"
|
2022-11-09 16:24:25 +08:00
|
|
|
"github.com/answerdev/answer/pkg/htmltext"
|
2022-09-27 17:59:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type AnswerRepo interface {
|
|
|
|
AddAnswer(ctx context.Context, answer *entity.Answer) (err error)
|
|
|
|
RemoveAnswer(ctx context.Context, id string) (err error)
|
|
|
|
UpdateAnswer(ctx context.Context, answer *entity.Answer, Colar []string) (err error)
|
|
|
|
GetAnswer(ctx context.Context, id string) (answer *entity.Answer, exist bool, err error)
|
|
|
|
GetAnswerList(ctx context.Context, answer *entity.Answer) (answerList []*entity.Answer, err error)
|
|
|
|
GetAnswerPage(ctx context.Context, page, pageSize int, answer *entity.Answer) (answerList []*entity.Answer, total int64, err error)
|
2022-11-01 15:25:44 +08:00
|
|
|
UpdateAdopted(ctx context.Context, id string, questionID string) error
|
2022-09-27 17:59:05 +08:00
|
|
|
GetByID(ctx context.Context, id string) (*entity.Answer, bool, error)
|
2022-11-01 15:25:44 +08:00
|
|
|
GetByUserIDQuestionID(ctx context.Context, userID string, questionID string) (*entity.Answer, bool, error)
|
2022-09-27 17:59:05 +08:00
|
|
|
SearchList(ctx context.Context, search *entity.AnswerSearch) ([]*entity.Answer, int64, error)
|
|
|
|
CmsSearchList(ctx context.Context, search *entity.CmsAnswerSearch) ([]*entity.Answer, int64, error)
|
|
|
|
UpdateAnswerStatus(ctx context.Context, answer *entity.Answer) (err error)
|
2022-11-02 15:50:32 +08:00
|
|
|
GetAnswerCount(ctx context.Context) (count int64, err error)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AnswerCommon user service
|
|
|
|
type AnswerCommon struct {
|
|
|
|
answerRepo AnswerRepo
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAnswerCommon(answerRepo AnswerRepo) *AnswerCommon {
|
|
|
|
return &AnswerCommon{
|
|
|
|
answerRepo: answerRepo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:25:44 +08:00
|
|
|
func (as *AnswerCommon) SearchAnswered(ctx context.Context, userID, questionID string) (bool, error) {
|
|
|
|
_, has, err := as.answerRepo.GetByUserIDQuestionID(ctx, userID, questionID)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
return has, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *AnswerCommon) CmsSearchList(ctx context.Context, search *entity.CmsAnswerSearch) ([]*entity.Answer, int64, error) {
|
2022-11-01 09:12:12 +08:00
|
|
|
if search.Status == 0 {
|
|
|
|
search.Status = 1
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
return as.answerRepo.CmsSearchList(ctx, search)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *AnswerCommon) Search(ctx context.Context, search *entity.AnswerSearch) ([]*entity.Answer, int64, error) {
|
|
|
|
list, count, err := as.answerRepo.SearchList(ctx, search)
|
|
|
|
if err != nil {
|
|
|
|
return list, count, err
|
|
|
|
}
|
|
|
|
return list, count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *AnswerCommon) ShowFormat(ctx context.Context, data *entity.Answer) *schema.AnswerInfo {
|
|
|
|
info := schema.AnswerInfo{}
|
|
|
|
info.ID = data.ID
|
2022-11-01 15:25:44 +08:00
|
|
|
info.QuestionID = data.QuestionID
|
2022-09-27 17:59:05 +08:00
|
|
|
info.Content = data.OriginalText
|
2022-11-01 15:25:44 +08:00
|
|
|
info.HTML = data.ParsedText
|
2022-09-27 17:59:05 +08:00
|
|
|
info.Adopted = data.Adopted
|
|
|
|
info.VoteCount = data.VoteCount
|
|
|
|
info.CreateTime = data.CreatedAt.Unix()
|
|
|
|
info.UpdateTime = data.UpdatedAt.Unix()
|
2022-11-01 15:25:44 +08:00
|
|
|
info.UserID = data.UserID
|
2022-09-27 17:59:05 +08:00
|
|
|
return &info
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *AnswerCommon) AdminShowFormat(ctx context.Context, data *entity.Answer) *schema.AdminAnswerInfo {
|
|
|
|
info := schema.AdminAnswerInfo{}
|
|
|
|
info.ID = data.ID
|
2022-11-01 15:25:44 +08:00
|
|
|
info.QuestionID = data.QuestionID
|
2022-09-27 17:59:05 +08:00
|
|
|
info.Adopted = data.Adopted
|
|
|
|
info.VoteCount = data.VoteCount
|
|
|
|
info.CreateTime = data.CreatedAt.Unix()
|
|
|
|
info.UpdateTime = data.UpdatedAt.Unix()
|
2022-11-01 15:25:44 +08:00
|
|
|
info.UserID = data.UserID
|
2022-11-09 16:24:25 +08:00
|
|
|
info.Description = htmltext.FetchExcerpt(data.ParsedText, "...", 240)
|
2022-09-27 17:59:05 +08:00
|
|
|
return &info
|
|
|
|
}
|