Skip to content

Commit

Permalink
Added equipment API helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
davewalker5 committed Dec 3, 2023
1 parent 01ee157 commit da8423f
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 7 deletions.
223 changes: 223 additions & 0 deletions src/music-catalogue-ui/helpers/api/apiEquipment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import config from "@/config.json";
import { apiReadResponseData } from "./apiUtils";
import { apiGetPostHeaders, apiGetHeaders } from "./apiHeaders";

/**
* Create an item of equipment
* @param {*} equipmentTypeId
* @param {*} manufacturerId
* @param {*} description
* @param {*} model
* @param {*} serialNumber
* @param {*} isWishListItem
* @param {*} purchased
* @param {*} price
* @param {*} retailerId
* @param {*} logout
* @returns
*/
const apiCreateEquipment = async (
equipmentTypeId,
manufacturerId,
description,
model,
serialNumber,
isWishListItem,
purchased,
price,
retailerId,
logout
) => {
// Create the request body
const body = JSON.stringify({
equipmentTypeId: equipmentTypeId,
manufacturerId: manufacturerId,
description: description,
model: model,
serialNumber: serialNumber,
isWishListItem: isWishListItem,
purchased: purchased,
price: price,
retailerId: retailerId,
});

// Call the API to create the equipment
const url = `${config.api.baseUrl}/equipment`;
const response = await fetch(url, {
method: "POST",
headers: apiGetPostHeaders(),
body: body,
});

const equipment = await apiReadResponseData(response, logout);
return equipment;
};

/**
* Update an item of equipment
* @param {*} id
* @param {*} equipmentTypeId
* @param {*} manufacturerId
* @param {*} description
* @param {*} model
* @param {*} serialNumber
* @param {*} isWishListItem
* @param {*} purchased
* @param {*} price
* @param {*} retailerId
* @param {*} logout
* @returns
*/
const apiUpdateEquipment = async (
id,
equipmentTypeId,
manufacturerId,
description,
model,
serialNumber,
isWishListItem,
purchased,
price,
retailerId,
logout
) => {
// Construct the body
const body = JSON.stringify({
id: id,
equipmentTypeId: equipmentTypeId,
manufacturerId: manufacturerId,
description: description,
model: model,
serialNumber: serialNumber,
isWishListItem: isWishListItem,
purchased: purchased,
price: price,
retailerId: retailerId,
});

// Call the API to set the wish list flag for a given album
const url = `${config.api.baseUrl}/equipment`;
const response = await fetch(url, {
method: "PUT",
headers: apiGetPostHeaders(),
body: body,
});

const equipment = await apiReadResponseData(response, logout);
return equipment;
};

/**
* Set the wish list flag on an item of equipment
* @param {*} equipment
* @param {*} wishListFlag
* @param {*} logout
* @returns
*/
const apiSetEquipmentWishListFlag = async (equipment, wishListFlag, logout) => {
// Send the update request to the API and return the response
const response = await apiUpdateEquipment(
equipment.id,
equipment.equipmentTypeId,
equipment.manufacturerId,
equipment.description,
equipment.model,
equipment.serialNumber,
wishListFlag,
equipment.purchased,
equipment.price,
equipment.retailerId,
logout
);
return response;
};

/**
* Set the purchase details for the specified item of equipment
* @param {*} equipment
* @param {*} purchaseDate
* @param {*} price
* @param {*} retailerId
* @param {*} logout
* @returns
*/
const apiSetEquipmentPurchaseDetails = async (
equipment,
purchaseDate,
price,
retailerId,
logout
) => {
// Send the update request to the API and return the response
const response = await apiUpdateEquipment(
equipment.id,
equipment.equipmentTypeId,
equipment.manufacturerId,
equipment.description,
equipment.model,
equipment.serialNumber,
equipment.IsWishListItem,
purchaseDate,
price,
retailerId,
logout
);
return response;
};

/**
* Delete the item of equipment with the specified ID
* @param {*} equipmentId
* @param {*} logout
* @returns
*/
const apiDeleteEquipment = async (equipmentId, logout) => {
// Call the API to delete the specified item of equipment
const url = `${config.api.baseUrl}/equipment/${equipmentId}`;
const response = await fetch(url, {
method: "DELETE",
headers: apiGetHeaders(),
});

if (response.status == 401) {
// Unauthorized so the token's likely expired - force a login
logout();
} else {
// Return the response status code
return response.ok;
}
};

/**
* Return a list of equipment
* @param {*} isWishList
* @param {*} logout
* @returns
*/
const apiFetchEquipment = async (isWishList, logout) => {
// Construct the filtering criteria as the request body and convert to JSON
const criteria = {
wishList: isWishList,
};
const body = JSON.stringify(criteria);

// Call the API to retrieve the matching equipment list
const url = `${config.api.baseUrl}/equipment/search`;
const response = await fetch(url, {
method: "POST",
headers: apiGetPostHeaders(),
body: body,
});

const equipment = await apiReadResponseData(response, logout);
return equipment;
};

export {
apiCreateEquipment,
apiUpdateEquipment,
apiDeleteEquipment,
apiFetchEquipment,
apiSetEquipmentWishListFlag,
apiSetEquipmentPurchaseDetails,
};
14 changes: 7 additions & 7 deletions src/music-catalogue-ui/helpers/api/apiManufacturers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const apiCreateManufacturer = async (name, logout) => {
body: body,
});

const equipmentType = await apiReadResponseData(response, logout);
return equipmentType;
const manufacturers = await apiReadResponseData(response, logout);
return manufacturers;
};

/**
Expand All @@ -49,19 +49,19 @@ const apiUpdateManufacturer = async (id, name, logout) => {
body: body,
});

const equipmentType = await apiReadResponseData(response, logout);
return equipmentType;
const manufacturers = await apiReadResponseData(response, logout);
return manufacturers;
};

/**
* Delete the manufacturer with the specified ID
* @param {*} equipmentTypeId
* @param {*} manufacturerId
* @param {*} logout
* @returns
*/
const apiDeleteManufacturer = async (equipmentTypeId, logout) => {
const apiDeleteManufacturer = async (manufacturerId, logout) => {
// Call the API to delete the specified manufacturer
const url = `${config.api.baseUrl}/manufacturers/${equipmentTypeId}`;
const url = `${config.api.baseUrl}/manufacturers/${manufacturerId}`;
const response = await fetch(url, {
method: "DELETE",
headers: apiGetHeaders(),
Expand Down
30 changes: 30 additions & 0 deletions src/music-catalogue-ui/hooks/useEquipment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { apiFetchEquipment } from "@/helpers/api/apiEquipment";
import { useState, useEffect } from "react";

/**
* Hook that uses the API helpers to retrieve a list of equipment from
* the Music Catalogue REST API
* @param {*} isWishList
* @param {*} logout
* @returns
*/
const useEquipment = (isWishList, logout) => {
// Current list of albums and the method to change it
const [equipment, setEquipment] = useState([]);

useEffect(() => {
const fetchEquipment = async () => {
try {
// Get a list of albums via the service and store it in state
var fetchedEquipment = await apiFetchEquipment(isWishList, logout);
setEquipment(fetchedEquipment);
} catch {}
};

fetchEquipment();
}, [isWishList, logout]);

return { equipment, setEquipment };
};

export default useEquipment;

0 comments on commit da8423f

Please sign in to comment.