]>
Commit | Line | Data |
---|---|---|
0b18f4aa C |
1 | import { NSFWQuery } from '../../../../shared/models/search' |
2 | ||
3 | export class AdvancedSearch { | |
4 | startDate: string // ISO 8601 | |
5 | endDate: string // ISO 8601 | |
6 | ||
7 | nsfw: NSFWQuery | |
8 | ||
9 | categoryOneOf: string | |
10 | ||
11 | licenceOneOf: string | |
12 | ||
13 | languageOneOf: string | |
14 | ||
15 | tagsOneOf: string | |
16 | tagsAllOf: string | |
17 | ||
18 | durationMin: number // seconds | |
19 | durationMax: number // seconds | |
20 | ||
cddf4503 C |
21 | sort: string |
22 | ||
0b18f4aa C |
23 | constructor (options?: { |
24 | startDate?: string | |
25 | endDate?: string | |
26 | nsfw?: NSFWQuery | |
27 | categoryOneOf?: string | |
28 | licenceOneOf?: string | |
29 | languageOneOf?: string | |
30 | tagsOneOf?: string | |
31 | tagsAllOf?: string | |
32 | durationMin?: string | |
33 | durationMax?: string | |
cddf4503 | 34 | sort?: string |
0b18f4aa C |
35 | }) { |
36 | if (!options) return | |
37 | ||
7afea880 C |
38 | this.startDate = options.startDate || undefined |
39 | this.endDate = options.endDate || undefined | |
40 | this.nsfw = options.nsfw || undefined | |
41 | this.categoryOneOf = options.categoryOneOf || undefined | |
42 | this.licenceOneOf = options.licenceOneOf || undefined | |
43 | this.languageOneOf = options.languageOneOf || undefined | |
44 | this.tagsOneOf = options.tagsOneOf || undefined | |
45 | this.tagsAllOf = options.tagsAllOf || undefined | |
0b18f4aa C |
46 | this.durationMin = parseInt(options.durationMin, 10) |
47 | this.durationMax = parseInt(options.durationMax, 10) | |
48 | ||
49 | if (isNaN(this.durationMin)) this.durationMin = undefined | |
50 | if (isNaN(this.durationMax)) this.durationMax = undefined | |
cddf4503 C |
51 | |
52 | this.sort = options.sort || '-match' | |
0b18f4aa C |
53 | } |
54 | ||
55 | containsValues () { | |
56 | const obj = this.toUrlObject() | |
57 | for (const k of Object.keys(obj)) { | |
cddf4503 C |
58 | if (k === 'sort') continue // Exception |
59 | ||
0b18f4aa C |
60 | if (obj[k] !== undefined) return true |
61 | } | |
62 | ||
63 | return false | |
64 | } | |
65 | ||
66 | reset () { | |
67 | this.startDate = undefined | |
68 | this.endDate = undefined | |
69 | this.nsfw = undefined | |
70 | this.categoryOneOf = undefined | |
71 | this.licenceOneOf = undefined | |
72 | this.languageOneOf = undefined | |
73 | this.tagsOneOf = undefined | |
74 | this.tagsAllOf = undefined | |
75 | this.durationMin = undefined | |
76 | this.durationMax = undefined | |
cddf4503 C |
77 | |
78 | this.sort = '-match' | |
0b18f4aa C |
79 | } |
80 | ||
81 | toUrlObject () { | |
82 | return { | |
83 | startDate: this.startDate, | |
84 | endDate: this.endDate, | |
85 | nsfw: this.nsfw, | |
86 | categoryOneOf: this.categoryOneOf, | |
87 | licenceOneOf: this.licenceOneOf, | |
88 | languageOneOf: this.languageOneOf, | |
89 | tagsOneOf: this.tagsOneOf, | |
90 | tagsAllOf: this.tagsAllOf, | |
91 | durationMin: this.durationMin, | |
cddf4503 C |
92 | durationMax: this.durationMax, |
93 | sort: this.sort | |
0b18f4aa C |
94 | } |
95 | } | |
96 | ||
97 | toAPIObject () { | |
98 | return { | |
99 | startDate: this.startDate, | |
100 | endDate: this.endDate, | |
101 | nsfw: this.nsfw, | |
4278710d C |
102 | categoryOneOf: this.intoArray(this.categoryOneOf), |
103 | licenceOneOf: this.intoArray(this.licenceOneOf), | |
104 | languageOneOf: this.intoArray(this.languageOneOf), | |
105 | tagsOneOf: this.intoArray(this.tagsOneOf), | |
106 | tagsAllOf: this.intoArray(this.tagsAllOf), | |
0b18f4aa | 107 | durationMin: this.durationMin, |
cddf4503 C |
108 | durationMax: this.durationMax, |
109 | sort: this.sort | |
0b18f4aa C |
110 | } |
111 | } | |
4278710d | 112 | |
c5d04b4f RK |
113 | size () { |
114 | let acc = 0 | |
115 | ||
116 | const obj = this.toUrlObject() | |
117 | for (const k of Object.keys(obj)) { | |
118 | if (k === 'sort') continue // Exception | |
119 | ||
120 | if (obj[k] !== undefined) acc++ | |
121 | } | |
122 | ||
123 | return acc | |
124 | } | |
125 | ||
4278710d C |
126 | private intoArray (value: any) { |
127 | if (!value) return undefined | |
128 | ||
129 | if (typeof value === 'string') return value.split(',') | |
130 | ||
131 | return [ value ] | |
132 | } | |
0b18f4aa | 133 | } |