Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.
Open
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
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*/node_modules
.env
20 changes: 20 additions & 0 deletions 2-api/controllers/availabilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { checkExistById } from "../utils/functions";
import { client } from "../server";
import express, { Express, Request, Response } from "express";

export const getAllAvailabilities = async (req: Request, res: Response) => {
const { prod_id, supp_id, unit_price } = req.body;
try {
if (!Number.isInteger(Number(unit_price)) || Number(unit_price) <= 0) {
throw new Error(`Price must be a positive integer.`);
}
const prodId = await checkExistById("products", prod_id);
const suppId = await checkExistById("suppliers", supp_id);
const insertSql = `INSERT INTO product_availability (prod_id,supp_id,unit_price) VALUES ($1,$2,$3) RETURNING *`;
const insert = await client.query(insertSql, [prodId, suppId, unit_price]);
res.status(200).json(insert.rows[0]);
} catch (err: any) {
console.error(err);
res.status(400).json({ error: err.message });
}
};
82 changes: 82 additions & 0 deletions 2-api/controllers/customers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { queryAndRespond, checkExistById } from "../utils/functions";
import express, { Express, Request, Response } from "express";
import { client } from "../server";

export const getAllCustomers = (req: Request, res: Response) =>
queryAndRespond(res, "SELECT * from customers");

export const getCustomer = (req: Request, res: Response) => {
const sql = `SELECT * from customers ${
req.params.customerId ? `WHERE id = ${req.params.customerId}` : ""
}`;
queryAndRespond(res, sql);
};
export const createCustomer = (req: Request, res: Response) => {
const { name, address, city, country } = req.body;
if (!name || !address || !city || !country) {
res.status(400).json({ error: `Name,Address,City,Country are required` });
}
const sql = `INSERT INTO customers (name,address,city,country) VALUES ($1,$2,$3,$4) RETURNING *`;
queryAndRespond(res, sql, [name, address, city, country]);
};
export const getOrders = (req: Request, res: Response) => {
const sql = `select o.order_reference, o.order_date, p.product_name, pa.unit_price, s.supplier_name , i.quantity from customers c JOIN orders o ON c.id=o.customer_id JOIN order_items i ON i.order_id=o.id JOIN product_availability pa ON pa.prod_id = i.product_id JOIN suppliers s ON s.id=pa.supp_id JOIN products p ON p.id=pa.prod_id WHERE c.id=$1;`;
queryAndRespond(res, sql, [req.params.customerId]);
};

export const createOrder = async (req: Request, res: Response) => {
const { order_date, order_reference } = req.body;
try {
const customerId = await checkExistById("customers", req.params.customerId);
const insertSql = `INSERT INTO orders (order_date, order_reference, customer_id) VALUES ($1,$2,$3) RETURNING *`;
const insert = await client.query(insertSql, [
order_date,
order_reference,
customerId,
]);
return res.status(200).json(insert.rows[0]);
} catch (err: any) {
console.error(err);
return res.status(400).json({ error: err.message });
}
};

export const updateCustomer = async (req: Request, res: Response) => {
try {
const customerId = await checkExistById("customers", req.params.customerId);
const updateSql = `UPDATE customers SET name=$1, address=$2, city=$3 , country=$4 WHERE id = $5 RETURNING *`;
const update = await client.query(updateSql, [
req.body.name,
req.body.address,
req.body.city,
req.body.country,
customerId,
]);
return res.status(200).json(update.rows[0]);
} catch (err: any) {
console.error(err);
return res.status(400).json({ error: err.message });
}
};

export const deleteCustomer = async (req: Request, res: Response) => {
try {
const checkIfExist = await client.query(
`select * from orders where customer_id=$1`,
[req.params.customerId]
);

if (checkIfExist.rows[0].length > 0) {
return res
.status(400)
.json({ error: "Can't delete customer with existing orders" });
} else {
const deleteSql = `DELETE FROM orders WHERE id=$1 RETURNING *`;
const result = await client.query(deleteSql, [req.params.customerId]);
return res.status(200).json(result.rows[0]);
}
} catch (err: any) {
console.error(err);
return res.status(400).json({ error: err.message });
}
};
12 changes: 12 additions & 0 deletions 2-api/controllers/orders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import express, { Express, Request, Response } from "express";
import { client } from "../server";
export const deleteOrder = async (req: Request, res: Response) => {
try {
const deleteSql = `DELETE FROM orders WHERE id=$1 RETURNING *`;
const result = await client.query(deleteSql, [req.params.orderId]);
return res.status(200).json(result.rows[0]);
} catch (err: any) {
console.error(err);
return res.status(400).json({ error: err.message });
}
};
18 changes: 18 additions & 0 deletions 2-api/controllers/products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { queryAndRespond } from "../utils/functions";
import express, { Express, Request, Response } from "express";

export const getAllProducts = (req: Request, res: Response) => {
const sql = `SELECT CAST(row_number() over () AS INTEGER) as id,p.product_name,pa.unit_price,s.supplier_name from products p JOIN product_availability pa ON p.id = pa.prod_id JOIN suppliers s ON pa.supp_id = s.id ${
req.query.name ? `WHERE p.product_name ILIKE '%${req.query.name}%'` : ""
}`;
queryAndRespond(res, sql);
};

export const createProduct = (req: Request, res: Response) => {
const { product_name } = req.body;
if (!product_name) {
return res.status(400).json({ error: `Product name is required` });
}
const sql = `INSERT INTO products (product_name) VALUES ($1) RETURNING *`;
queryAndRespond(res, sql, [product_name]);
};
5 changes: 5 additions & 0 deletions 2-api/controllers/suppliers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { queryAndRespond } from "../utils/functions";
import express, { Express, Request, Response } from "express";

export const getAllSuppliers = (req: Request, res: Response) =>
queryAndRespond(res, "SELECT * from suppliers");
Loading