2017-05-10 11:03:49 +02:00
package common
2017-05-15 01:31:17 +02:00
import (
2017-06-13 13:31:11 +02:00
humanize "github.com/dustin/go-humanize"
2017-05-15 01:31:17 +02:00
"strconv"
"strings"
)
2017-05-10 11:03:49 +02:00
type Status uint8
const (
ShowAll Status = iota
FilterRemakes
Trusted
APlus
)
2017-05-26 01:48:14 +02:00
func ( st * Status ) ToString ( ) string {
switch * st {
case FilterRemakes :
return "1"
case Trusted :
return "2"
case APlus :
return "3"
}
return ""
}
2017-05-15 01:31:17 +02:00
func ( st * Status ) Parse ( s string ) {
switch s {
case "1" :
* st = FilterRemakes
case "2" :
* st = Trusted
case "3" :
* st = APlus
default :
* st = ShowAll
}
}
2017-05-10 11:03:49 +02:00
type SortMode uint8
const (
ID SortMode = iota
Name
Date
Downloads
Size
2017-05-11 13:40:50 +02:00
Seeders
Leechers
Completed
2017-05-10 11:03:49 +02:00
)
2017-05-15 01:31:17 +02:00
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 = ID
}
}
2017-05-26 01:48:14 +02:00
/ * 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 {
switch * s {
case ID :
return "id"
case Name :
2017-06-01 04:04:49 +02:00
return "name.raw"
2017-05-26 01:48:14 +02:00
case Date :
return "date"
case Downloads :
return "downloads"
case Size :
return "filesize"
case Seeders :
return "seeders"
case Leechers :
return "leechers"
case Completed :
return "completed"
}
return "id"
}
2017-06-13 13:31:11 +02:00
func ( s * SortMode ) ToDBField ( ) string {
switch * s {
case ID :
return "torrent_id"
case Name :
return "torrent_name"
case Date :
return "date"
case Downloads :
return "downloads"
case Size :
return "filesize"
case Seeders :
return "seeders"
case Leechers :
return "leechers"
case Completed :
return "completed"
}
return "id"
}
2017-05-10 11:03:49 +02:00
type Category struct {
Main , Sub uint8
}
func ( c Category ) String ( ) ( s string ) {
if c . Main != 0 {
s += strconv . Itoa ( int ( c . Main ) )
}
s += "_"
if c . Sub != 0 {
s += strconv . Itoa ( int ( c . Sub ) )
}
return
}
2017-05-15 01:31:17 +02:00
func ( c Category ) IsSet ( ) bool {
2017-05-26 01:48:14 +02:00
return c . IsMainSet ( ) && c . IsSubSet ( )
}
func ( c Category ) IsMainSet ( ) bool {
return c . Main != 0
}
func ( c Category ) IsSubSet ( ) bool {
return c . Sub != 0
2017-05-15 01:31:17 +02:00
}
// Parse sets category by string
// returns true if string is valid otherwise returns false
func ( c * Category ) Parse ( s string ) ( ok bool ) {
parts := strings . Split ( s , "_" )
if len ( parts ) == 2 {
tmp , err := strconv . ParseUint ( parts [ 0 ] , 10 , 8 )
if err == nil {
c . Main = uint8 ( tmp )
tmp , err = strconv . ParseUint ( parts [ 1 ] , 10 , 8 )
if err == nil {
c . Sub = uint8 ( tmp )
ok = true
}
}
}
return
}
2017-06-13 13:31:11 +02:00
type SizeBytes uint64
func ( sz * SizeBytes ) Parse ( s string ) bool {
size64 , err := humanize . ParseBytes ( s )
if err != nil {
* sz = 0
return false
}
* sz = SizeBytes ( size64 )
return true
}
2017-05-15 01:31:17 +02:00
// deprecated for TorrentParam
2017-05-10 11:03:49 +02:00
type SearchParam struct {
2017-05-30 00:28:21 +02:00
TorrentID uint
2017-05-30 14:12:42 +02:00
FromID uint // Search for torrentID > FromID
2017-05-30 00:28:21 +02:00
Order bool // True means acsending
Status Status
Sort SortMode
Category Category
2017-06-02 16:10:31 +02:00
FromDate string
ToDate string
2017-05-30 00:28:21 +02:00
Page int
UserID uint
Max uint
NotNull string
2017-06-12 01:14:26 +02:00
Language string
2017-06-13 13:31:11 +02:00
MinSize SizeBytes
MaxSize SizeBytes
2017-05-30 00:28:21 +02:00
Query string
2017-05-10 11:03:49 +02:00
}