|
1 | 1 | import axios from "axios";
|
| 2 | +import { CUSTOMERS_API } from "../config"; |
| 3 | +import cache from "./cache"; |
2 | 4 |
|
3 |
| -function findAll() { |
4 |
| - return axios.get("http://localhost:8000/api/customers") |
5 |
| - .then(response => response.data["hydra:member"]); |
| 5 | +async function findAll() { |
| 6 | + const cachedCustomers = await cache.get("customers"); |
| 7 | + |
| 8 | + if (cachedCustomers) { |
| 9 | + return cachedCustomers; |
| 10 | + } |
| 11 | + |
| 12 | + return axios.get(CUSTOMERS_API) |
| 13 | + .then(response => { |
| 14 | + const customers = response.data["hydra:member"]; |
| 15 | + cache.set("customers", customers); |
| 16 | + return customers; |
| 17 | + }); |
6 | 18 | }
|
7 | 19 |
|
8 |
| -function find(id) { |
9 |
| - return axios.get("http://localhost:8000/api/customers/" + id) |
10 |
| - .then(response => response.data); |
| 20 | +async function find(id) { |
| 21 | + const cachedCustomer = await cache.get("customers." + id); |
| 22 | + if (cachedCustomer) { |
| 23 | + return cachedCustomer; |
| 24 | + } |
| 25 | + return axios.get(CUSTOMERS_API + "/" + id) |
| 26 | + .then(response => { |
| 27 | + const customer = response.data; |
| 28 | + cache.set("customers." + id, customer); |
| 29 | + |
| 30 | + return customer; |
| 31 | + }); |
11 | 32 | }
|
12 | 33 |
|
13 | 34 | function create(customer) {
|
14 |
| - return axios.post("http://localhost:8000/api/customers", customer); |
| 35 | + return axios.post(CUSTOMERS_API, customer) |
| 36 | + .then(async response => { |
| 37 | + const cachedCustomers = await cache.get("customers"); |
| 38 | + if (cachedCustomers) { |
| 39 | + cache.set("customers", [...cachedCustomers, response.data]); |
| 40 | + } |
| 41 | + return response; |
| 42 | + }); |
15 | 43 | }
|
16 | 44 |
|
17 | 45 | function update(id, customer) {
|
18 |
| - return axios.put("http://localhost:8000/api/customers/" + id, customer); |
| 46 | + return axios.put(CUSTOMERS_API + "/" + id, customer) |
| 47 | + .then(async response => { |
| 48 | + const cachedCustomers = await cache.get("customers"); |
| 49 | + const cachedCustomer = await cache.get("customers." + id); |
| 50 | + |
| 51 | + if (cachedCustomer) { |
| 52 | + cache.set("customers." + id, response.data); |
| 53 | + } |
| 54 | + |
| 55 | + if (cachedCustomers) { |
| 56 | + const index = cachedCustomers.findIndex(c => c.id == id); |
| 57 | + |
| 58 | + cachedCustomers[index] = response.data; |
| 59 | + } |
| 60 | + |
| 61 | + return response; |
| 62 | + }); |
19 | 63 | }
|
20 | 64 |
|
21 | 65 | function deleteCustomer(id) {
|
22 |
| - return axios.delete("http://localhost:8000/api/customers/" + id); |
| 66 | + return axios.delete(CUSTOMERS_API + "/" + id) |
| 67 | + .then(async response => { |
| 68 | + const cachedCustomers = await cache.get("customers"); |
| 69 | + if (cachedCustomers) { |
| 70 | + cache.set("customers", cachedCustomers.filter(c => c.id != id)); |
| 71 | + } |
| 72 | + return response; |
| 73 | + }); |
23 | 74 | }
|
24 | 75 |
|
25 | 76 | export default {
|
|
0 commit comments