Skip to content

Commit

Permalink
Merge pull request #45 from flycash/questions
Browse files Browse the repository at this point in the history
小修改 QuesionSet
  • Loading branch information
flycash authored Mar 20, 2024
2 parents e995377 + 5e01e9d commit 7b02908
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 69 deletions.
32 changes: 1 addition & 31 deletions internal/question/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,43 +1268,13 @@ func (s *HandlerTestSuite) TestQuestionSet_UpdateQuestions() {
wantCode: 500,
wantResp: test.Result[int64]{Code: 502001, Msg: "系统错误"},
},
{
name: "待添加/删除的问题ID不存在",
before: func(t *testing.T) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

// 创建一个空题集
id, err := s.questionSetDAO.Create(ctx, dao.QuestionSet{
Id: 221,
Uid: uid,
Title: "Go",
Description: "Go题集",
})
require.Equal(t, int64(221), id)
require.NoError(t, err)
},
after: func(t *testing.T) {
t.Helper()
},
req: web.UpdateQuestionsOfQuestionSetReq{
QSID: 221,
QIDs: []int64{1000, 1001, 1002},
},
wantCode: 500,
wantResp: test.Result[int64]{
Code: 502001,
Msg: "系统错误",
},
},
}

for _, tc := range testCases {
s.T().Run(tc.name, func(t *testing.T) {
tc.before(t)
req, err := http.NewRequest(http.MethodPost,
"/question-sets/update", iox.NewJSONReader(tc.req))
"/question-sets/questions/save", iox.NewJSONReader(tc.req))
req.Header.Set("content-type", "application/json")
require.NoError(t, err)
recorder := test.NewJSONResponseRecorder[int64]()
Expand Down
45 changes: 13 additions & 32 deletions internal/question/internal/repository/dao/question_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ package dao
import (
"context"
"errors"
"fmt"
"time"

"github.com/ego-component/egorm"
"gorm.io/gorm"
)

var (
ErrInvalidQuestionSetID = errors.New("题集ID非法")
ErrInvalidQuestionID = errors.New("问题ID非法")
ErrInvalidQuestionID = errors.New("问题ID非法")
)

type QuestionSetDAO interface {
Expand Down Expand Up @@ -61,41 +59,33 @@ func (g *GORMQuestionSetDAO) Create(ctx context.Context, qs QuestionSet) (int64,

func (g *GORMQuestionSetDAO) GetByIDAndUID(ctx context.Context, id, uid int64) (QuestionSet, error) {
var qs QuestionSet
if err := g.db.WithContext(ctx).Find(&qs, "id = ? AND uid = ?", id, uid).Error; err != nil {
if err := g.db.WithContext(ctx).First(&qs, "id = ? AND uid = ?", id, uid).Error; err != nil {
return QuestionSet{}, err
}
if qs.Id == 0 {
return QuestionSet{}, fmt.Errorf("%w", ErrInvalidQuestionSetID)
}
return qs, nil
}

func (g *GORMQuestionSetDAO) GetQuestionsByID(ctx context.Context, id int64) ([]Question, error) {
var qsq []QuestionSetQuestion
tx := g.db.WithContext(ctx)
if err := tx.Find(&qsq, "qs_id = ?", id).Error; err != nil {
return nil, err
}
questionIDs := make([]int64, len(qsq))
for i := range qsq {
questionIDs[i] = qsq[i].QID
}
var q []Question
err := g.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var qsq []QuestionSetQuestion
if err := tx.WithContext(ctx).Find(&qsq, "qs_id = ?", id).Error; err != nil {
return err
}
questionIDs := make([]int64, len(qsq))
for i := range qsq {
questionIDs[i] = qsq[i].QID
}
return tx.WithContext(ctx).Where("id IN ?", questionIDs).Order("id ASC").Find(&q).Error
})
err := tx.WithContext(ctx).Where("id IN ?", questionIDs).Order("id ASC").Find(&q).Error
return q, err
}

func (g *GORMQuestionSetDAO) UpdateQuestionsByIDAndUID(ctx context.Context, id, uid int64, qids []int64) error {
return g.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var qs QuestionSet
if err := tx.WithContext(ctx).Find(&qs, "id = ? AND uid = ?", id, uid).Error; err != nil {
if err := tx.WithContext(ctx).First(&qs, "id = ? AND uid = ?", id, uid).Error; err != nil {
return err
}
if qs.Id == 0 {
return fmt.Errorf("%w", ErrInvalidQuestionSetID)
}

// 全部删除
if err := tx.WithContext(ctx).Where("qs_id = ?", id).Delete(&QuestionSetQuestion{}).Error; err != nil {
return err
Expand All @@ -105,15 +95,6 @@ func (g *GORMQuestionSetDAO) UpdateQuestionsByIDAndUID(ctx context.Context, id,
return nil
}

// 检查问题ID合法性
var count int64
if err := tx.WithContext(ctx).Model(&Question{}).Where("id IN ?", qids).Count(&count).Error; err != nil {
return err
}
if int64(len(qids)) != count {
return fmt.Errorf("%w", ErrInvalidQuestionID)
}

// 重新创建
now := time.Now().UnixMilli()
var newQuestions []QuestionSetQuestion
Expand Down
12 changes: 7 additions & 5 deletions internal/question/internal/web/question_set_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ func NewQuestionSetHandler(svc service.QuestionSetService) (*QuestionSetHandler,
}

func (h *QuestionSetHandler) PrivateRoutes(server *gin.Engine) {
server.POST("/question-sets/save", ginx.BS[SaveQuestionSetReq](h.SaveQuestionSet))
server.POST("/question-sets/update", ginx.BS[UpdateQuestionsOfQuestionSetReq](h.UpdateQuestionsOfQuestionSet))
server.POST("/question-sets/list", ginx.BS[Page](h.ListPrivateQuestionSets))
server.POST("/question-sets/pub/list", ginx.B[Page](h.ListAllQuestionSets))
server.POST("/question-sets/detail", ginx.BS[QuestionSetID](h.RetrieveQuestionSetDetail))
g := server.Group("/question-sets")
g.POST("/save", ginx.BS[SaveQuestionSetReq](h.SaveQuestionSet))
g.POST("/questions/save", ginx.BS[UpdateQuestionsOfQuestionSetReq](h.UpdateQuestionsOfQuestionSet))
g.POST("/list", ginx.BS[Page](h.ListPrivateQuestionSets))
g.POST("/detail", ginx.BS[QuestionSetID](h.RetrieveQuestionSetDetail))

g.POST("/pub/list", ginx.B[Page](h.ListAllQuestionSets))
}

// SaveQuestionSet 保存
Expand Down
2 changes: 1 addition & 1 deletion internal/user/internal/service/wechat_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type WechatOAuth2Service struct {

func NewWechatService(appId, appSecret string) OAuth2Service {
return &WechatOAuth2Service{
redirectURL: url.PathEscape("https://i.meoying.com/oauth2/wechat/callback"),
redirectURL: url.PathEscape("https://8.meoying.com/oauth2/wechat/callback"),
logger: elog.DefaultLogger,
client: http.DefaultClient,
appId: appId,
Expand Down

0 comments on commit 7b02908

Please sign in to comment.