✨ 完善基础架构,新增部分问题接口
This commit is contained in:
19
repository/base.go
Normal file
19
repository/base.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package repository
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// 分页组件
|
||||
func page(current, size int) func(db *gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
if current == 0 {
|
||||
current = 1
|
||||
}
|
||||
if size < 1 {
|
||||
size = 10
|
||||
}
|
||||
// 计算偏移量
|
||||
offset := (current - 1) * size
|
||||
// 返回组装结果
|
||||
return db.Offset(offset).Limit(size)
|
||||
}
|
||||
}
|
31
repository/problem.go
Normal file
31
repository/problem.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"online_code/client"
|
||||
"online_code/models/entity"
|
||||
"online_code/models/param"
|
||||
)
|
||||
|
||||
type problemService struct{}
|
||||
|
||||
func ProblemService() *problemService {
|
||||
return &problemService{}
|
||||
}
|
||||
|
||||
// GetList 获取题目列表
|
||||
func (problemService) GetList(p param.GetProblemList) (records entity.Problem, count int64, err error) {
|
||||
sel := client.MySQL.Scopes(page(p.Current, p.Size))
|
||||
|
||||
if p.Keyword != "" {
|
||||
sel.Where("title LIKE ? OR content like ?", "%"+p.Keyword+"%", "%"+p.Keyword+"%")
|
||||
}
|
||||
|
||||
err = sel.Order("updated_at DESC").Find(&records).Offset(-1).Limit(-1).Count(&count).Error
|
||||
return
|
||||
}
|
||||
|
||||
// GetProblemInfo 获取题目详情
|
||||
func (problemService) GetProblemInfo(id int) (problem entity.Problem, err error) {
|
||||
err = client.MySQL.Where("id = ?", id).Find(&problem).Error
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user