Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

141 refactor services #171

Merged
merged 6 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/deskstar-frontend/components/AddResourceModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
createDeskType,
createFloor,
createRoom,
getDeskTypes,
getFloors,
getRooms,
} from "../lib/api/ResourceService";
Expand Down Expand Up @@ -87,16 +86,18 @@ const AddResourceModal = ({
async function onSelectedBuildingChange(
selectedBuilding: IBuilding | null | undefined
) {
if (!selectedBuilding) {
return;
}
if (!selectedBuilding) return;

setBuilding(selectedBuilding);
if (!session) {
return [];

if (!session) return [];

try {
const resFloors = await getFloors(session, selectedBuilding.buildingId);
setFloors(resFloors);
} catch (error) {
toast.error(`${error}`);
}
const resFloors = await getFloors(session, selectedBuilding.buildingId);
setFloors(resFloors);

setFloor(null);
setRoom(null);
Expand All @@ -105,26 +106,25 @@ const AddResourceModal = ({
async function onSelectedFloorChange(
selectedFloor: IFloor | null | undefined
) {
if (!selectedFloor) {
return;
}
if (!selectedFloor) return;

setFloor(selectedFloor);
if (!session) {
return [];
if (!session) return [];

try {
const resRooms = await getRooms(session, selectedFloor.floorId);
setRooms(resRooms);
} catch (error) {
toast.error(`${error}`);
}

const resRooms = await getRooms(session, selectedFloor.floorId);
setRooms(resRooms);
setRoom(null);
}

async function onSelectedDeskTypeChange(
onSelectedDeskType: IDeskType | null | undefined
) {
if (!onSelectedDeskType) {
return;
}
if (!onSelectedDeskType) return;
setDeskType(onSelectedDeskType);
}

Expand Down
53 changes: 30 additions & 23 deletions src/deskstar-frontend/components/Filterbar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useSession } from "next-auth/react";
import { useState, Fragment } from "react";
import { toast } from "react-toastify";
import { getDesks, getFloors, getRooms } from "../lib/api/ResourceService";
import { IBuilding } from "../types/building";
import { IDesk } from "../types/desk";
Expand Down Expand Up @@ -78,9 +79,12 @@ export default function Filterbar({
return;
}

const floors = await getFloors(session, selectedBuilding.buildingId);

setFloors(floors);
try {
const floors = await getFloors(session, selectedBuilding.buildingId);
setFloors(floors);
} catch (error) {
toast.error(`${error}`);
}
setSelectedFloor(null);
}

Expand All @@ -97,37 +101,40 @@ export default function Filterbar({
return;
}

const rooms = await getRooms(session, selectedFloor.floorId);
try {
const rooms = await getRooms(session, selectedFloor.floorId);
setRooms(rooms);
} catch (error) {
toast.error(`${error}`);
}

setRooms(rooms);
setSelectedRoom(null);
}

async function setSelectedRoom(selectedRoom: IRoom | null) {
_setSelectedRoom(selectedRoom);
if (!selectedRoom) {
return;
}
if (!selectedRoom) return;

if (!session) {
setRooms([]);
return;
}
if (!session) return setRooms([]);

const desks = await getDesks(
session,
selectedRoom.roomId,
startDateTime.getTime(),
endDateTime.getTime()
);
try {
const desks = await getDesks(
session,
selectedRoom.roomId,
startDateTime.getTime(),
endDateTime.getTime()
);

const filteredDesks = desks.filter((desk) => desk.bookings.length === 0);
const filteredDesks = desks.filter((desk) => desk.bookings.length === 0);

setDeskTypes(deskTypes);
setSelectedDeskType(null); // Equals all there
setDeskTypes(deskTypes);
setSelectedDeskType(null); // Equals all there

setDesks(filteredDesks);
setFilteredDesks(filteredDesks);
setDesks(filteredDesks);
setFilteredDesks(filteredDesks);
} catch (error) {
toast.error(`${error}`);
}
}

function setSelectedDeskType(selectedDeskType: IDeskType | null) {
Expand Down
111 changes: 64 additions & 47 deletions src/deskstar-frontend/lib/api/BookingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ function getParams(queryOptions: QueryOptions) {
return params;
}

/**
* Gets list of bookings associated with user session
* @param session The associated user session
* @param queryOptions The Query parameter for the request
* @returns The total amount of bookings and list of bookings according to `queryOptions`
* @throws Error containing status code and/or error message
*/
export async function getBookings(
session: Session,
queryOptions: QueryOptions
Expand All @@ -47,12 +54,7 @@ export async function getBookings(
},
});

if (response.status !== 200) {
return {
amountOfBookings: 0,
bookings: [],
};
}
if (!response.ok) throw Error(`${response.status} ${response.statusText}`);

const data = await response.json();

Expand All @@ -74,6 +76,15 @@ export async function getBookings(
return bookingsResponse;
}

/**
* Creates a user booking
* @param session The associated user session
* @param deskId The desk id associated to the booking
* @param startTime Start time of the booking
* @param endTime End time of the booking
* @returns The response object
* @throws Error containing status code and/or error message
*/
export async function createBooking(
session: Session,
deskId: string,
Expand All @@ -92,35 +103,34 @@ export async function createBooking(
endTime,
}),
});
console.log(
JSON.stringify({
deskId,
startTime,
endTime,
})
);
console.log(await response.status);
if (response.status !== 200) {

if (!response.ok) {
let text = await response.text();
// take away the quotes
text = text.substring(1, text.length - 1);
console.log(text);
switch (text) {
case "User not found":
return "User not found";
case "Desk not found":
return "The desk you are trying to book does not exist";
case "Time slot not available":
return "The time slot you are trying to book is not available";
default:
return "An unknown error occurred";
}
} else {
return "success";

const textToErrorMessage: { [key: string]: string } = {
"User not found": "User not found",
"Desk not found": "The desk you are trying to book does not exist",
"Time slot not available":
"The time slot you are trying to book is not available",
};

throw Error(
`${response.status} ${textToErrorMessage[text] ?? response.statusText}`
);
}

return response;
}

// create function for deleting booking. BookingId is passed as query parameter
/**
* Deletes a booking associated to an user
* @param session The associated user session
* @param bookingId The bookings id
* @returns The response object
* @throws Error containing status code and/or error message
*/
export async function deleteBooking(session: Session, bookingId: string) {
const response = await fetch(BACKEND_URL + `/bookings/${bookingId}`, {
method: "DELETE",
Expand All @@ -130,40 +140,41 @@ export async function deleteBooking(session: Session, bookingId: string) {
});

// check if response is 200. If not return error message
if ((await response.status) !== 200) {
if (!response.ok) {
let text = await response.text();
text = text.substring(1, text.length - 1);
console.log(text);
switch (text) {
case "User not found":
return "User not found";
case "Booking not found":
return "The booking you are trying to delete does not exist";
case "You are not allowed to delete this booking":
return "You are not allowed to delete this booking";
default:
return "An unknown error occurred";
}
} else {
return "success";

const textToErrorMessage: { [key: string]: string } = {
"User not found": "User not found",
"Booking not found":
"The booking you are trying to delete does not exist",
"You are not allowed to delete this booking":
"You are not allowed to delete this booking",
};

throw Error(
`${response.status} ${textToErrorMessage[text] ?? response.statusText}`
);
}
return response;
}

/**
* Update start and end time for a given booking
* Update start and end time for a given booking associated to an user
* @param session the user session
* @param bookingId
* @param bookingId The bookings id
* @param startTime new start time for given booking
* @param endTime new end time for given booking
* @returns
* @returns The updated booking
* @throws Error containing status code and/or error message
*/
export async function updateBooking(
session: Session,
bookingId: string,
startTime: string,
endTime: string
) {
return await fetch(BACKEND_URL + `/bookings/${bookingId}`, {
const response = await fetch(BACKEND_URL + `/bookings/${bookingId}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Expand All @@ -174,4 +185,10 @@ export async function updateBooking(
endTime,
}),
});

if (!response.ok) throw Error(`${response.status} ${response.statusText}`);

//TODO: fix this
//return await response.json();
return response;
}
Loading