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

31 lines
761 B
Go
Raw Normal View History

2022-09-20 16:42:57 +08:00
package problem
import (
"github.com/WHUPRJ/woj-server/internal/e"
2022-10-20 16:50:19 +08:00
"github.com/WHUPRJ/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
query := s.db
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.
Where(s.db.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
}