|
| 1 | +import axios from "axios"; |
| 2 | +const BACKEND_DOMAIN = "http://localhost:8080/fridge"; |
| 3 | +export const registerIngredientManual = async (ingredients) => { |
| 4 | + const token = localStorage.getItem("accessToken"); |
| 5 | + try { |
| 6 | + const response = await axios.post( |
| 7 | + `${BACKEND_DOMAIN}/ingredient/register/manual`, |
| 8 | + { ingredients }, |
| 9 | + { |
| 10 | + headers: { |
| 11 | + Authorization: `Bearer ${token}`, |
| 12 | + }, |
| 13 | + } |
| 14 | + ); |
| 15 | + return response.data; |
| 16 | + } catch (error) { |
| 17 | + if (error.response) { |
| 18 | + console.log(error.response.status); |
| 19 | + console.log(error.response.data); |
| 20 | + } |
| 21 | + throw error; |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +export const updateIngredientInfo = async (ingredient) => { |
| 26 | + const token = localStorage.getItem("accessToken"); |
| 27 | + try { |
| 28 | + const response = await axios.patch( |
| 29 | + `${BACKEND_DOMAIN}/ingredient/update`, |
| 30 | + ingredient, |
| 31 | + { |
| 32 | + headers: { |
| 33 | + Authorization: `Bearer ${token}`, |
| 34 | + }, |
| 35 | + } |
| 36 | + ); |
| 37 | + return response.data; |
| 38 | + } catch (error) { |
| 39 | + if (error.response) { |
| 40 | + console.log(error.response.status); |
| 41 | + console.log(error.response.data); |
| 42 | + } |
| 43 | + throw error; |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +export const deleteIngredientInfo = async (id) => { |
| 48 | + const token = localStorage.getItem("accessToken"); |
| 49 | + try { |
| 50 | + await axios.delete(`${BACKEND_DOMAIN}/ingredient/${id}`, { |
| 51 | + headers: { |
| 52 | + Authorization: `Bearer ${token}`, |
| 53 | + }, |
| 54 | + }); |
| 55 | + return true; |
| 56 | + } catch (error) { |
| 57 | + if (error.response) { |
| 58 | + console.log(error.response.status); |
| 59 | + console.log(error.response.data); |
| 60 | + } |
| 61 | + throw error; |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +export const updateIngredients = async (ingredients) => { |
| 66 | + const token = localStorage.getItem("accessToken"); |
| 67 | + try { |
| 68 | + const response = await axios.patch( |
| 69 | + `${BACKEND_DOMAIN}/ingredients/register/update`, |
| 70 | + ingredients, |
| 71 | + { |
| 72 | + headers: { |
| 73 | + Authorization: `Bearer ${token}`, |
| 74 | + }, |
| 75 | + } |
| 76 | + ); |
| 77 | + return response.data; |
| 78 | + } catch (error) { |
| 79 | + if (error.response) { |
| 80 | + console.log(error.response.status); |
| 81 | + console.log(error.response.data); |
| 82 | + } |
| 83 | + throw error; |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +export const getFridgeIngredients = async () => { |
| 88 | + const token = localStorage.getItem("accessToken"); |
| 89 | + try { |
| 90 | + const response = await axios.get(`${BACKEND_DOMAIN}/ingredients`, { |
| 91 | + headers: { |
| 92 | + Authorization: `Bearer ${token}`, |
| 93 | + }, |
| 94 | + }); |
| 95 | + return response.data; |
| 96 | + } catch (error) { |
| 97 | + if (error.response) { |
| 98 | + console.log(error.response.status); |
| 99 | + console.log(error.response.data); |
| 100 | + } |
| 101 | + throw error; |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +export const filterFridgeIngredientsByCategory = async (category) => { |
| 106 | + const token = localStorage.getItem("accessToken"); |
| 107 | + try { |
| 108 | + const response = await axios.get(`${BACKEND_DOMAIN}/ingredients`, { |
| 109 | + headers: { |
| 110 | + Authorization: `Bearer ${token}`, |
| 111 | + }, |
| 112 | + params: { |
| 113 | + category: category, |
| 114 | + }, |
| 115 | + }); |
| 116 | + return response.data; |
| 117 | + } catch (error) { |
| 118 | + if (error.response) { |
| 119 | + console.log(error.response.status); |
| 120 | + console.log(error.response.data); |
| 121 | + } |
| 122 | + throw error; |
| 123 | + } |
| 124 | +}; |
0 commit comments