Skip to content

Commit

Permalink
对接案例集
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Aug 8, 2024
1 parent cd19066 commit 6efd51f
Show file tree
Hide file tree
Showing 33 changed files with 208 additions and 349 deletions.
Binary file modified config/.DS_Store
Binary file not shown.
32 changes: 16 additions & 16 deletions internal/bff/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (c *CollectionHandlerTestSuite) SetupSuite() {
func (c *CollectionHandlerTestSuite) Test_Handler() {
t := c.T()
req, err := http.NewRequest(http.MethodPost,
"/interactive/collection/info", iox.NewJSONReader(web.CollectionInfoReq{
"/interactive/collection/records", iox.NewJSONReader(web.CollectionInfoReq{
ID: 1,
Offset: 0,
Limit: 10,
Expand All @@ -147,9 +147,9 @@ func (c *CollectionHandlerTestSuite) Test_Handler() {
},
{
Question: web.Question{
ID: 2,
Title: "这是题目2",
Result: 2 % 4,
ID: 2,
Title: "这是题目2",
ExamineResult: 2 % 4,
},
},
{
Expand All @@ -158,14 +158,14 @@ func (c *CollectionHandlerTestSuite) Test_Handler() {
Title: "这是题集3",
Questions: []web.Question{
{
ID: 33,
Title: "这是题目33",
Result: 33 % 4,
ID: 33,
Title: "这是题目33",
ExamineResult: 33 % 4,
},
{
ID: 36,
Title: "这是题目36",
Result: 36 % 4,
ID: 36,
Title: "这是题目36",
ExamineResult: 36 % 4,
},
},
},
Expand All @@ -176,14 +176,14 @@ func (c *CollectionHandlerTestSuite) Test_Handler() {
Title: "这是题集4",
Questions: []web.Question{
{
ID: 44,
Title: "这是题目44",
Result: 44 % 4,
ID: 44,
Title: "这是题目44",
ExamineResult: 44 % 4,
},
{
ID: 48,
Title: "这是题目48",
Result: 48 % 4,
ID: 48,
Title: "这是题目48",
ExamineResult: 48 % 4,
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/bff/internal/web/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
const (
CaseBiz = "case"
QuestionBiz = "question"
QuestionSetBiz = "question_set"
QuestionSetBiz = "questionSet"
)

func (h *Handler) CollectionInfo(ctx *ginx.Context, req CollectionInfoReq, sess session.Session) (ginx.Result, error) {
func (h *Handler) CollectionRecords(ctx *ginx.Context, req CollectionInfoReq, sess session.Session) (ginx.Result, error) {
uid := sess.Claims().Uid
// 获取收藏记录
records, err := h.intrSvc.CollectionInfo(ctx, uid, req.ID, req.Offset, req.Limit)
Expand Down
2 changes: 1 addition & 1 deletion internal/bff/internal/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ func NewHandler(

func (h *Handler) PrivateRoutes(server *gin.Engine) {
g := server.Group("/interactive")
g.POST("/collection/info", ginx.BS[CollectionInfoReq](h.CollectionInfo))
g.POST("/collection/records", ginx.BS[CollectionInfoReq](h.CollectionRecords))
}
29 changes: 16 additions & 13 deletions internal/bff/internal/web/vo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ type CollectionInfoReq struct {
}

type CollectionRecord struct {
Case Case `json:"case"`
Question Question `json:"question"`
QuestionSet QuestionSet `json:"questionSet"`
Id int64 `json:"id"`
Case Case `json:"case,omitempty"`
Question Question `json:"question,omitempty"`
QuestionSet QuestionSet `json:"questionSet,omitempty"`
}

type Case struct {
Expand All @@ -24,9 +25,9 @@ type Case struct {
}

type Question struct {
ID int64 `json:"id"`
Title string `json:"title"`
Result uint8 `json:"Result"`
ID int64 `json:"id"`
Title string `json:"title"`
ExamineResult uint8 `json:"examineResult"`
}

type QuestionSet struct {
Expand All @@ -44,14 +45,17 @@ func newCollectionRecord(record interactive.CollectionRecord,
switch record.Biz {
case CaseBiz:
return CollectionRecord{
Id: record.Id,
Case: setCases(record, cm),
}
case QuestionBiz:
return CollectionRecord{
Id: record.Id,
Question: setQuestion(record, qm, examMap),
}
case QuestionSetBiz:
return CollectionRecord{
Id: record.Id,
QuestionSet: setQuestionSet(record, qsm, examMap),
}
}
Expand All @@ -70,9 +74,9 @@ func setQuestion(record interactive.CollectionRecord, qm map[int64]baguwen.Quest
q := qm[record.Question]
exam := examMap[record.Question]
return Question{
ID: q.Id,
Title: q.Title,
Result: exam.Result.ToUint8(),
ID: q.Id,
Title: q.Title,
ExamineResult: exam.Result.ToUint8(),
}
}

Expand All @@ -82,15 +86,14 @@ func setQuestionSet(record interactive.CollectionRecord, qsm map[int64]baguwen.Q
for _, q := range qs.Questions {
exam := examMap[q.Id]
questions = append(questions, Question{
ID: q.Id,
Title: q.Title,
Result: exam.Result.ToUint8(),
ID: q.Id,
Title: q.Title,
ExamineResult: exam.Result.ToUint8(),
})
}
return QuestionSet{
ID: qs.Id,
Title: qs.Title,
Questions: questions,
}

}
4 changes: 4 additions & 0 deletions internal/bff/type.go
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
package bff

type Module struct {
Hdl *Handler
}
16 changes: 9 additions & 7 deletions internal/bff/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (
"github.com/google/wire"
)

func InitHandler(intrSvc interactive.Service,
caseSvc cases.Service,
queSvc baguwen.Service,
queSetSvc baguwen.QuestionSetService,
examSvc baguwen.ExamService) (*web.Handler, error) {
func InitModule(intrModule *interactive.Module,
caseModule *cases.Module,
queModule *baguwen.Module) (*Module, error) {
wire.Build(
web.NewHandler,
wire.FieldsOf(new(*baguwen.Module), "Svc", "SetSvc", "ExamSvc"),
wire.FieldsOf(new(*interactive.Module), "Svc"),
wire.FieldsOf(new(*cases.Module), "Svc"),
wire.Struct(new(Module), "*"),
)
return new(web.Handler), nil
return new(Module), nil
}

type Hdl web.Handler
type Handler = web.Handler
24 changes: 16 additions & 8 deletions internal/bff/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/cases/internal/service/case_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func (c *casSetSvc) GetCandidates(ctx context.Context, id int64, offset int, lim
cids := slice.Map(cs.Cases, func(idx int, src domain.Case) int64 {
return src.Id
})
// 在 NOT IN 查询里面,如果要是 cids 没有元素,那么会变成 NOT IN (NULL)
// 结果就是一个都查询不到,所以这是一个 tricky 的写法
// 不然走 if-else 代码就很难看
if len(cids) == 0 {
cids = append(cids, -1)
}
return c.caRepo.Exclude(ctx, cids, offset, limit)
}

Expand Down
3 changes: 0 additions & 3 deletions internal/cases/internal/web/admin_case_set_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ func NewAdminCaseSetHandler(svc service.CaseSetService) *AdminCaseSetHandler {
return &AdminCaseSetHandler{svc: svc}
}

func (a *AdminCaseSetHandler) PublicRoutes(server *gin.Engine) {
}

func (a *AdminCaseSetHandler) PrivateRoutes(server *gin.Engine) {
g := server.Group("/case-sets")
g.POST("/save", ginx.BS[CaseSet](a.SaveCaseSet))
Expand Down
2 changes: 1 addition & 1 deletion internal/cases/internal/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (h *Handler) PubList(ctx *ginx.Context, req Page) (ginx.Result, error) {
return src.Id
})
var err1 error
intrs, err1 = h.intrSvc.GetByIds(ctx, "question", ids)
intrs, err1 = h.intrSvc.GetByIds(ctx, "case", ids)
// 这个数据查询不到也不需要担心
if err1 != nil {
h.logger.Error("查询数据的点赞数据失败",
Expand Down
1 change: 1 addition & 0 deletions internal/interactive/internal/domain/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Collection struct {
}

type CollectionRecord struct {
Id int64
// 用于分发的
Biz string
Case int64
Expand Down
10 changes: 4 additions & 6 deletions internal/interactive/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package integration

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -593,10 +592,12 @@ func (i *InteractiveTestSuite) TestCollection_Delete() {
require.NoError(t, err)
assert.ElementsMatch(t, []domain.CollectionRecord{
{
Id: 1,
Biz: "case",
Case: 1,
},
{
Id: 2,
Biz: "case",
Case: 2,
},
Expand Down Expand Up @@ -796,10 +797,7 @@ func (i *InteractiveTestSuite) TestCollection_Move() {
Uid: uid,
Biz: "case",
BizId: 1,
Cid: sql.NullInt64{
Valid: true,
Int64: id,
},
Cid: id,
},
}, collectionRecords)

Expand All @@ -810,7 +808,7 @@ func (i *InteractiveTestSuite) TestCollection_Move() {
for _, tc := range testcases {
i.T().Run(tc.name, func(t *testing.T) {
id := tc.before(t)
tc.req.CollectionId = id
tc.req.Cid = id
req, err := http.NewRequest(http.MethodPost,
"/interactive/collection/move", iox.NewJSONReader(tc.req))
req.Header.Set("content-type", "application/json")
Expand Down
6 changes: 6 additions & 0 deletions internal/interactive/internal/integration/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,22 @@ func (i *InteractiveTestSuite) TestCollection_Info() {
},
wantVal: []domain.CollectionRecord{
{
Id: 4,
Biz: repository.QuestionSetBiz,
QuestionSet: 1,
},
{
Id: 3,
Biz: repository.QuestionBiz,
Question: 1,
},
{
Id: 2,
Biz: repository.CaseBiz,
Case: 2,
},
{
Id: 1,
Biz: repository.CaseBiz,
Case: 1,
},
Expand Down Expand Up @@ -301,10 +305,12 @@ func (i *InteractiveTestSuite) TestCollection_Info() {
},
wantVal: []domain.CollectionRecord{
{
Id: 6,
Biz: repository.QuestionBiz,
Question: 2,
},
{
Id: 5,
Biz: repository.CaseBiz,
Case: 3,
},
Expand Down
4 changes: 1 addition & 3 deletions internal/interactive/internal/repository/dao/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

package dao

import "database/sql"

// Interactive 汇总表
type Interactive struct {
Id int64 `gorm:"primaryKey,autoIncrement"`
Expand Down Expand Up @@ -46,7 +44,7 @@ type UserCollectionBiz struct {
Biz string `gorm:"type:varchar(128);uniqueIndex:uid_biz_type_id"`

// Cid 收藏夹id
Cid sql.NullInt64 `gorm:"index"`
Cid int64 `gorm:"index;not null;default(0)"`
Utime int64
Ctime int64
}
Expand Down
7 changes: 4 additions & 3 deletions internal/interactive/internal/repository/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
const (
CaseBiz = "case"
QuestionBiz = "question"
QuestionSetBiz = "question_set"
QuestionSetBiz = "questionSet"
)

var defaultTimeout = 1 * time.Second
Expand All @@ -49,9 +49,9 @@ type InteractiveRepository interface {
DeleteCollection(ctx context.Context, uid, collectionId int64) error
// 收藏夹列表
CollectionList(ctx context.Context, uid int64, offset, limit int) ([]domain.Collection, error)
// 收藏夹详情
// CollectionInfo 收藏夹收藏记录
CollectionInfo(ctx context.Context, uid, collectionId int64, offset, limit int) ([]domain.CollectionRecord, error)
// 转移收藏夹
// MoveCollection 转移收藏夹
MoveCollection(ctx context.Context, biz string, bizid, uid, collectionId int64) error
}

Expand Down Expand Up @@ -207,6 +207,7 @@ func (i *interactiveRepository) collectionToEntity(ie domain.Collection) dao.Col

func (i *interactiveRepository) toCollectionRecord(collectBiz dao.UserCollectionBiz) domain.CollectionRecord {
record := domain.CollectionRecord{
Id: collectBiz.Id,
Biz: collectBiz.Biz,
}
switch collectBiz.Biz {
Expand Down
Loading

0 comments on commit 6efd51f

Please sign in to comment.