feat 奇怪的知识1.0.1
This commit is contained in:
@@ -28,23 +28,27 @@ func NewQuestionDao(ctx context.Context, cache ...*redis.CacheClient) *QuestionD
|
||||
return dao
|
||||
}
|
||||
|
||||
func (d *QuestionDao) Create(question *model.Question) error {
|
||||
func (d *QuestionDao) Create(question *model.Question) (*model.Question, error) {
|
||||
err := d.query.Question.WithContext(d.ctx).
|
||||
Create(question)
|
||||
return err
|
||||
return question, err
|
||||
}
|
||||
|
||||
func (d *QuestionDao) FindByRandom() (*model.Question, error) {
|
||||
count, err := d.query.Question.WithContext(d.ctx).Count()
|
||||
func (d *QuestionDao) FindByRandom(categorySn string) (*model.Question, error) {
|
||||
q := d.query.Question.WithContext(d.ctx)
|
||||
if categorySn != "" {
|
||||
q = q.Where(d.query.Question.CategorySn.Eq(categorySn))
|
||||
}
|
||||
|
||||
count, err := q.Count()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count == 0 {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
first, err := d.query.Question.WithContext(d.ctx).
|
||||
Offset(utils.RandInt(0, int(count-1))).
|
||||
First()
|
||||
|
||||
first, err := q.Offset(utils.RandInt(0, int(count-1))).First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -70,14 +74,22 @@ func (d *QuestionDao) FindBySn(sn string) (*model.Question, error) {
|
||||
return first, nil
|
||||
}
|
||||
|
||||
func (d *QuestionDao) FindCategory() ([]string, error) {
|
||||
var categories []string
|
||||
err := d.query.Question.WithContext(d.ctx).
|
||||
Select(d.query.Question.Category).
|
||||
Distinct().
|
||||
Scan(&categories)
|
||||
func (d *QuestionDao) FindByCategory(categorySn string) (*model.Question, error) {
|
||||
count, err := d.query.Question.WithContext(d.ctx).
|
||||
Where(d.query.Question.CategorySn.Eq(categorySn)).
|
||||
Count()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return categories, nil
|
||||
if count == 0 {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
first, err := d.query.Question.WithContext(d.ctx).
|
||||
Where(d.query.Question.CategorySn.Eq(categorySn)).
|
||||
Offset(utils.RandInt(0, int(count-1))).
|
||||
First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return first, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user