]>
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 | ||
21 | constructor (options?: { | |
22 | startDate?: string | |
23 | endDate?: string | |
24 | nsfw?: NSFWQuery | |
25 | categoryOneOf?: string | |
26 | licenceOneOf?: string | |
27 | languageOneOf?: string | |
28 | tagsOneOf?: string | |
29 | tagsAllOf?: string | |
30 | durationMin?: string | |
31 | durationMax?: string | |
32 | }) { | |
33 | if (!options) return | |
34 | ||
7afea880 C |
35 | this.startDate = options.startDate || undefined |
36 | this.endDate = options.endDate || undefined | |
37 | this.nsfw = options.nsfw || undefined | |
38 | this.categoryOneOf = options.categoryOneOf || undefined | |
39 | this.licenceOneOf = options.licenceOneOf || undefined | |
40 | this.languageOneOf = options.languageOneOf || undefined | |
41 | this.tagsOneOf = options.tagsOneOf || undefined | |
42 | this.tagsAllOf = options.tagsAllOf || undefined | |
0b18f4aa C |
43 | this.durationMin = parseInt(options.durationMin, 10) |
44 | this.durationMax = parseInt(options.durationMax, 10) | |
45 | ||
46 | if (isNaN(this.durationMin)) this.durationMin = undefined | |
47 | if (isNaN(this.durationMax)) this.durationMax = undefined | |
48 | } | |
49 | ||
50 | containsValues () { | |
51 | const obj = this.toUrlObject() | |
52 | for (const k of Object.keys(obj)) { | |
53 | if (obj[k] !== undefined) return true | |
54 | } | |
55 | ||
56 | return false | |
57 | } | |
58 | ||
59 | reset () { | |
60 | this.startDate = undefined | |
61 | this.endDate = undefined | |
62 | this.nsfw = undefined | |
63 | this.categoryOneOf = undefined | |
64 | this.licenceOneOf = undefined | |
65 | this.languageOneOf = undefined | |
66 | this.tagsOneOf = undefined | |
67 | this.tagsAllOf = undefined | |
68 | this.durationMin = undefined | |
69 | this.durationMax = undefined | |
70 | } | |
71 | ||
72 | toUrlObject () { | |
73 | return { | |
74 | startDate: this.startDate, | |
75 | endDate: this.endDate, | |
76 | nsfw: this.nsfw, | |
77 | categoryOneOf: this.categoryOneOf, | |
78 | licenceOneOf: this.licenceOneOf, | |
79 | languageOneOf: this.languageOneOf, | |
80 | tagsOneOf: this.tagsOneOf, | |
81 | tagsAllOf: this.tagsAllOf, | |
82 | durationMin: this.durationMin, | |
83 | durationMax: this.durationMax | |
84 | } | |
85 | } | |
86 | ||
87 | toAPIObject () { | |
88 | return { | |
89 | startDate: this.startDate, | |
90 | endDate: this.endDate, | |
91 | nsfw: this.nsfw, | |
92 | categoryOneOf: this.categoryOneOf ? this.categoryOneOf.split(',') : undefined, | |
93 | licenceOneOf: this.licenceOneOf ? this.licenceOneOf.split(',') : undefined, | |
94 | languageOneOf: this.languageOneOf ? this.languageOneOf.split(',') : undefined, | |
95 | tagsOneOf: this.tagsOneOf ? this.tagsOneOf.split(',') : undefined, | |
96 | tagsAllOf: this.tagsAllOf ? this.tagsAllOf.split(',') : undefined, | |
97 | durationMin: this.durationMin, | |
98 | durationMax: this.durationMax | |
99 | } | |
100 | } | |
101 | } |