Skip to content

Commit 28c5d53

Browse files
authored
C 端 question set detail 只会访问线上库 (#309)
2 parents f6e717d + b414b97 commit 28c5d53

File tree

8 files changed

+345
-71
lines changed

8 files changed

+345
-71
lines changed

config/config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ web:
4747

4848
session:
4949
sessionEncryptedKey: "abcd"
50+
cookie:
51+
domain: ".mianshi.icu"
5052

5153
kafka:
5254
network: tcp
@@ -69,4 +71,4 @@ cron:
6971
# 独立对账
7072
syncPaymentAndOrder:
7173
enableSeconds: true # 是否使用秒作解析器,默认否
72-
spec: "* * * * *" # 每分钟执行一次
74+
spec: "* * * * *" # 每分钟执行一次

internal/question/internal/integration/set_handler_test.go

Lines changed: 165 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ func (s *SetHandlerTestSuite) TestQuestionSet_Detail() {
354354
wantCode int
355355
wantResp test.Result[web.QuestionSet]
356356
}{
357+
357358
{
358359
name: "空题集",
359360
before: func(t *testing.T, req *http.Request) {
@@ -421,7 +422,7 @@ func (s *SetHandlerTestSuite) TestQuestionSet_Detail() {
421422
require.Equal(t, int64(322), id)
422423

423424
// 添加问题
424-
questions := []dao.Question{
425+
questions := []dao.PublishQuestion{
425426
{
426427
Id: 614,
427428
Uid: uid + 1,
@@ -469,7 +470,7 @@ func (s *SetHandlerTestSuite) TestQuestionSet_Detail() {
469470
require.NoError(t, err)
470471

471472
// 题集中题目为1
472-
qs, err := s.questionSetDAO.GetQuestionsByID(ctx, id)
473+
qs, err := s.questionSetDAO.GetPubQuestionsByID(ctx, id)
473474
require.NoError(t, err)
474475
require.Equal(t, len(qids), len(qs))
475476
},
@@ -567,7 +568,7 @@ func (s *SetHandlerTestSuite) TestQuestionSet_Detail() {
567568
require.Equal(t, int64(333), id)
568569

569570
// 添加问题
570-
questions := []dao.Question{
571+
questions := []dao.PublishQuestion{
571572
{
572573
Id: 714,
573574
Uid: uid + 1,
@@ -615,7 +616,7 @@ func (s *SetHandlerTestSuite) TestQuestionSet_Detail() {
615616
require.NoError(t, err)
616617

617618
// 题集中题目为1
618-
qs, err := s.questionSetDAO.GetQuestionsByID(ctx, id)
619+
qs, err := s.questionSetDAO.GetPubQuestionsByID(ctx, id)
619620
require.NoError(t, err)
620621
require.Equal(t, len(qids), len(qs))
621622
},
@@ -691,6 +692,166 @@ func (s *SetHandlerTestSuite) TestQuestionSet_Detail() {
691692
},
692693
},
693694
},
695+
{
696+
name: "非空题集-忽略未发布",
697+
before: func(t *testing.T, req *http.Request) {
698+
t.Helper()
699+
700+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
701+
defer cancel()
702+
703+
// 创建一个空题集
704+
id, err := s.questionSetDAO.Create(ctx, dao.QuestionSet{
705+
Id: 433,
706+
Uid: uid,
707+
Title: "Go",
708+
Description: "Go题集",
709+
Biz: "roadmap",
710+
BizId: 2,
711+
Utime: now,
712+
})
713+
require.NoError(t, err)
714+
require.Equal(t, int64(433), id)
715+
716+
// 添加问题
717+
questions := []dao.PublishQuestion{
718+
{
719+
Id: 814,
720+
Uid: uid + 1,
721+
Biz: "project",
722+
BizId: 1,
723+
Title: "Go问题1",
724+
Content: "Go问题1",
725+
Ctime: now,
726+
Utime: now,
727+
},
728+
{
729+
Id: 815,
730+
Uid: uid + 2,
731+
Biz: "project",
732+
BizId: 1,
733+
Title: "Go问题2",
734+
Content: "Go问题2",
735+
Ctime: now,
736+
Utime: now,
737+
},
738+
{
739+
Id: 816,
740+
Uid: uid + 3,
741+
Biz: "project",
742+
BizId: 1,
743+
Title: "Go问题3",
744+
Content: "Go问题3",
745+
Ctime: now,
746+
Utime: now,
747+
},
748+
}
749+
err = s.db.WithContext(ctx).Create(&questions).Error
750+
require.NoError(t, err)
751+
752+
err = s.db.WithContext(ctx).Create(&dao.Question{
753+
Id: 817,
754+
Uid: uid + 3,
755+
Biz: "project",
756+
BizId: 1,
757+
Title: "Go问题3",
758+
Content: "Go问题3",
759+
Status: domain.UnPublishedStatus.ToUint8(),
760+
Ctime: now,
761+
Utime: now,
762+
}).Error
763+
require.NoError(t, err)
764+
// 817 还没发布
765+
qids := []int64{814, 815, 816, 817}
766+
require.NoError(t, s.questionSetDAO.UpdateQuestionsByID(ctx, id, qids))
767+
768+
// 添加用户答题记录,只需要添加一个就可以
769+
err = s.db.WithContext(ctx).Create(&dao.QuestionResult{
770+
Uid: uid,
771+
Qid: 814,
772+
Result: domain.ResultAdvanced.ToUint8(),
773+
Ctime: now,
774+
Utime: now,
775+
}).Error
776+
require.NoError(t, err)
777+
778+
// 题集中题目为1
779+
qs, err := s.questionSetDAO.GetPubQuestionsByID(ctx, id)
780+
require.NoError(t, err)
781+
require.Equal(t, len(questions), len(qs))
782+
},
783+
after: func(t *testing.T) {
784+
},
785+
req: web.QuestionSetID{
786+
QSID: 433,
787+
},
788+
wantCode: 200,
789+
wantResp: test.Result[web.QuestionSet]{
790+
Data: web.QuestionSet{
791+
Id: 433,
792+
Biz: "roadmap",
793+
BizId: 2,
794+
Title: "Go",
795+
Description: "Go题集",
796+
Interactive: web.Interactive{
797+
ViewCnt: 434,
798+
LikeCnt: 435,
799+
CollectCnt: 436,
800+
Liked: true,
801+
Collected: false,
802+
},
803+
Questions: []web.Question{
804+
{
805+
Id: 814,
806+
Biz: "project",
807+
BizId: 1,
808+
Title: "Go问题1",
809+
Content: "Go问题1",
810+
Interactive: web.Interactive{
811+
ViewCnt: 815,
812+
LikeCnt: 816,
813+
CollectCnt: 817,
814+
Liked: false,
815+
Collected: true,
816+
},
817+
ExamineResult: domain.ResultAdvanced.ToUint8(),
818+
Utime: now,
819+
},
820+
{
821+
Id: 815,
822+
Biz: "project",
823+
BizId: 1,
824+
Title: "Go问题2",
825+
Content: "Go问题2",
826+
Interactive: web.Interactive{
827+
ViewCnt: 816,
828+
LikeCnt: 817,
829+
CollectCnt: 818,
830+
Liked: true,
831+
Collected: false,
832+
},
833+
Utime: now,
834+
},
835+
{
836+
Id: 816,
837+
Biz: "project",
838+
BizId: 1,
839+
Title: "Go问题3",
840+
Content: "Go问题3",
841+
Interactive: web.Interactive{
842+
ViewCnt: 817,
843+
LikeCnt: 818,
844+
CollectCnt: 819,
845+
Liked: false,
846+
Collected: true,
847+
},
848+
Utime: now,
849+
},
850+
},
851+
Utime: now,
852+
},
853+
},
854+
},
694855
}
695856

696857
for _, tc := range testCases {

internal/question/internal/repository/dao/question_set.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type QuestionSetDAO interface {
2929
GetByID(ctx context.Context, id int64) (QuestionSet, error)
3030

3131
GetQuestionsByID(ctx context.Context, id int64) ([]Question, error)
32+
GetPubQuestionsByID(ctx context.Context, id int64) ([]PublishQuestion, error)
33+
3234
UpdateQuestionsByID(ctx context.Context, id int64, qids []int64) error
3335
CountByBiz(ctx context.Context, biz string) (int64, error)
3436
Count(ctx context.Context) (int64, error)
@@ -153,6 +155,21 @@ func (g *GORMQuestionSetDAO) GetQuestionsByID(ctx context.Context, id int64) ([]
153155
return q, err
154156
}
155157

158+
func (g *GORMQuestionSetDAO) GetPubQuestionsByID(ctx context.Context, id int64) ([]PublishQuestion, error) {
159+
var qsq []QuestionSetQuestion
160+
tx := g.db.WithContext(ctx)
161+
if err := tx.Find(&qsq, "qs_id = ?", id).Error; err != nil {
162+
return nil, err
163+
}
164+
questionIDs := make([]int64, len(qsq))
165+
for i := range qsq {
166+
questionIDs[i] = qsq[i].QID
167+
}
168+
var q []PublishQuestion
169+
err := tx.WithContext(ctx).Where("id IN ?", questionIDs).Order("id ASC").Find(&q).Error
170+
return q, err
171+
}
172+
156173
func (g *GORMQuestionSetDAO) UpdateQuestionsByID(ctx context.Context, id int64, qids []int64) error {
157174
return g.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
158175
var qs QuestionSet

internal/question/internal/repository/question_set.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import (
2727
type QuestionSetRepository interface {
2828
Create(ctx context.Context, set domain.QuestionSet) (int64, error)
2929
UpdateQuestions(ctx context.Context, set domain.QuestionSet) error
30+
3031
GetByID(ctx context.Context, id int64) (domain.QuestionSet, error)
32+
PubGetByID(ctx context.Context, id int64) (domain.QuestionSet, error)
33+
3134
Total(ctx context.Context) (int64, error)
3235
List(ctx context.Context, offset int, limit int) ([]domain.QuestionSet, error)
3336
UpdateNonZero(ctx context.Context, set domain.QuestionSet) error
@@ -137,6 +140,16 @@ func (q *questionSetRepository) UpdateQuestions(ctx context.Context, set domain.
137140
return q.dao.UpdateQuestionsByID(ctx, set.Id, qids)
138141
}
139142

143+
func (q *questionSetRepository) getPubDomainQuestions(ctx context.Context, id int64) ([]domain.Question, error) {
144+
questions, err := q.dao.GetPubQuestionsByID(ctx, id)
145+
if err != nil {
146+
return nil, err
147+
}
148+
return slice.Map(questions, func(idx int, src dao.PublishQuestion) domain.Question {
149+
return q.toDomainQuestion(dao.Question(src))
150+
}), err
151+
}
152+
140153
func (q *questionSetRepository) getDomainQuestions(ctx context.Context, id int64) ([]domain.Question, error) {
141154
questions, err := q.dao.GetQuestionsByID(ctx, id)
142155
if err != nil {
@@ -182,6 +195,28 @@ func (q *questionSetRepository) GetByID(ctx context.Context, id int64) (domain.Q
182195
}, nil
183196
}
184197

198+
func (q *questionSetRepository) PubGetByID(ctx context.Context, id int64) (domain.QuestionSet, error) {
199+
set, err := q.dao.GetByID(ctx, id)
200+
if err != nil {
201+
return domain.QuestionSet{}, err
202+
}
203+
questions, err := q.getPubDomainQuestions(ctx, id)
204+
if err != nil {
205+
return domain.QuestionSet{}, err
206+
}
207+
208+
return domain.QuestionSet{
209+
Id: set.Id,
210+
Uid: set.Uid,
211+
Title: set.Title,
212+
Biz: set.Biz,
213+
BizId: set.BizId,
214+
Description: set.Description,
215+
Questions: questions,
216+
Utime: time.UnixMilli(set.Utime),
217+
}, nil
218+
}
219+
185220
func (q *questionSetRepository) Total(ctx context.Context) (int64, error) {
186221
return q.dao.Count(ctx)
187222
}

internal/question/internal/service/question_set.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type QuestionSetService interface {
4141
DetailByBiz(ctx context.Context, biz string, bizId int64) (domain.QuestionSet, error)
4242
GetCandidates(ctx context.Context, id int64, offset int, limit int) ([]domain.Question, int64, error)
4343
GetByIDsWithQuestion(ctx context.Context, ids []int64) ([]domain.QuestionSet, error)
44+
45+
PubDetail(ctx context.Context, id int64) (domain.QuestionSet, error)
4446
}
4547

4648
type questionSetService struct {
@@ -139,6 +141,22 @@ func (q *questionSetService) Detail(ctx context.Context, id int64) (domain.Quest
139141
return qs, err
140142
}
141143

144+
func (q *questionSetService) PubDetail(ctx context.Context, id int64) (domain.QuestionSet, error) {
145+
qs, err := q.repo.PubGetByID(ctx, id)
146+
if err == nil {
147+
// 没有区分 B 端还是 C 端,但是这种计数不需要精确计算
148+
go func() {
149+
newCtx, cancel := context.WithTimeout(context.Background(), time.Second*3)
150+
defer cancel()
151+
err1 := q.intrProducer.Produce(newCtx, event.NewViewCntEvent(id, domain.QuestionSetBiz))
152+
if err1 != nil {
153+
q.logger.Error("发送阅读计数消息到消息队列失败", elog.FieldErr(err1), elog.Int64("qsid", id))
154+
}
155+
}()
156+
}
157+
return qs, err
158+
}
159+
142160
func (q *questionSetService) List(ctx context.Context, offset, limit int) ([]domain.QuestionSet, int64, error) {
143161
var (
144162
eg errgroup.Group

internal/question/internal/web/question_set_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (h *QuestionSetHandler) GetDetailByBiz(
113113
func (h *QuestionSetHandler) RetrieveQuestionSetDetail(
114114
ctx *ginx.Context,
115115
req QuestionSetID) (ginx.Result, error) {
116-
data, err := h.svc.Detail(ctx.Request.Context(), req.QSID)
116+
data, err := h.svc.PubDetail(ctx.Request.Context(), req.QSID)
117117
if err != nil {
118118
return systemErrorResult, err
119119
}

0 commit comments

Comments
 (0)