Skip to content
Open
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
100 changes: 21 additions & 79 deletions db/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function filterById(id) {
return newRows[0];
}

const fitlerProducts = async (query) => {
const fitlerProducts = (query) => {
let sortSQLQuery = "";
if (query.sort) {
const [column, direction] = query.sort
Expand Down Expand Up @@ -74,97 +74,39 @@ const fitlerProducts = async (query) => {
}
}

const { rows } = await pool.query(
`SELECT inventory.*, categories.category, categories.color FROM inventory JOIN categories ON category_id = categories.id ${categorySQLQuery} ${sortSQLQuery};`
);

return rows;
return `SELECT inventory.*, categories.category, categories.color FROM inventory JOIN categories ON category_id = categories.id ${categorySQLQuery} ${sortSQLQuery};`;
};

const getAllProducts = async (query = null) => {
try {
let mainQuery =
"SELECT inventory.*, categories.category, categories.color FROM inventory JOIN categories ON category_id = categories.id;";

if (query) {
return fitlerProducts(query);
mainQuery = fitlerProducts(query);
}

const { rows } = await pool.query(
"SELECT inventory.*, categories.category, categories.color FROM inventory JOIN categories ON category_id = categories.id;"
);
const { rows } = await pool.query(mainQuery);

return rows;
const newRows = rows.map((row) => {
const brightness = brightnessByColor(row.color);
let fontColor;

if (brightness > 130) {
fontColor = "#212529e0";
} else {
fontColor = "#fefefee0";
}

return { ...row, fontColor };
});

return newRows;
} catch (err) {
console.error(err);
}
};

// async function showAllProducts() {
// const { rows } = await pool.query(
// "SELECT inventory.*, categories.category, categories.color FROM inventory JOIN categories ON inventory.category_id = categories.id ORDER BY inventory.name ASC"
// );

// const newRows = rows.map((row) => {
// const brightness = brightnessByColor(row.color);
// let fontColor;

// if (brightness > 130) {
// fontColor = "#212529e0";
// } else {
// fontColor = "#fefefee0";
// }

// return { ...row, fontColor };
// });
// return newRows;
// }

// async function sortByPrice({ sort = "priceASC" }) {
// const order = sort.toUpperCase() === "PRICEDESC" ? "DESC" : "ASC";

// const { rows } = await pool.query(
// `SELECT inventory.*, categories.category, categories.color FROM inventory JOIN categories ON inventory.category_id = categories.id ORDER BY price ${order}`
// );
// return rows;
// }

// async function sortByName({ sort = "nameASC" }) {
// const order = sort.toUpperCase() === "NAMEDESC" ? "DESC" : "ASC";

// const { rows } = await pool.query(
// `SELECT inventory.*, categories.category, categories.color FROM inventory JOIN categories ON inventory.category_id = categories.id ORDER BY name ${order}`
// );
// return rows;
// }

// async function filterByCategory({ selectedCategory }) {
// // Make sure selectedCategory is an array to use with .map()
// if (!Array.isArray(selectedCategory)) {
// selectedCategory = [selectedCategory];
// }

// // Make placeholder for each selection "COLOR, "
// const placeholder = selectedCategory
// .map((_, index) => `$${index + 1}`)
// .join(", ");
// const query = `SELECT inventory.*, categories.category, categories.color FROM inventory JOIN categories ON inventory.category_id = categories.id WHERE categories.category IN (${placeholder})
// ORDER BY category ASC`;

// // Pass the query as well as the category for the placeholder function to work
// const { rows } = await pool.query(query, selectedCategory);
// const newRows = rows.map((row) => {
// const brightness = brightnessByColor(row.color);
// let fontColor;

// if (brightness > 130) {
// fontColor = "#212529e0";
// } else {
// fontColor = "#fefefee0";
// }

// return { ...row, fontColor };
// });
// return newRows;
// }

async function getCategories() {
const { rows } = await pool.query(
"SELECT * FROM categories ORDER BY category ASC;"
Expand Down