VocabularyService.ts 9.44 KB
Newer Older
1

2 3 4
import * as UrlTemplate from 'url-template';
import * as QueryString from 'query-string';
import { axiosBackend } from 'utilities/requestUtils';
5

6 7 8 9 10
import Page, { IPageRequest } from 'model/Page';
import FilteredPage from 'model/FilteredPage';
import Vocabulary from 'model/vocabulary/Vocabulary';
import VocabularyFilter from 'model/vocabulary/VocabularyFilter';
import VocabularyTerm from 'model/vocabulary/VocabularyTerm';
11

12 13
const URL_AUTOCOMPLETE_LANGUAGE_TERM = `/api/v1/vocabulary/lang/autocomplete`;
const URL_UPDATE_LANGUAGE_VOCABULARY = `/api/v1/vocabulary/lang/update`;
14
const URL_UPDATE_ISO3166_VOCABULARY = `/api/v1/geo/iso3166/update`;
15
const URL_GET_LANGUAGE_TERM = UrlTemplate.parse(`/api/v1/vocabulary/lang/{code}`);
Maxym Borodenko's avatar
Maxym Borodenko committed
16 17 18 19 20 21 22 23
const URL_CREATE = `/api/v1/vocabulary/create`;
const URL_LIST = `/api/v1/vocabulary/list`;
const URL_UPDATE_CONTROLLED_VOCABULARY = `/api/v1/vocabulary/update`;
const URL_GET = UrlTemplate.parse(`/api/v1/vocabulary/{UUID}`);
const URL_DELETE = UrlTemplate.parse(`/api/v1/vocabulary/{UUID},{version}`);
const URL_AUTOCOMPLETE_TERM = UrlTemplate.parse(`/api/v1/vocabulary/{UUID}/autocomplete`);
const URL_LIST_TERMS = UrlTemplate.parse(`/api/v1/vocabulary/{UUID}/terms`);
const URL_GET_TERM = UrlTemplate.parse(`/api/v1/vocabulary/{UUID}/{code}`);
24 25 26
const URL_AUTOCOMPLETE_WIEWS_TERM = `/api/v1/vocabulary/wiews/autocomplete`;
const URL_UPDATE_WIEWS_VOCABULARY = `/api/v1/vocabulary/wiews/update`;
const URL_GET_WIEWS_TERM = UrlTemplate.parse(`/api/v1/vocabulary/wiews/{code}`);
27

28 29 30 31
/*
* Defined in Swagger as 'vocabulary'
*/
class VocabularyService {
32

33
  /**
34
   * autocompleteLanguageTerm at /api/v1/vocabulary/lang/autocomplete
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
   *
   * @param l l
   */
  public static autocompleteLanguageTerm(l: string): Promise<VocabularyTerm[]> {

    const qs = QueryString.stringify({
      l: l || undefined,
    }, {});
    const apiUrl = URL_AUTOCOMPLETE_LANGUAGE_TERM + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as VocabularyTerm[]);
52 53
  }

Matija Obreza's avatar
Matija Obreza committed
54
  /**
55
   * updateLanguageVocabulary at /api/v1/vocabulary/iso3166/update
Matija Obreza's avatar
Matija Obreza committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
   *
   */
  public static updateISO3166Vocabulary(): Promise<string> {

    const apiUrl = URL_UPDATE_ISO3166_VOCABULARY;
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as string);
  }

71
  /**
72
   * updateLanguageVocabulary at /api/v1/vocabulary/lang/update
73 74 75
   *
   */
  public static updateLanguageVocabulary(): Promise<string> {
76

77 78 79 80 81 82
    const apiUrl = URL_UPDATE_LANGUAGE_VOCABULARY;
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
83
      method: 'POST',
84 85
      ...content,
    }).then(({ data }) => data as string);
86 87
  }

88
  /**
89
   * getLanguageTerm at /api/v1/vocabulary/lang/{code}
90 91 92 93 94 95 96 97
   *
   * @param code code
   */
  public static getLanguageTerm(code: string): Promise<VocabularyTerm> {

    const apiUrl = URL_GET_LANGUAGE_TERM.expand({code});
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };
98

99 100
    return axiosBackend.request({
      url: apiUrl,
101
      method: 'GET',
102 103
      ...content,
    }).then(({ data }) => data as VocabularyTerm);
104 105
  }

106
  /**
Maxym Borodenko's avatar
Maxym Borodenko committed
107
   * create at /api/v1/vocabulary/create
108 109 110 111 112 113 114 115
   *
   * @param source source
   */
  public static create(source: Vocabulary): Promise<Vocabulary> {

    const apiUrl = URL_CREATE;
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: source };
116

117 118
    return axiosBackend.request({
      url: apiUrl,
119
      method: 'POST',
120 121
      ...content,
    }).then(({ data }) => data as Vocabulary);
122 123
  }

124
  /**
Maxym Borodenko's avatar
Maxym Borodenko committed
125
   * list at /api/v1/vocabulary/list
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
   *
   * @param filter filter
   * @param f f
   * @param page undefined
   */
  public static list(filter: string | VocabularyFilter, page?: IPageRequest): Promise<FilteredPage<Vocabulary>> {

    const qs = QueryString.stringify({
      f: typeof filter === 'string' ? filter : undefined,
      p: page.page || undefined,
      l: page.size || undefined,
      d: page.direction && page.direction.length && page.direction || undefined,
      s: page.properties || undefined,
    }, {});
    const apiUrl = URL_LIST + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: typeof filter === 'string' ? null : { ...filter } };

    return axiosBackend.request({
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as FilteredPage<Vocabulary>);
  }

  /**
Maxym Borodenko's avatar
Maxym Borodenko committed
152
   * updateControlledVocabulary at /api/v1/vocabulary/update
153 154 155 156 157 158 159 160
   *
   * @param source source
   */
  public static updateControlledVocabulary(source: Vocabulary): Promise<Vocabulary> {

    const apiUrl = URL_UPDATE_CONTROLLED_VOCABULARY;
    // console.log(`Fetching from ${apiUrl}`);
    const content = { data: source };
161

162 163 164 165 166 167 168 169
    return axiosBackend.request({
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as Vocabulary);
  }

  /**
Maxym Borodenko's avatar
Maxym Borodenko committed
170
   * get at /api/v1/vocabulary/{UUID}
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
   *
   * @param UUID UUID
   */
  public static get(UUID: string): Promise<Vocabulary> {

    const apiUrl = URL_GET.expand({UUID});
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as Vocabulary);
  }

  /**
Maxym Borodenko's avatar
Maxym Borodenko committed
188
   * delete at /api/v1/vocabulary/{UUID},{version}
189 190 191 192 193 194 195 196 197 198 199 200
   *
   * @param UUID UUID
   * @param version version
   */
  public static delete(UUID: string, version: number): Promise<Vocabulary> {

    const apiUrl = URL_DELETE.expand({UUID, version});
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
201
      method: 'DELETE',
202 203
      ...content,
    }).then(({ data }) => data as Vocabulary);
204
  }
Valeriy Panov's avatar
Valeriy Panov committed
205

206
  /**
Maxym Borodenko's avatar
Maxym Borodenko committed
207
   * autocompleteTerm at /api/v1/vocabulary/{UUID}/autocomplete
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
   *
   * @param UUID UUID
   * @param q q
   */
  public static autocompleteTerm(UUID: string, q: string): Promise<VocabularyTerm[]> {

    const qs = QueryString.stringify({
      q: q || undefined,
    }, {});
    const apiUrl = URL_AUTOCOMPLETE_TERM.expand({UUID}) + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
223
      method: 'POST',
224 225
      ...content,
    }).then(({ data }) => data as VocabularyTerm[]);
226 227
  }

228
  /**
Maxym Borodenko's avatar
Maxym Borodenko committed
229
   * listTerms at /api/v1/vocabulary/{UUID}/terms
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
   *
   * @param UUID UUID
   * @param page undefined
   */
  public static listTerms(UUID: string, page?: IPageRequest): Promise<Page<VocabularyTerm>> {

    const qs = QueryString.stringify({
      p: page.page || undefined,
      l: page.size || undefined,
      d: page.direction && page.direction.length && page.direction || undefined,
      s: page.properties || undefined,
    }, {});
    const apiUrl = URL_LIST_TERMS.expand({UUID}) + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as Page<VocabularyTerm>);
  }

  /**
Maxym Borodenko's avatar
Maxym Borodenko committed
254
   * getTerm at /api/v1/vocabulary/{UUID}/{code}
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
   *
   * @param code code
   * @param UUID UUID
   */
  public static getTerm(code: string, UUID: string): Promise<VocabularyTerm> {

    const apiUrl = URL_GET_TERM.expand({code, UUID});
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as VocabularyTerm);
  }

  /**
273
   * autocompleteWiewsTerm at /api/v1/vocabulary/wiews/autocomplete
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
   *
   * @param term term
   */
  public static autocompleteWiewsTerm(term: string): Promise<VocabularyTerm[]> {

    const qs = QueryString.stringify({
      term: term || undefined,
    }, {});
    const apiUrl = URL_AUTOCOMPLETE_WIEWS_TERM + (qs ? `?${qs}` : '');
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as VocabularyTerm[]);
  }

  /**
294
   * updateWiewsVocabulary at /api/v1/vocabulary/wiews/update
295 296 297 298 299 300 301 302 303 304 305 306 307
   *
   */
  public static updateWiewsVocabulary(): Promise<string> {

    const apiUrl = URL_UPDATE_WIEWS_VOCABULARY;
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
      method: 'POST',
      ...content,
    }).then(({ data }) => data as string);
308 309
  }

310
  /**
311
   * getWiewsTerm at /api/v1/vocabulary/wiews/{code}
312 313 314 315 316 317 318 319 320 321 322 323 324 325
   *
   * @param code code
   */
  public static getWiewsTerm(code: string): Promise<VocabularyTerm> {

    const apiUrl = URL_GET_WIEWS_TERM.expand({code});
    // console.log(`Fetching from ${apiUrl}`);
    const content = { /* No content in request body */ };

    return axiosBackend.request({
      url: apiUrl,
      method: 'GET',
      ...content,
    }).then(({ data }) => data as VocabularyTerm);
Valeriy Panov's avatar
Valeriy Panov committed
326
  }
327 328


329
}
330 331

export default VocabularyService;