summaryrefslogtreecommitdiffstats
path: root/common/search.go
blob: 96c0684e9a5f36f1ca2a04631abd2b352f1e5667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package common

import (
	"strconv"
	"strings"
)


type Status uint8

const (
	ShowAll Status = iota
	FilterRemakes
	Trusted
	APlus
)

func (st *Status) Parse(s string) {
	switch s {
	case "1":
		*st = FilterRemakes
		break
	case "2":
		*st = Trusted
		break
	case "3":
		*st = APlus
		break
	default:
		*st = ShowAll
	}
}

type SortMode uint8

const (
	ID SortMode = iota
	Name
	Date
	Downloads
	Size
	Seeders
	Leechers
	Completed
)

func (s *SortMode) Parse(str string) {
	switch str {
	case "1":
		*s = Name
		break
	case "2":
		*s = Date
		break
	case "3":
		*s = Downloads
		break
	case "4":
		*s = Size
		break
	case "5":
		*s = Seeders
		break
	case "6":
		*s = Leechers
		break
	case "7":
		*s = Completed
		break
	default:
		*s = ID
	}
}

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
}

func (c Category) IsSet() bool {
	return c.Main != 0 && c.Sub != 0
}

// 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
}


// deprecated for TorrentParam
type SearchParam struct {
       Order    bool // True means acsending
       Status   Status
       Sort     SortMode
       Category Category
       Page     int
       UserID   uint
       Max      uint
       NotNull  string
       Query    string
}