CropService.ts 1.81 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1
import * as UrlTemplate from 'url-template';
2
import { axiosBackend } from 'utilities/requestUtils';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
3
4
5

import Crop from 'model/Crop';

6
7
8
const URL_LIST_CROPS = UrlTemplate.parse(`/api/v0/crops`);
const URL_SAVE_CROP = `/api/v0/crops/save`;
const URL_DELETE_CROP = UrlTemplate.parse(`/api/v0/crops/{shortName}`);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
9
10
11
12
13
14
15
16

export class CropService {

    /**
     * listCrops at /api/v0/crops
     *
     * @param authToken Authorization token
     */
17
    public static listCrops(): Promise<Crop[]> {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
18
19
20
21
22

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

23
      return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
24
25
26
27
28
29
30
31
32
33
34
35
        url: apiUrl,
        method: 'GET',
        ...content,
      }).then(({ data }) => data as Crop[]);
    }

    /**
     * saveCrop at /api/v0/crops/save
     *
     * @param authToken Authorization token
     * @param cropJson cropJson
     */
36
    public static saveCrop(cropJson: Crop): Promise<Crop> {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
37
38
39
40
41

        const apiUrl = URL_SAVE_CROP;
        // console.log(`Fetching from ${apiUrl}`);
        const content = { data: cropJson };

42
        return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
43
44
45
46
47
48
49
50
51
52
53
54
            url: apiUrl,
            method: 'PUT',
            ...content,
        }).then(({ data }) => data as Crop);
    }

    /**
     * deleteCrop at /api/v0/crops/{shortName}
     *
     * @param authToken Authorization token
     * @param shortName shortName
     */
55
    public static deleteCrop(shortName: string): Promise<Crop> {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
56
57
58
59
60

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

61
        return axiosBackend.request({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
62
63
64
65
66
67
68
            url: apiUrl,
            method: 'DELETE',
            ...content,
        }).then(({ data }) => data as Crop);
    }

}