You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
package logic
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"healthapi/internal/model"
|
|
"healthapi/internal/svc"
|
|
"healthapi/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetGroupedQuestionsLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewGetGroupedQuestionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGroupedQuestionsLogic {
|
|
return &GetGroupedQuestionsLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *GetGroupedQuestionsLogic) GetGroupedQuestions() (resp *types.GroupedQuestionsResp, err error) {
|
|
var questions []model.QuestionBank
|
|
l.svcCtx.DB.Order("constitution_type, order_num").Find(&questions)
|
|
|
|
groups := make(map[string][]types.Question)
|
|
for _, q := range questions {
|
|
var options []string
|
|
json.Unmarshal([]byte(q.Options), &options)
|
|
|
|
question := types.Question{
|
|
ID: int(q.ID),
|
|
ConstitutionType: q.ConstitutionType,
|
|
QuestionText: q.QuestionText,
|
|
Options: options,
|
|
OrderNum: q.OrderNum,
|
|
}
|
|
groups[q.ConstitutionType] = append(groups[q.ConstitutionType], question)
|
|
}
|
|
|
|
return &types.GroupedQuestionsResp{Groups: groups}, nil
|
|
}
|
|
|