aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/search/advanced-search.model.ts
blob: a0f3331755f2f9941590cdfb630c57f21885f382 (plain) (blame)
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
import { NSFWQuery } from '../../../../shared/models/search'

export class AdvancedSearch {
  startDate: string // ISO 8601
  endDate: string // ISO 8601

  nsfw: NSFWQuery

  categoryOneOf: string

  licenceOneOf: string

  languageOneOf: string

  tagsOneOf: string
  tagsAllOf: string

  durationMin: number // seconds
  durationMax: number // seconds

  constructor (options?: {
    startDate?: string
    endDate?: string
    nsfw?: NSFWQuery
    categoryOneOf?: string
    licenceOneOf?: string
    languageOneOf?: string
    tagsOneOf?: string
    tagsAllOf?: string
    durationMin?: string
    durationMax?: string
  }) {
    if (!options) return

    this.startDate = options.startDate
    this.endDate = options.endDate
    this.nsfw = options.nsfw
    this.categoryOneOf = options.categoryOneOf
    this.licenceOneOf = options.licenceOneOf
    this.languageOneOf = options.languageOneOf
    this.tagsOneOf = options.tagsOneOf
    this.tagsAllOf = options.tagsAllOf
    this.durationMin = parseInt(options.durationMin, 10)
    this.durationMax = parseInt(options.durationMax, 10)

    if (isNaN(this.durationMin)) this.durationMin = undefined
    if (isNaN(this.durationMax)) this.durationMax = undefined
  }

  containsValues () {
    const obj = this.toUrlObject()
    for (const k of Object.keys(obj)) {
      if (obj[k] !== undefined) return true
    }

    return false
  }

  reset () {
    this.startDate = undefined
    this.endDate = undefined
    this.nsfw = undefined
    this.categoryOneOf = undefined
    this.licenceOneOf = undefined
    this.languageOneOf = undefined
    this.tagsOneOf = undefined
    this.tagsAllOf = undefined
    this.durationMin = undefined
    this.durationMax = undefined
  }

  toUrlObject () {
    return {
      startDate: this.startDate,
      endDate: this.endDate,
      nsfw: this.nsfw,
      categoryOneOf: this.categoryOneOf,
      licenceOneOf: this.licenceOneOf,
      languageOneOf: this.languageOneOf,
      tagsOneOf: this.tagsOneOf,
      tagsAllOf: this.tagsAllOf,
      durationMin: this.durationMin,
      durationMax: this.durationMax
    }
  }

  toAPIObject () {
    return {
      startDate: this.startDate,
      endDate: this.endDate,
      nsfw: this.nsfw,
      categoryOneOf: this.categoryOneOf ? this.categoryOneOf.split(',') : undefined,
      licenceOneOf: this.licenceOneOf ? this.licenceOneOf.split(',') : undefined,
      languageOneOf: this.languageOneOf ? this.languageOneOf.split(',') : undefined,
      tagsOneOf: this.tagsOneOf ? this.tagsOneOf.split(',') : undefined,
      tagsAllOf: this.tagsAllOf ? this.tagsAllOf.split(',') : undefined,
      durationMin: this.durationMin,
      durationMax: this.durationMax
    }
  }
}