2022-05-26 00:39:39 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"online_code/client"
|
|
|
|
"online_code/models/entity"
|
|
|
|
"online_code/models/param"
|
|
|
|
)
|
|
|
|
|
|
|
|
type problemService struct{}
|
|
|
|
|
|
|
|
func ProblemService() *problemService {
|
|
|
|
return &problemService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetList 获取题目列表
|
2022-06-02 16:21:08 +08:00
|
|
|
func (problemService) GetList(p param.GetProblemList) (records []entity.ProblemBasic, count int64, err error) {
|
2022-05-26 23:53:18 +08:00
|
|
|
sel := client.MySQL.Scopes(page(p.Current, p.Size)).Preload("ProblemCategories").Preload("ProblemCategories.CategoryBasic")
|
2022-05-26 00:39:39 +08:00
|
|
|
|
|
|
|
if p.Keyword != "" {
|
|
|
|
sel.Where("title LIKE ? OR content like ?", "%"+p.Keyword+"%", "%"+p.Keyword+"%")
|
|
|
|
}
|
2022-05-26 15:40:21 +08:00
|
|
|
if p.CategoryIdentity != "" {
|
|
|
|
sel.Joins("RIGHT JOINN problem_category pc on pc.problem_id = problem.id").
|
|
|
|
Where("pc.category_id= (SELECT cb.id FROM category cb WHERE cb.identity = ?)", p.CategoryIdentity)
|
|
|
|
}
|
2022-05-26 00:39:39 +08:00
|
|
|
|
|
|
|
err = sel.Order("updated_at DESC").Find(&records).Offset(-1).Limit(-1).Count(&count).Error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProblemInfo 获取题目详情
|
2022-05-26 23:53:18 +08:00
|
|
|
func (problemService) GetProblemInfo(id int) (problem entity.ProblemBasic, err error) {
|
2022-05-26 00:39:39 +08:00
|
|
|
err = client.MySQL.Where("id = ?", id).Find(&problem).Error
|
|
|
|
return
|
|
|
|
}
|