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"
|
2022-10-22 17:38:39 +08:00
|
|
|
"go.uber.org/zap"
|
2022-09-26 16:13:31 +08:00
|
|
|
"gorm.io/gorm/clause"
|
2022-09-20 16:42:57 +08:00
|
|
|
)
|
|
|
|
|
2022-10-22 17:38:39 +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()
|
2022-10-22 17:38:39 +08:00
|
|
|
if associations {
|
|
|
|
query = query.Preload(clause.Associations)
|
|
|
|
}
|
|
|
|
if shouldEnable {
|
|
|
|
query = query.Where("is_enabled = true")
|
2022-09-20 16:42:57 +08:00
|
|
|
}
|
2022-10-22 17:38:39 +08:00
|
|
|
query = query.
|
2023-07-15 16:19:49 +08:00
|
|
|
Where(s.db.Get().Where("title LIKE ?", "%"+search+"%").
|
2022-10-22 17:38:39 +08:00
|
|
|
Or("statement LIKE ?", "%"+search+"%"))
|
|
|
|
err := query.Find(&problems).Error
|
2022-09-20 16:42:57 +08:00
|
|
|
if err != nil {
|
2022-10-22 17:38:39 +08:00
|
|
|
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
|
|
|
|
}
|