feat 奇怪的知识1.0.1
This commit is contained in:
64
internal/dao/repository/categories.go
Normal file
64
internal/dao/repository/categories.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/mysql"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/model"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/query"
|
||||
)
|
||||
|
||||
type CategoryDao struct {
|
||||
ctx context.Context
|
||||
query *query.Query
|
||||
cache *redis.CacheClient
|
||||
}
|
||||
|
||||
func NewCategoryDao(ctx context.Context, cache ...*redis.CacheClient) *CategoryDao {
|
||||
dao := &CategoryDao{
|
||||
ctx: ctx,
|
||||
query: query.Use(mysql.GetDB(dbName)),
|
||||
}
|
||||
if len(cache) > 0 {
|
||||
dao.cache = cache[0]
|
||||
}
|
||||
return dao
|
||||
}
|
||||
|
||||
func (d *CategoryDao) Create(category *model.Category) (*model.Category, error) {
|
||||
err := d.query.Category.WithContext(d.ctx).
|
||||
Create(category)
|
||||
return category, err
|
||||
}
|
||||
|
||||
func (d *CategoryDao) FindAll() ([]*model.Category, error) {
|
||||
find, err := d.query.Category.WithContext(d.ctx).
|
||||
Select(d.query.Category.Sn, d.query.Category.Category).
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return find, nil
|
||||
}
|
||||
|
||||
func (d *CategoryDao) FindNameBySn(sn string) (string, error) {
|
||||
first, err := d.query.Category.WithContext(d.ctx).
|
||||
Select(d.query.Category.Category).
|
||||
Where(d.query.Category.Sn.Eq(sn)).
|
||||
First()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return first.Category, nil
|
||||
}
|
||||
|
||||
func (d *CategoryDao) FindSnByName(category string) (string, error) {
|
||||
first, err := d.query.Category.WithContext(d.ctx).
|
||||
Select(d.query.Category.Sn).
|
||||
Where(d.query.Category.Category.Eq(category)).
|
||||
First()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return first.Sn, nil
|
||||
}
|
||||
@@ -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