woj-server/internal/service/problem/queryFuzz.go

31 lines
771 B
Go
Raw Normal View History

2022-09-20 16:42:57 +08:00
package problem
import (
2023-07-14 21:47:11 +08:00
"git.0x7f.app/WOJ/woj-server/internal/e"
"git.0x7f.app/WOJ/woj-server/internal/model"
"go.uber.org/zap"
2022-09-26 16:13:31 +08:00
"gorm.io/gorm/clause"
2022-09-20 16:42:57 +08:00
)
func (s *service) QueryFuzz(search string, associations bool, shouldEnable bool) ([]*model.Problem, e.Status) {
problems := make([]*model.Problem, 0)
2022-09-20 16:42:57 +08:00
2023-07-15 16:19:49 +08:00
query := s.db.Get()
if associations {
query = query.Preload(clause.Associations)
}
if shouldEnable {
query = query.Where("is_enabled = true")
2022-09-20 16:42:57 +08:00
}
query = query.
2023-07-15 16:19:49 +08:00
Where(s.db.Get().Where("title LIKE ?", "%"+search+"%").
Or("statement LIKE ?", "%"+search+"%"))
err := query.Find(&problems).Error
2022-09-20 16:42:57 +08:00
if err != nil {
s.log.Warn("DatabaseError", zap.Error(err), zap.Any("search", search))
2022-09-20 16:42:57 +08:00
return nil, e.DatabaseError
}
return problems, e.Success
}