Skip to content

Commit

Permalink
implemented filters for /get route.
Browse files Browse the repository at this point in the history
  • Loading branch information
dddictionary committed Jun 4, 2024
1 parent cc47ac2 commit c326f08
Showing 1 changed file with 54 additions and 14 deletions.
68 changes: 54 additions & 14 deletions routes/projectsDB.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Router} from "express";
import { Router } from "express";
import connectDB from "../connectDB";
import express from "express";
import logger from "../utils/logger";
Expand All @@ -10,33 +10,68 @@ router.use(express.json());

router.get("/", async (req: any, res: any) => {
try {
await req.client.query('SELECT * FROM projects');
await req.client.query("SELECT * FROM projects");
return res.status(200).send("No errors!!");
} catch (err: any) {
return res.status(500).send(err.message);
}
});

router.get("/get", (req: any, res: any) => {
req.client.query('SELECT * FROM projects', (err: any, result: any) => {
let baseQuery = "SELECT * FROM projects";
const filters: string[] = [];
const values: (string | number)[] = [];

// if the name filter was provided
if (req.query.name) {
filters.push(`name ILIKE $${filters.length + 1}`);
values.push(`%${req.query.name}%`);
}

// if the cohort filter was provided
if (req.query.cohort) {
filters.push(`cohort ILIKE $${filters.length + 1}`);
values.push(`%${req.query.cohort}%`);
}
// if the team filter was provided
if (req.query.team) {
filters.push(`team ILIKE $${filters.length + 1}`);
values.push(`%${req.query.team}%`);
}

// combine all the filters into a single query
if (filters.length > 0) {
baseQuery += " WHERE " + filters.join(" AND ");
}

// execute the query, making sure to provide the values for the filters
req.client.query(baseQuery, values, (err: any, result: any) => {
if (err) {
logger.error("Error reading from database");
return res.status(500).send(err.message);
logger.error(`Error reading from database: ${err.message}`);
return res
.status(500)
.json({ message: "Error reading from database" });
}
if (result.rows.length === 0) {
logger.warn("No projects found");
return res.status(404).json({"message": "No projects found"});
}
return res.status(200).send(result.rows);
});
});

router.post("/add" , (req: any, res: any) => {
router.post("/add", (req: any, res: any) => {
const keys = Object.keys(req.body);
const values = Object.values(req.body);

const query = `INSERT INTO projects (${keys.map(key => {
if (key.includes("-")) {
return `"${key}"`;
}
return key;
}).join(", ")}) VALUES (${keys.map((key, i) => `$${i + 1}`).join(", ")})`;

const query = `INSERT INTO projects (${keys
.map((key) => {
if (key.includes("-")) {
return `"${key}"`;
}
return key;
})
.join(", ")}) VALUES (${keys.map((key, i) => `$${i + 1}`).join(", ")})`;

req.client.query(query, values, (err: any, result: any) => {
if (err) {
Expand All @@ -47,4 +82,9 @@ router.post("/add" , (req: any, res: any) => {
});
});

export default router;
router.post("/update", (req: any, res: any) => {
// TODO

});

export default router;

0 comments on commit c326f08

Please sign in to comment.