source: frontend/src/api.js

Last change on this file was 09e02d7, checked in by Nikola Sarafimov <sarafimov.nikola12345@…>, 4 days ago

Final room reservation system implementation

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[09e02d7]1const API_BASE_URL =
2 import.meta.env.VITE_API_BASE_URL || "http://localhost:8080/api";
3
4async function request(path, options = {}) {
5 const response = await fetch(`${API_BASE_URL}${path}`, {
6 headers: {
7 "Content-Type": "application/json",
8 ...(options.headers || {}),
9 },
10 ...options,
11 });
12
13 if (!response.ok) {
14 let message = `Request failed with status ${response.status}`;
15
16 try {
17 const data = await response.json();
18 message = data.message || message;
19 } catch {
20 try {
21 const text = await response.text();
22 if (text) {
23 message = text;
24 }
25 } catch {
26 // ignore
27 }
28 }
29
30 throw new Error(message);
31 }
32
33 if (response.status === 204) {
34 return null;
35 }
36
37 return response.json();
38}
39
40export function getHealth() {
41 return request("/health");
42}
43
44export function getDashboardStats() {
45 return request("/dashboard");
46}
47
48export function getUsers() {
49 return request("/users");
50}
51
52export function getApprovers() {
53 return request("/approvers");
54}
55
56export function getEquipment() {
57 return request("/equipment");
58}
59
60export function getRooms() {
61 return request("/rooms");
62}
63
64export function searchRooms(payload) {
65 return request("/rooms/search", {
66 method: "POST",
67 body: JSON.stringify(payload),
68 });
69}
70
71export function createReservation(payload) {
72 return request("/reservations", {
73 method: "POST",
74 body: JSON.stringify(payload),
75 });
76}
77
78export function loginUser(payload) {
79 return request("/auth/login", {
80 method: "POST",
81 body: JSON.stringify(payload),
82 });
83}
84
85export function registerUser(payload) {
86 return request("/auth/register", {
87 method: "POST",
88 body: JSON.stringify(payload),
89 });
90}
91
92export function getReservation(id) {
93 return request(`/reservations/${id}`);
94}
95
96export function getPendingReservations() {
97 return request("/reservations/pending");
98}
99
100export function approveReservation(payload) {
101 return request("/approvals", {
102 method: "POST",
103 body: JSON.stringify(payload),
104 });
105}
106
107export function getRoomUtilizationReport() {
108 return request("/reports/room-utilization");
109}
110
111export function getEquipmentDemandReport() {
112 return request("/reports/equipment-demand");
113}
Note: See TracBrowser for help on using the repository browser.