feat 包命名规范
This commit is contained in:
88
internal/grpcserver/server_category.go
Normal file
88
internal/grpcserver/server_category.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/kafka"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/repository"
|
||||
)
|
||||
|
||||
// 玩法 - 类目答题
|
||||
|
||||
// GetAllCategory 获取所有类目
|
||||
func (s *Server) GetAllCategory(ctx context.Context, req *rspb.GetAllCategoryReq) (*rspb.GetAllCategoryResp, error) {
|
||||
categoryList, err := repository.NewCategoryDao(ctx, s.query).FindAll()
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
categories := make([]*rspb.GetAllCategoryItem, 0)
|
||||
for _, category := range categoryList {
|
||||
categories = append(categories, &rspb.GetAllCategoryItem{
|
||||
Sn: category.Sn,
|
||||
Category: category.Category,
|
||||
})
|
||||
}
|
||||
return &rspb.GetAllCategoryResp{
|
||||
Categories: categories,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CategoryGetQuestion 获取题目
|
||||
func (s *Server) CategoryGetQuestion(ctx context.Context, req *rspb.CategoryGetQuestionReq) (*rspb.CategoryGetQuestionResp, error) {
|
||||
question, err := repository.NewQuestionDao(ctx, s.query).FindByRandom(req.CategorySn)
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
options := make([]string, 0)
|
||||
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
|
||||
return nil, utils.ErrorsWrapF(err, "data: %v", question.Options)
|
||||
}
|
||||
category, _ := repository.NewCategoryDao(ctx, s.query).FindNameBySn(question.CategorySn)
|
||||
return &rspb.CategoryGetQuestionResp{
|
||||
Sn: question.Sn,
|
||||
Question: question.Question,
|
||||
Options: options,
|
||||
Category: category,
|
||||
Difficulty: question.Difficulty,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CategoryAnswerQuestion 回答题目
|
||||
func (s *Server) CategoryAnswerQuestion(ctx context.Context, req *rspb.CategoryAnswerQuestionReq) (*rspb.CategoryAnswerQuestionResp, error) {
|
||||
utils.ShouldBindUsn(ctx, &req.USN)
|
||||
|
||||
question, err := repository.NewQuestionDao(ctx, s.query).FindBySn(req.Sn)
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
options := make([]string, 0)
|
||||
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
|
||||
return nil, utils.ErrorsWrapF(err, "data: %v", question.Options)
|
||||
}
|
||||
// 保存答题记录
|
||||
if req.USN != 0 {
|
||||
kafka.NewProducer().Produce(ctx, &kafka.TopicQuestionAnswer{
|
||||
MessageSn: utils.SnowflakeInstance().Generate().Int64(),
|
||||
USN: req.USN,
|
||||
QuestionSn: question.Sn,
|
||||
QuestionAnswer: question.Answer,
|
||||
Answer: req.Answer,
|
||||
})
|
||||
|
||||
// 类目答题正确给5分,错误不得分
|
||||
if req.Answer == question.Answer {
|
||||
kafka.NewProducer().Produce(ctx, &kafka.TopicAddPoint{
|
||||
MessageSn: utils.SnowflakeInstance().Generate().Int64(),
|
||||
Source: kafka.AddPointSourceCategory,
|
||||
USN: req.USN,
|
||||
Point: 5,
|
||||
})
|
||||
}
|
||||
}
|
||||
return &rspb.CategoryAnswerQuestionResp{
|
||||
Answer: question.Answer,
|
||||
Explanation: question.Explanation,
|
||||
}, nil
|
||||
}
|
||||
43
internal/grpcserver/server_init.go
Normal file
43
internal/grpcserver/server_init.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-common/config"
|
||||
"git.hlsq.asia/mmorpg/service-common/discover/common"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/query"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/repository"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
rspb.UnimplementedQgdzsServer
|
||||
service.Base
|
||||
query *query.Query
|
||||
}
|
||||
|
||||
func NewServer(cfg *config.GrpcConfig) *Server {
|
||||
s := &Server{
|
||||
Base: service.Base{
|
||||
Target: common.KeyDiscoverQgdzs,
|
||||
ServiceName: common.KeyDiscoverServiceNameQgdzs,
|
||||
Cfg: cfg,
|
||||
},
|
||||
}
|
||||
s.Base.OnCustomGrpcServerOption = s.OnCustomGrpcServerOption
|
||||
s.Base.OnInit = s.OnInit
|
||||
s.Base.OnClose = s.OnClose
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) OnCustomGrpcServerOption() []grpc.ServerOption {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) OnInit(serve *grpc.Server) {
|
||||
rspb.RegisterQgdzsServer(serve, s)
|
||||
s.query = repository.Query()
|
||||
}
|
||||
|
||||
func (s *Server) OnClose() {
|
||||
}
|
||||
28
internal/grpcserver/server_leaderboard.go
Normal file
28
internal/grpcserver/server_leaderboard.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
)
|
||||
|
||||
// 答题总数排行榜
|
||||
// 正确数量排行榜
|
||||
// 正确率排行榜
|
||||
// 错误数量排行榜
|
||||
// 错误率排行榜
|
||||
|
||||
var (
|
||||
scriptUpdateSha1 string
|
||||
scriptUpdate = `
|
||||
local key = KEYS[1]
|
||||
`
|
||||
)
|
||||
|
||||
// UpdateLeaderboard 更新排行榜
|
||||
func (s *Server) UpdateLeaderboard(ctx context.Context) error {
|
||||
if scriptUpdateSha1 == "" {
|
||||
scriptUpdateSha1 = redis.GetClient().ScriptLoad(ctx, scriptUpdate).Val()
|
||||
}
|
||||
redis.GetClient().EvalSha(ctx, scriptUpdateSha1, []string{"key"}, "value")
|
||||
return nil
|
||||
}
|
||||
48
internal/grpcserver/server_point.go
Normal file
48
internal/grpcserver/server_point.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/httpresp"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/repository"
|
||||
)
|
||||
|
||||
// 模块 - 学识分
|
||||
|
||||
// GetPointRecord 获取学识分获取记录
|
||||
func (s *Server) GetPointRecord(ctx context.Context, req *rspb.GetPointRecordReq) (*rspb.GetPointRecordResp, error) {
|
||||
if !utils.ShouldBindUsn(ctx, &req.USN) {
|
||||
return nil, httpresp.ParamError
|
||||
}
|
||||
records, count, err := repository.NewPointRecordsDao(ctx, s.query).FindByUSN(req.USN, int(req.Page), int(req.PageSize))
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
resp := make([]*rspb.GetPointRecordItem, 0)
|
||||
for _, record := range records {
|
||||
resp = append(resp, &rspb.GetPointRecordItem{
|
||||
Source: record.Source,
|
||||
Point: record.Point,
|
||||
CreateTime: record.CreatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
return &rspb.GetPointRecordResp{
|
||||
Count: int32(count),
|
||||
Records: resp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetPoint 获取学识分
|
||||
func (s *Server) GetPoint(ctx context.Context, req *rspb.GetPointReq) (*rspb.GetPointResp, error) {
|
||||
if !utils.ShouldBindUsn(ctx, &req.USN) {
|
||||
return nil, httpresp.ParamError
|
||||
}
|
||||
pointCard, err := repository.NewPointCardDao(ctx, s.query).FindPointByUserSn(req.USN)
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
return &rspb.GetPointResp{
|
||||
Point: pointCard.Point,
|
||||
}, nil
|
||||
}
|
||||
124
internal/grpcserver/server_question.go
Normal file
124
internal/grpcserver/server_question.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/ai"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/model"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/repository"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
var prompt = []string{`
|
||||
你是一个微信小游戏的内容策划,我需要生成“每日趣味题”,要求:
|
||||
- 避免敏感、争议或超纲内容
|
||||
- 确保选项有迷惑性,解析有趣
|
||||
- 绝对要避免重复,现在的时间是:%v
|
||||
- 这些是我目前的分类:%v
|
||||
- 输出格式为 JSON 数组,不要任何解释、不要Markdown、不要任何其他字符:
|
||||
[{
|
||||
"question": "题目文本", // 简洁,30字以内
|
||||
"options": ["A. 选项1", "B. 选项2", "C. 选项3", "D. 选项4"], // 提供4个选项(A/B/C/D),其中仅1个正确
|
||||
"answer": "C", // 答案
|
||||
"explanation": "解析文本", // 200字以内,尽量幽默有趣,不要在文本中说正确和错误,因为不知道用户选的哪个
|
||||
"category": "分类", // 尽量从上述分类中选择,你也可以增加,但是命名风格要类似
|
||||
"difficulty": 100, // 难度分 0 - 100
|
||||
}]
|
||||
如果你准备好了,请回答”好的“,不要有其他任何字符
|
||||
`, `
|
||||
请帮我生成 %v 道,只允许回答 JSON 数组:
|
||||
`, `
|
||||
请继续生成 %v 道,只允许回答 JSON 数组:
|
||||
`, `
|
||||
请继续生成 %v 道,只允许回答 JSON 数组:
|
||||
`, `
|
||||
请继续生成 %v 道,只允许回答 JSON 数组:
|
||||
`, `
|
||||
请继续生成 %v 道,只允许回答 JSON 数组:
|
||||
`}
|
||||
|
||||
type Question struct {
|
||||
Question string `json:"question"` // 题干
|
||||
Options []string `json:"options"` // 选项
|
||||
Answer string `json:"answer"` // 答案
|
||||
Explanation string `json:"explanation"` // 解析
|
||||
Category string `json:"category"` // 分类
|
||||
Difficulty int32 `json:"difficulty"` // 难度分
|
||||
}
|
||||
|
||||
func (s *Server) GenerateQuestion(ctx context.Context, req *rspb.GenerateQuestionReq) (*rspb.GenerateQuestionResp, error) {
|
||||
categoryDao := repository.NewCategoryDao(ctx, s.query, redis.GetCacheClient())
|
||||
category, err := categoryDao.FindAll()
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
categoryName := make([]string, 0)
|
||||
for _, c := range category {
|
||||
categoryName = append(categoryName, c.Category)
|
||||
}
|
||||
question := make([]*Question, 0)
|
||||
err = ai.NewAIClient(false, "", 0.9).
|
||||
RequestAI(
|
||||
[]string{
|
||||
fmt.Sprintf(prompt[0], time.Now().Format("2006-01-02 15:04:05"), categoryName),
|
||||
fmt.Sprintf(prompt[1], req.Num),
|
||||
fmt.Sprintf(prompt[2], req.Num),
|
||||
fmt.Sprintf(prompt[3], req.Num),
|
||||
fmt.Sprintf(prompt[4], req.Num),
|
||||
fmt.Sprintf(prompt[5], req.Num),
|
||||
},
|
||||
func(content string, i int) error {
|
||||
if i == 0 {
|
||||
return nil
|
||||
}
|
||||
step := make([]*Question, 0)
|
||||
if err = json.Unmarshal([]byte(content), &step); err != nil {
|
||||
return utils.ErrorsWrapF(err, "data: %v", content)
|
||||
}
|
||||
question = append(question, step...)
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
|
||||
questionDao := repository.NewQuestionDao(ctx, s.query, redis.GetCacheClient())
|
||||
for _, q := range question {
|
||||
marshal, _ := json.Marshal(q.Options)
|
||||
categorySn, err := categoryDao.FindSnByName(q.Category)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
cat, err := categoryDao.Create(&model.Category{
|
||||
Category: q.Category,
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf("GenerateQuestion CreateCategory error: %v", err)
|
||||
continue
|
||||
}
|
||||
categorySn = cat.Sn
|
||||
} else {
|
||||
log.Errorf("GenerateQuestion FindSnByName error: %v", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
if _, err = questionDao.Create(&model.Question{
|
||||
Question: q.Question,
|
||||
Options: string(marshal),
|
||||
Answer: q.Answer,
|
||||
Explanation: q.Explanation,
|
||||
CategorySn: categorySn,
|
||||
Difficulty: q.Difficulty,
|
||||
}); err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
60
internal/grpcserver/server_quickly.go
Normal file
60
internal/grpcserver/server_quickly.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/kafka"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/repository"
|
||||
)
|
||||
|
||||
// 玩法 - 随机答题
|
||||
|
||||
// QuicklyGetQuestion 获取题目
|
||||
func (s *Server) QuicklyGetQuestion(ctx context.Context, req *rspb.QuicklyGetQuestionReq) (*rspb.QuicklyGetQuestionResp, error) {
|
||||
question, err := repository.NewQuestionDao(ctx, s.query).FindByRandom(0)
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
options := make([]string, 0)
|
||||
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
|
||||
return nil, utils.ErrorsWrapF(err, "data: %v", question.Options)
|
||||
}
|
||||
category, _ := repository.NewCategoryDao(ctx, s.query).FindNameBySn(question.CategorySn)
|
||||
return &rspb.QuicklyGetQuestionResp{
|
||||
Sn: question.Sn,
|
||||
Question: question.Question,
|
||||
Options: options,
|
||||
Category: category,
|
||||
Difficulty: question.Difficulty,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// QuicklyAnswerQuestion 回答题目
|
||||
func (s *Server) QuicklyAnswerQuestion(ctx context.Context, req *rspb.QuicklyAnswerQuestionReq) (*rspb.QuicklyAnswerQuestionResp, error) {
|
||||
utils.ShouldBindUsn(ctx, &req.USN)
|
||||
|
||||
question, err := repository.NewQuestionDao(ctx, s.query).FindBySn(req.Sn)
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
options := make([]string, 0)
|
||||
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
|
||||
return nil, utils.ErrorsWrapF(err, "data: %v", question.Options)
|
||||
}
|
||||
// 保存答题记录
|
||||
if req.USN != 0 {
|
||||
kafka.NewProducer().Produce(ctx, &kafka.TopicQuestionAnswer{
|
||||
MessageSn: utils.SnowflakeInstance().Generate().Int64(),
|
||||
USN: req.USN,
|
||||
QuestionSn: question.Sn,
|
||||
QuestionAnswer: question.Answer,
|
||||
Answer: req.Answer,
|
||||
})
|
||||
}
|
||||
return &rspb.QuicklyAnswerQuestionResp{
|
||||
Answer: question.Answer,
|
||||
Explanation: question.Explanation,
|
||||
}, nil
|
||||
}
|
||||
70
internal/grpcserver/server_random.go
Normal file
70
internal/grpcserver/server_random.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/kafka"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/repository"
|
||||
)
|
||||
|
||||
// 玩法 - 随机答题
|
||||
|
||||
// RandomGetQuestion 获取题目
|
||||
func (s *Server) RandomGetQuestion(ctx context.Context, req *rspb.RandomGetQuestionReq) (*rspb.RandomGetQuestionResp, error) {
|
||||
question, err := repository.NewQuestionDao(ctx, s.query).FindByRandom(0)
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
options := make([]string, 0)
|
||||
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
|
||||
return nil, utils.ErrorsWrapF(err, "data: %v", question.Options)
|
||||
}
|
||||
category, _ := repository.NewCategoryDao(ctx, s.query).FindNameBySn(question.CategorySn)
|
||||
return &rspb.RandomGetQuestionResp{
|
||||
Sn: question.Sn,
|
||||
Question: question.Question,
|
||||
Options: options,
|
||||
Category: category,
|
||||
Difficulty: question.Difficulty,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// RandomAnswerQuestion 回答题目
|
||||
func (s *Server) RandomAnswerQuestion(ctx context.Context, req *rspb.RandomAnswerQuestionReq) (*rspb.RandomAnswerQuestionResp, error) {
|
||||
utils.ShouldBindUsn(ctx, &req.USN)
|
||||
|
||||
question, err := repository.NewQuestionDao(ctx, s.query).FindBySn(req.Sn)
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
options := make([]string, 0)
|
||||
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
|
||||
return nil, utils.ErrorsWrapF(err, "data: %v", question.Options)
|
||||
}
|
||||
// 保存答题记录
|
||||
if req.USN != 0 {
|
||||
kafka.NewProducer().Produce(ctx, &kafka.TopicQuestionAnswer{
|
||||
MessageSn: utils.SnowflakeInstance().Generate().Int64(),
|
||||
USN: req.USN,
|
||||
QuestionSn: question.Sn,
|
||||
QuestionAnswer: question.Answer,
|
||||
Answer: req.Answer,
|
||||
})
|
||||
|
||||
// 随机答题正确给10分,错误不得分
|
||||
if req.Answer == question.Answer {
|
||||
kafka.NewProducer().Produce(ctx, &kafka.TopicAddPoint{
|
||||
MessageSn: utils.SnowflakeInstance().Generate().Int64(),
|
||||
Source: kafka.AddPointSourceRandom,
|
||||
USN: req.USN,
|
||||
Point: 10,
|
||||
})
|
||||
}
|
||||
}
|
||||
return &rspb.RandomAnswerQuestionResp{
|
||||
Answer: question.Answer,
|
||||
Explanation: question.Explanation,
|
||||
}, nil
|
||||
}
|
||||
59
internal/grpcserver/server_record.go
Normal file
59
internal/grpcserver/server_record.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/httpresp"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/repository"
|
||||
)
|
||||
|
||||
// 模块 - 答题记录
|
||||
|
||||
// GetRecord 获取答题记录
|
||||
func (s *Server) GetRecord(ctx context.Context, req *rspb.GetRecordReq) (*rspb.GetRecordResp, error) {
|
||||
if !utils.ShouldBindUsn(ctx, &req.USN) {
|
||||
return nil, httpresp.ParamError
|
||||
}
|
||||
records, count, err := repository.NewRecordDao(ctx, s.query).FindByUSN(req.USN, int(req.Page), int(req.PageSize))
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
resp := make([]*rspb.GetRecordItem, 0)
|
||||
for _, record := range records {
|
||||
resp = append(resp, &rspb.GetRecordItem{
|
||||
QuestionSn: record.QuestionSn,
|
||||
Question: record.Question,
|
||||
Difficulty: record.Difficulty,
|
||||
Category: record.Category,
|
||||
QuestionAnswer: record.QuestionAnswer,
|
||||
Answer: record.Answer,
|
||||
CreateTime: record.CreatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
return &rspb.GetRecordResp{
|
||||
Count: int32(count),
|
||||
Records: resp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetQuestionInfo 获取具体的题目
|
||||
func (s *Server) GetQuestionInfo(ctx context.Context, req *rspb.GetQuestionInfoReq) (*rspb.GetQuestionInfoResp, error) {
|
||||
question, err := repository.NewQuestionDao(ctx, s.query).FindBySn(req.QuestionSn)
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
options := make([]string, 0)
|
||||
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
|
||||
return nil, utils.ErrorsWrapF(err, "data: %v", question.Options)
|
||||
}
|
||||
category, _ := repository.NewCategoryDao(ctx, s.query).FindNameBySn(question.CategorySn)
|
||||
return &rspb.GetQuestionInfoResp{
|
||||
Question: question.Question,
|
||||
Options: options,
|
||||
Category: category,
|
||||
Difficulty: question.Difficulty,
|
||||
Explanation: question.Explanation,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user