Skip to content

Commit

Permalink
添加 Correct 接口允许用户纠正 AI 诊断结果 (#255)
Browse files Browse the repository at this point in the history
* 实现correct请求,修改测试结果

* Remove config/.DS_Store from tracking

* fix comment and ci issues

* fix comment and ci issues2

---------

Co-authored-by: 倪诗梦 <[email protected]>
  • Loading branch information
2 people authored and flycash committed Aug 18, 2024
1 parent efc0f96 commit 757ad12
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ go.work
/go.work.sum
/internal/user/internal/integration/logs/
config/local.yaml
config/.DS_Store
logs/
/internal/baguwen/internal/integration/logs/
/**/**/logs
Expand Down
13 changes: 0 additions & 13 deletions .run/local.run.xml

This file was deleted.

Binary file removed config/.DS_Store
Binary file not shown.
75 changes: 75 additions & 0 deletions internal/question/internal/integration/examine_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,81 @@ func (s *ExamineHandlerTest) TestExamine() {
}
}

func (s *ExamineHandlerTest) TestCorrect() {
testCases := []struct {
name string
before func(t *testing.T)
after func(t *testing.T)

req web.CorrectReq

wantCode int
wantResp test.Result[web.ExamineResult]
}{
{
name: "修改成通过",
before: func(t *testing.T) {
err := s.db.Create(&dao.QuestionResult{
Id: 2,
Uid: uid,
Qid: 1,
Result: domain.ResultFailed.ToUint8(),
Ctime: 123,
Utime: 123,
}).Error
require.NoError(t, err)
},
after: func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
var record dao.QuestionResult
err := s.db.WithContext(ctx).Where("uid = ? AND qid = ?", uid, 1).First(&record).Error

require.NoError(t, err)
assert.True(t, record.Utime > 0)
record.Utime = 0
assert.True(t, record.Ctime > 0)
record.Ctime = 0
assert.True(t, record.Id > 0)
record.Id = 0

assert.Equal(t, dao.QuestionResult{
Uid: uid,
Qid: 1,
Result: domain.ResultBasic.ToUint8(),
}, record)

},
req: web.CorrectReq{
Qid: 1,
Result: domain.ResultBasic.ToUint8(),
},
wantCode: 200,
wantResp: test.Result[web.ExamineResult]{
Data: web.ExamineResult{
Qid: 1,
Result: domain.ResultBasic.ToUint8(),
},
},
},
}

for _, tc := range testCases {
s.T().Run(tc.name, func(t *testing.T) {
tc.before(t)
req, err := http.NewRequest(http.MethodPost,
"/question/examine/correct", iox.NewJSONReader(tc.req))
req.Header.Set("content-type", "application/json")
require.NoError(t, err)
recorder := test.NewJSONResponseRecorder[web.ExamineResult]()
s.server.ServeHTTP(recorder, req)
require.Equal(t, tc.wantCode, recorder.Code)
assert.Equal(t, tc.wantResp, recorder.MustScan())
tc.after(t)
})
}
}

func TestExamineHandler(t *testing.T) {
suite.Run(t, new(ExamineHandlerTest))
}
14 changes: 14 additions & 0 deletions internal/question/internal/repository/dao/examine.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type ExamineDAO interface {
SaveResult(ctx context.Context, record ExamineRecord) error
GetResultByUidAndQid(ctx context.Context, uid int64, qid int64) (QuestionResult, error)
GetResultByUidAndQids(ctx context.Context, uid int64, ids []int64) ([]QuestionResult, error)
UpdateQuestionResult(ctx context.Context, result QuestionResult) error
}

var _ ExamineDAO = &GORMExamineDAO{}
Expand Down Expand Up @@ -73,6 +74,19 @@ func (dao *GORMExamineDAO) SaveResult(ctx context.Context, record ExamineRecord)
})
}

func (dao *GORMExamineDAO) UpdateQuestionResult(ctx context.Context, result QuestionResult) error {
now := time.Now().UnixMilli()
return dao.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
res := tx.Model(&result).Where("uid = ? and qid = ?", result.Uid, result.Qid).Updates(map[string]any{
"uid": result.Uid,
"qid": result.Qid,
"result": result.Result,
"utime": now,
})
return res.Error
})
}

func NewGORMExamineDAO(db *egorm.Component) ExamineDAO {
return &GORMExamineDAO{db: db}
}
10 changes: 10 additions & 0 deletions internal/question/internal/repository/examine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ExamineRepository interface {
SaveResult(ctx context.Context, uid, qid int64, result domain.ExamineResult) error
GetResultByUidAndQid(ctx context.Context, uid int64, qid int64) (domain.Result, error)
GetResultsByIds(ctx context.Context, uid int64, ids []int64) ([]domain.ExamineResult, error)
UpdateQuestionResult(ctx context.Context, uid int64, qid int64, result domain.Result) error
}

var _ ExamineRepository = &CachedExamineRepository{}
Expand Down Expand Up @@ -67,6 +68,15 @@ func (repo *CachedExamineRepository) SaveResult(ctx context.Context, uid, qid in
return err
}

func (repo *CachedExamineRepository) UpdateQuestionResult(ctx context.Context, uid int64, qid int64, result domain.Result) error {
err := repo.dao.UpdateQuestionResult(ctx, dao.QuestionResult{
Uid: uid,
Qid: qid,
Result: result.ToUint8(),
})
return err
}

func NewCachedExamineRepository(dao dao.ExamineDAO) ExamineRepository {
return &CachedExamineRepository{dao: dao}
}
7 changes: 7 additions & 0 deletions internal/question/internal/service/examine.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type ExamineService interface {
Examine(ctx context.Context, uid, qid int64, input string) (domain.ExamineResult, error)
QuestionResult(ctx context.Context, uid, qid int64) (domain.Result, error)
GetResults(ctx context.Context, uid int64, ids []int64) (map[int64]domain.ExamineResult, error)
Correct(ctx context.Context, uid int64, qid int64, questionResult domain.Result) error
}

var _ ExamineService = &LLMExamineService{}
Expand Down Expand Up @@ -92,6 +93,12 @@ func (svc *LLMExamineService) Examine(ctx context.Context,
return result, err
}

func (svc *LLMExamineService) Correct(ctx context.Context, uid int64,
qid int64, questionResult domain.Result) error {
// 更新结果
return svc.repo.UpdateQuestionResult(ctx, uid, qid, questionResult)
}

func (svc *LLMExamineService) parseExamineResult(answer string) domain.Result {
answer = strings.TrimSpace(answer)
// 获取第一行
Expand Down
14 changes: 13 additions & 1 deletion internal/question/internal/web/examine_hander.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/ecodeclub/ginx"
"github.com/ecodeclub/ginx/session"
"github.com/ecodeclub/webook/internal/question/internal/domain"
"github.com/ecodeclub/webook/internal/question/internal/errs"
"github.com/ecodeclub/webook/internal/question/internal/service"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -59,7 +60,18 @@ func (h *ExamineHandler) Examine(ctx *ginx.Context, req ExamineReq, sess session
}
}

// 修改题目的结果
func (h *ExamineHandler) Correct(ctx *ginx.Context, req CorrectReq, sess session.Session) (ginx.Result, error) {
// 实现这个接口
panic("implement me")
err := h.svc.Correct(ctx, sess.Claims().Uid, req.Qid, domain.Result(req.Result))
if err != nil {
return systemErrorResult, err
}
return ginx.Result{
Data: newExamineResult(domain.ExamineResult{
Qid: req.Qid,
Result: domain.Result(req.Result),
}),
}, nil

}
6 changes: 6 additions & 0 deletions internal/question/internal/web/examine_vo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ func newExamineResult(r domain.ExamineResult) ExamineResult {
Amount: r.Amount,
}
}

type CorrectReq struct {
Qid int64 `json:"qid"`
// 修正结果,对应 domain.Result
Result uint8 `json:"result"`
}
6 changes: 0 additions & 6 deletions internal/question/internal/web/vo.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,3 @@ type BizReq struct {
Biz string `json:"biz"`
BizId int64 `json:"bizId"`
}

type CorrectReq struct {
Qid int64 `json:"qid"`
// 修正结果,对应 domain.Result
Result uint8 `json:"result"`
}
38 changes: 38 additions & 0 deletions internal/question/mocks/examine.mock.go

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

0 comments on commit 757ad12

Please sign in to comment.