Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/utils/search/sortmode.go
akuma06 d7d737d48b Fix Search by ID (#1592)
torrent_id is a column from both torrents and scrapes tables. So you have to specify for which table you want to filter torrent_id.
Fix #1589
2017-09-18 15:43:07 +02:00

80 lignes
1,4 Kio
Go

package search
import "github.com/NyaaPantsu/nyaa/config"
const (
ID SortMode = iota
Name
Date
Downloads
Size
Seeders
Leechers
Completed
MaxIota
)
// SortField is a database column in ES/DB
type SortField struct {
ES string
DB string
}
var sortFields = []SortField{
{"id", "torrents.torrent_id"},
{"name.raw", "torrent_name"},
{"date", "date"},
{"downloads", "downloads"},
{"filesize", "filesize"},
{"seeders", "seeders"},
{"leechers", "leechers"},
{"completed", "completed"},
}
// SortMode selected sort mode
type SortMode uint8
// Parse a string to sortMode
func (s *SortMode) Parse(str string) {
switch str {
case "1":
*s = Name
case "2":
*s = Date
case "3":
*s = Downloads
case "4":
*s = Size
case "5":
*s = Seeders
case "6":
*s = Leechers
case "7":
*s = Completed
default:
*s = Date
}
}
// ToESField convert a sortmode to use with ES
/* INFO Always need to keep in sync with the field that are used in the
* elasticsearch index.
* TODO Verify the field in postgres database
*/
func (s *SortMode) ToESField() string {
return s.toField().ES
}
// ToDBField convert a sortmode to use with database
func (s *SortMode) ToDBField() string {
return s.toField().DB
}
// Private function to convert sormode to a field struct
func (s *SortMode) toField() SortField {
// if sortmode is within range
if *s >= MaxIota || *s < 1 {
s.Parse(config.Get().Torrents.Order)
}
return sortFields[int(*s)]
}