feat 奇怪的知识1.0.1
This commit is contained in:
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -68,7 +70,8 @@ type Question struct {
|
||||
}
|
||||
|
||||
func (s *Server) GenerateQuestion(ctx context.Context, req *grpc_pb.GenerateQuestionReq) (*grpc_pb.GenerateQuestionResp, error) {
|
||||
category, err := repository.NewQuestionDao(ctx).FindCategory()
|
||||
categoryDao := repository.NewCategoryDao(ctx, redis.GetCacheClient())
|
||||
category, err := categoryDao.FindAll()
|
||||
if err != nil {
|
||||
log.Errorf("GenerateQuestion FindCategory error: %v", err)
|
||||
return nil, err
|
||||
@@ -105,12 +108,28 @@ func (s *Server) GenerateQuestion(ctx context.Context, req *grpc_pb.GenerateQues
|
||||
questionDao := repository.NewQuestionDao(ctx, redis.GetCacheClient())
|
||||
for _, q := range question {
|
||||
marshal, _ := json.Marshal(q.Options)
|
||||
if err = questionDao.Create(&model.Question{
|
||||
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,
|
||||
Category: q.Category,
|
||||
CategorySn: categorySn,
|
||||
Difficulty: q.Difficulty,
|
||||
}); err != nil {
|
||||
log.Errorf("GenerateQuestion Create error: %v", err)
|
||||
@@ -121,7 +140,7 @@ func (s *Server) GenerateQuestion(ctx context.Context, req *grpc_pb.GenerateQues
|
||||
}
|
||||
|
||||
func (s *Server) GetQuestion(ctx context.Context, req *grpc_pb.GetQuestionReq) (*grpc_pb.GetQuestionResp, error) {
|
||||
question, err := repository.NewQuestionDao(ctx).FindByRandom()
|
||||
question, err := repository.NewQuestionDao(ctx).FindByRandom(req.CategorySn)
|
||||
if err != nil {
|
||||
log.Errorf("GetQuestion error: %v", err)
|
||||
return nil, err
|
||||
@@ -131,10 +150,13 @@ func (s *Server) GetQuestion(ctx context.Context, req *grpc_pb.GetQuestionReq) (
|
||||
log.Errorf("GetQuestion json.Unmarshal error: %v, data: %v", err, question.Options)
|
||||
return nil, err
|
||||
}
|
||||
category, _ := repository.NewCategoryDao(ctx).FindNameBySn(question.CategorySn)
|
||||
return &grpc_pb.GetQuestionResp{
|
||||
Sn: question.Sn,
|
||||
Question: question.Question,
|
||||
Options: options,
|
||||
Sn: question.Sn,
|
||||
Question: question.Question,
|
||||
Options: options,
|
||||
Category: category,
|
||||
Difficulty: question.Difficulty,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -154,3 +176,21 @@ func (s *Server) AnswerQuestion(ctx context.Context, req *grpc_pb.AnswerQuestion
|
||||
Explanation: question.Explanation,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) GetAllCategory(ctx context.Context, req *grpc_pb.GetAllCategoryReq) (*grpc_pb.GetAllCategoryResp, error) {
|
||||
categoryList, err := repository.NewCategoryDao(ctx).FindAll()
|
||||
if err != nil {
|
||||
log.Errorf("GetAllCategory error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
categories := make([]*grpc_pb.GetAllCategoryItem, 0)
|
||||
for _, category := range categoryList {
|
||||
categories = append(categories, &grpc_pb.GetAllCategoryItem{
|
||||
Sn: category.Sn,
|
||||
Category: category.Category,
|
||||
})
|
||||
}
|
||||
return &grpc_pb.GetAllCategoryResp{
|
||||
Categories: categories,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user