60 lines
1.9 KiB
Go
60 lines
1.9 KiB
Go
package grpc_server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
|
"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 *grpc_pb.GetRecordReq) (*grpc_pb.GetRecordResp, error) {
|
|
if !utils.ShouldBindUsn(ctx, &req.USN) {
|
|
return nil, http_resp.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([]*grpc_pb.GetRecordItem, 0)
|
|
for _, record := range records {
|
|
resp = append(resp, &grpc_pb.GetRecordItem{
|
|
QuestionSn: record.QuestionSn,
|
|
Question: record.Question,
|
|
Difficulty: record.Difficulty,
|
|
Category: record.Category,
|
|
QuestionAnswer: record.QuestionAnswer,
|
|
Answer: record.Answer,
|
|
CreateTime: record.CreatedAt.Unix(),
|
|
})
|
|
}
|
|
return &grpc_pb.GetRecordResp{
|
|
Count: int32(count),
|
|
Records: resp,
|
|
}, nil
|
|
}
|
|
|
|
// GetQuestionInfo 获取具体的题目
|
|
func (s *Server) GetQuestionInfo(ctx context.Context, req *grpc_pb.GetQuestionInfoReq) (*grpc_pb.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 &grpc_pb.GetQuestionInfoResp{
|
|
Question: question.Question,
|
|
Options: options,
|
|
Category: category,
|
|
Difficulty: question.Difficulty,
|
|
Explanation: question.Explanation,
|
|
}, nil
|
|
}
|