Former-commit-id: eb3b737613fb8c237072e03506cc45c4c9b87bdc
This commit is contained in:
Ben
2022-04-25 00:37:42 +01:00
parent 7a84d3a632
commit 4ce954cd4d
5 changed files with 96 additions and 7 deletions

View File

@@ -118,6 +118,12 @@ class BasketPopout extends Component {
}
OnMount() {
this.setState({
items: {},
total: 0,
subtotal: 0,
}, false);
this.OnLocalBasketUpdate(Object.bind(this));
basketCallback = this.OnLocalBasketUpdate.bind(this);
@@ -144,7 +150,8 @@ class BasketPopout extends Component {
{this.state.total} Items
</div>
<div class="popup-content">
${this.state.items ? Object.keys(this.state.items).map((key) => {
${this.state.items
? Object.keys(this.state.items).map((key) => {
const item = this.state.items[key];
return /* html */`
<div class="popup-content-item">

View File

@@ -112,6 +112,7 @@ class ProductListing extends Component {
`;
}
console.log(this.state);
return {
template: /* html */`
<div class="product-page">
@@ -133,7 +134,7 @@ class ProductListing extends Component {
</div>
<div class="product-name">{this.state.name} {this.state.id}</div>
${this.state.discountbar
${this.state.discount
? `<span class="product-listing-price-full">£${parseFloat(this.state.price).toFixed(2)}</span><span class="product-listing-price-new">£${parseFloat(this.state.discount).toFixed(2)}</span>`
: `<span class="product-listing-price">£${parseFloat(this.state.price).toFixed(2)}</span>`}
<div class="product-description">${this.state.description || this.state.name + ' ' + this.state.tag}</div>

View File

@@ -62,6 +62,32 @@ async function Search(fuzzyString) {
return bricks;
}
async function SumPrices(bricksArr, quantityArray) {
await Database.Query('BEGIN TRANSACTION;');
const dbres = await Database.Query(PgFormat(`
SELECT COALESCE(new_price, price) AS "price"
FROM lego_brick
LEFT JOIN lego_brick_inventory AS brick_inventory ON brick_inventory.brick_id = lego_brick.id
WHERE lego_brick.id IN (%L);
`, bricksArr), []);
await Database.Query('END TRANSACTION;');
// validate database response
if (dbres.rows.length === 0) {
return {
error: 'Bricks not found',
long: 'The bricks you are looking for do not exist',
};
}
let sum = 0;
for (let i = 0; i < dbres.rows.length; i++) {
sum += parseFloat(dbres.rows[i].price) * parseFloat(quantityArray[i]);
}
return sum;
}
async function GetBulkBricks(bricksArr) {
await Database.Query('BEGIN TRANSACTION;');
const dbres = await Database.Query(PgFormat(`
@@ -101,7 +127,7 @@ async function GetBrick(brickId) {
inv.last_updated AS "last_stock_update",
weight, dimensions_x, dimensions_y, dimensions_z
FROM lego_brick
LEFT JOIN lego_brick_inventory AS inv on inv.brick_id = lego_brick.id
LEFT JOIN lego_brick_inventory AS inv ON inv.brick_id = lego_brick.id
LEFT JOIN lego_brick_tag AS tags ON tags.brick_id = lego_brick.id
LEFT JOIN tag AS tag ON tags.tag = tag.id
WHERE lego_brick.id = $1;
@@ -142,6 +168,7 @@ async function GetBrick(brickId) {
module.exports = {
Search,
SumPrices,
GetBulkBricks,
GetBrick,
};

View File

@@ -1,6 +1,8 @@
const ControllerMaster = require('./controller-master.js');
const Database = require('../database/database.js');
const PgFormat = require('pg-format');
async function Search(fuzzyString) {
await Database.Query('BEGIN TRANSACTION;');
const dbres = await Database.Query(`
@@ -70,6 +72,33 @@ async function Search(fuzzyString) {
return sets;
}
async function SumPrices(setsArray, quantityArray) {
await Database.Query('BEGIN TRANSACTION;');
const dbres = await Database.Query(PgFormat(`
SELECT COALESCE(new_price, price) AS "price"
FROM lego_set
LEFT JOIN lego_set_inventory AS set_inventory ON set_inventory.set_id = lego_set.id
WHERE lego_set.id IN (%L);
`, setsArray), []);
await Database.Query('END TRANSACTION;');
// validate database response
if (dbres.rows.length === 0) {
return {
error: 'Bricks not found',
long: 'The bricks you are looking for do not exist',
};
}
let sum = 0;
console.log(dbres);
for (let i = 0; i < dbres.rows.length; i++) {
sum += parseFloat(dbres.rows[i].price) * parseFloat(quantityArray[i]);
}
return sum;
}
async function GetSet(setId) {
await Database.Query('BEGIN TRANSACTION;');
const dbres = await Database.Query(`
@@ -160,6 +189,7 @@ async function GetSets(page, resPerPage) {
module.exports = {
Search,
SumPrices,
GetSet,
GetSets,
};

View File

@@ -1,7 +1,10 @@
const BrickController = require('../controllers/brick-controller.js');
const SetController = require('../controllers/set-controller.js');
// AppEng Deadline
const EndDate = new Date(1651269600 * 1000);
function Special(req, res, next) {
function Special(req, res) {
res.send({
data: {
title: '£10 off any LEGO set! Limited Time Only! use code: LEGO10',
@@ -10,10 +13,31 @@ function Special(req, res, next) {
});
}
function CalculateBasketPrice(req, res) {
async function CalculateBasketPrice(req, res) {
const setList = [];
const setQuantities = [];
const brickList = [];
const brickQuantities = [];
for (const [item, value] of Object.entries(req.body.items)) {
if (value.type === 'set') {
setList.push(item.split('~')[0]);
setQuantities.push(value.quantity);
}
if (value.type === 'brick') {
brickList.push(item.split('~')[0]);
brickQuantities.push(value.quantity);
}
}
const setSubtotal = await SetController.SumPrices(setList, setQuantities);
const brickSubtotal = await BrickController.SumPrices(brickList, brickQuantities);
console.log(setSubtotal, brickSubtotal);
res.send({
data: {
subtotal: 10,
subtotal: parseFloat(setSubtotal) + parseFloat(brickSubtotal),
},
});
}