api.js 4.14 KB
Newer Older
abergavenny's avatar
abergavenny committed
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import axios from 'axios'

import { getBearerToken } from '@/helpers'

const BASE_URI = import.meta.env.VITE_API_URI

const publicApi = axios.create()
const privateApi = axios.create()

const multipartOptions = {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
}

privateApi.interceptors.request.use((config) => {
  config.headers.common['Authorization'] = getBearerToken()
  return config
}, (err) => Promise.reject(err))

export const auth = {
  async login(data) {
    return publicApi.post(`${BASE_URI}/auth/login`, data).then(response => response.data)
  },
  async register(data) {
    return publicApi.post(`${BASE_URI}/auth/register`, data).then(response => response.data)
  },
  async resetPassword(data) {
    return publicApi.post(`${BASE_URI}/auth/reset-password`, data).then(response => response.data)
  },
  async updatePassword(token, data) {
    return publicApi.post(`${BASE_URI}/auth/update-password/${token}`, data).then(response => response.data)
  }
}

export const apartments = {
  async create(data) {
    return privateApi.post(`${BASE_URI}/api/v1/apartments`, data).then(response => response.data)
  },
  async findAll() {
    return privateApi.get(`${BASE_URI}/api/v1/apartments`).then(response => response.data)
  },
  async findFile(id) {
    return privateApi.get(`${BASE_URI}/api/v1/apartments/${id}/files`, { responseType: 'blob' }).then(response => response.data)
  },
  async findOne(id) {
    return privateApi.get(`${BASE_URI}/api/v1/apartments/${id}`).then(response => response.data)
  },
  async update(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/apartments/${id}`, data).then(response => response.data)
  },

  async updateHeating(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/apartments/${id}/heating`, data).then(response => response.data)
  },
  async updateWindows(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/apartments/${id}/windows`, data).then(response => response.data)
  },
  async uploadFile(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/apartments/${id}/files`, data, multipartOptions).then(response => response.data)
  }
}

export const buildings = {
  async create(data) {
    return privateApi.post(`${BASE_URI}/api/v1/buildings`, data).then(response => response.data)
  },
  async findAll() {
    return privateApi.get(`${BASE_URI}/api/v1/buildings`).then(response => response.data)
  },
  async findOne(id) {
    return privateApi.get(`${BASE_URI}/api/v1/buildings/${id}`).then(response => response.data)
  },
  async update(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/buildings/${id}`, data).then(response => response.data)
  },
  async updateBasement(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/buildings/${id}/basement`, data).then(response => response.data)
  },
  async updateCharacteristics(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/buildings/${id}/characteristics`, data).then(response => response.data)
  },
  async updateFacade(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/buildings/${id}/facade`, data).then(response => response.data)
  },
  async updateHeating(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/buildings/${id}/heating`, data).then(response => response.data)
  },
  async updateRoof(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/buildings/${id}/roof`, data).then(response => response.data)
  }
}

export const self = {
  async findOne() {
    return privateApi.get(`${BASE_URI}/api/v1/self`).then(response => response.data)
  }
}

export const users = {
  async findAll() {
    return privateApi.get(`${BASE_URI}/api/v1/users`).then(response => response.data)
  },
  async findOne(id) {
    return privateApi.get(`${BASE_URI}/api/v1/users/${id}`).then(response => response.data)
  },
  async update(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/users/${id}`, data).then(response => response.data)
  },
  async updatePassword(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/users/${id}/change-password`, data).then(response => response.data)
  },
  async updateSharing(id, data) {
    return privateApi.put(`${BASE_URI}/api/v1/users/${id}/sharing`, data).then(response => response.data)
  }
}