package grpcserver import ( "context" "encoding/json" "git.hlsq.asia/alex/service-common/net/http/httpresp" "git.hlsq.asia/alex/service-common/proto/rs/rspb" "git.hlsq.asia/alex/service-common/utils" "git.hlsq.asia/alex/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 }