diff --git a/client/public/components/product-listing.mjs b/client/public/components/product-listing.mjs index cc4ad55..ee47c93 100644 --- a/client/public/components/product-listing.mjs +++ b/client/public/components/product-listing.mjs @@ -15,9 +15,10 @@ class ProductListing extends Component { const getURL = new URL(`/api/${type}/${id}`, document.baseURI); const data = await fetch(getURL).then(response => response.json()); + console.log(data); this.setState({ ...this.getState, - ...data.data[0], + ...data.data, }); } diff --git a/client/public/components/templates/product-listing.html b/client/public/components/templates/product-listing.html index fb74c9f..e58cf23 100644 --- a/client/public/components/templates/product-listing.html +++ b/client/public/components/templates/product-listing.html @@ -12,6 +12,11 @@
{this.state.name}
£{this.state.price}
+
{this.state.description}
Quantity: diff --git a/docs/API.md b/docs/API.md index 6d54fa8..5a29961 100644 --- a/docs/API.md +++ b/docs/API.md @@ -126,9 +126,10 @@ Response Object ```js { error: false - result: { + data: { // defined in the response description for each route } + // other important data, or metadata for the data can be added here } ``` @@ -136,10 +137,8 @@ Response Object ```js { - error: { - short: "Error doing x", - long: "y needs to be z", - } + error: "Error doing x", + long: "y needs to be z", } ``` diff --git a/src/controllers/set-controller.js b/src/controllers/set-controller.js index b0815ea..204f23b 100644 --- a/src/controllers/set-controller.js +++ b/src/controllers/set-controller.js @@ -5,27 +5,37 @@ function ValidateQuery(query) { const validation = ControllerMaster.ValidateQuery(query); if (!validation.isValid) { return { - isValid: false, error: validation.error, - longError: validation.longError, + long: validation.longError, }; } - return { - isValid: true, - }; + return true; } async function GetSet(setId) { await Database.Query('BEGIN TRANSACTION;'); const dbres = await Database.Query(` - SELECT id, name, price, new_price AS "discount", inv.stock + SELECT id, name, description, inv.price, date_released, weight, dimensions_x, dimensions_y, dimensions_z, new_price AS "discount", inv.stock, inv.last_updated AS "last_stock_update" FROM lego_set LEFT JOIN lego_set_inventory AS inv ON inv.set_id = lego_set.id - WHERE id = $1::int; - `); - console.log(dbres); + WHERE id = $1; + `, [setId]); await Database.Query('END TRANSACTION;'); + + // validate database response + if (dbres.rows.length === 0) { + return { + error: 'Set not found', + long: 'The set you are looking for does not exist', + }; + } + + const set = dbres.rows[0]; + set.image = `/api/cdn/${set.id}.png`; + set.type = 'set'; + + return set; } async function GetSets(page, resPerPage) { @@ -39,11 +49,19 @@ async function GetSets(page, resPerPage) { FROM lego_set LEFT JOIN lego_set_inventory as inv ON lego_set.id = inv.set_id ORDER BY id ASC - LIMIT $1::int - OFFSET $2::int`, + LIMIT $1 + OFFSET $2;`, [resPerPage, page * resPerPage]); await Database.Query('END TRANSACTION;'); + // validate database response + if (dbres.rows.length === 0) { + return { + error: 'No sets found', + long: 'There are no sets to display', + }; + } + const sets = dbres.rows; for (const set of sets) { diff --git a/src/routes/sets-router.js b/src/routes/sets-router.js index 279b489..98c90d9 100644 --- a/src/routes/sets-router.js +++ b/src/routes/sets-router.js @@ -4,44 +4,27 @@ const Database = require('../database/database.js'); async function Get(req, res) { // get id from url const id = req.params.id; - // query for the set - const dbres = await Database.Query(`SELECT - id, name, description, inv.price, inv.new_price AS "discount", stock - FROM lego_set - LEFT JOIN lego_set_inventory as inv ON lego_set.id = inv.set_id - WHERE id = $1::text;`, [id]); - // send the set - const set = dbres.rows[0]; - set.image = `/api/cdn/${set.id}.png`; - set.type = 'set'; + const set = await Controller.GetSet(id); + + if (set.error) { + res.send(JSON.stringify(set)); + return; + } res.send(JSON.stringify({ - data: [set], + data: set, })); - - // res.send(JSON.stringify({ - // data: [ - // { - // id: '1', - // name: 'Tail 4 x 1 x 3', - // description: 'Tail 4 x 1 x 3', - // price: '1.00', - // discount: '0.50', - // stock: '10', - // release_date: '2020-01-01', - // tags: ['tail', '4', '1', '3', '2020'], - // dimensions: { width: '1', height: '1', depth: '1' }, - // type: 'set', - // image: 'https://via.placeholder.com/320x320', - // }, - // ], - // })); } async function Featured(req, res) { // query all sets and return all of them const { sets } = await Controller.GetSets(0, 8); + if (sets.error) { + res.send(JSON.stringify(sets)); + return; + } + res.send(JSON.stringify({ data: [...sets], page: {