Files
dps-webshop/01-frontend/src/helper/query/Queries.tsx

136 lines
3.9 KiB
TypeScript

// api/queries.js
import AccountType, { CustomerType, SubmitLogin } from "../../components/Account";
import { SubmitOrder } from "../../components/Order";
import RatingSubmitType from "../../components/RatingSubmit";
export const fetchItemList = async () => {
const response = await fetch('http://localhost:8085/article/all');
console.log("API Response:", response);
if (!response.ok) {
throw new Error('Fehler beim Laden der Items');
}
const data = await response.json();
return data;
};
export const submitRating = async (ratingData: RatingSubmitType) => {
const response = await fetch('http://localhost:8085/review?uuid=' + ratingData.articleId + '&rating=' + ratingData.rating * 2, {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
},
body: ratingData.content,
});
if (!response.ok) {
throw new Error('Fehler beim Senden der Bewertung');
}
const data = await response.json();
return data;
}
export const fetchRatingList = async (itemId: string) => {
const response = await fetch('http://localhost:8085/review/all?uuid=' + itemId);
console.log("API Response:", response);
if (!response.ok) {
throw new Error('Fehler beim Laden der Items');
}
const data = await response.json();
return data;
};
export const submitOrder = async (data: SubmitOrder) => {
const response = await fetch('http://localhost:8085/order', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error('Fehler beim Senden der Bestellung');
}
return await response.json();
}
export const submitAccount = async (data: AccountType) => {
const response = await fetch('http://localhost:8085/account', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error('Fehler beim Senden des Accounts');
}
return await response.json();
}
export const submitCustomer = async (data: CustomerType) => {
const response = await fetch('http://localhost:8085/customer', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error('Fehler beim Senden des Accounts');
}
return await response.json();
}
export const submitLogin = async (loginData: SubmitLogin) => {
const response = await fetch("http://localhost:8085/session?email=" + loginData.email + "&password=" + loginData.password, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(loginData),
});
if (!response.ok) {
throw new Error("Login failed");
}
return response.json();
};
export const fetchAccount = async (loginData: SubmitLogin) => {
const response = await fetch("http://localhost:8085/account?email=" + loginData.email + "&password=" + loginData.password);
if (!response.ok) {
throw new Error("Login failed");
}
return response.json();
};
export const submitRegister = async (registerData: AccountType) => {
const response = await fetch("http://localhost:8085/account", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(registerData),
});
if (!response.ok) {
throw new Error("Login failed");
}
return response.json();
};
export const fetchCustomer = async (userId: number) => {
const response = await fetch('http://localhost:8085/customer?id=' + userId);
console.log("API Response:", response);
if (!response.ok) {
throw new Error('Fehler beim Laden des Customers');
}
const data = await response.json();
return data;
};