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
32 changes: 28 additions & 4 deletions ingredients/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require("path");
const express = require("express");
const router = express.Router();

const pg = require("pg");
// client side static assets
router.get("/", (_, res) => res.sendFile(path.join(__dirname, "./index.html")));
router.get("/client.js", (_, res) =>
Expand All @@ -14,13 +14,28 @@ router.get("/client.js", (_, res) =>

// connect to postgres

const pool = new pg.Pool({
user: "postgres",
host: "localhost",
password: "ryan",
database: "recipeguru",
port: 5432
})

router.get("/type", async (req, res) => {
const { type } = req.query;
console.log("get ingredients", type);

// return all ingredients of a type

res.status(501).json({ status: "not implemented", rows: [] });
const query = 'SELECT * FROM ingredients where type=$1';
const values = [type];
const result = await pool.query(query, values);
if ( result ) {
res.status(200).json({ status: "success", rows: result.rows });
}
else {
res.status(500).json({ status: "error", rows: [] });
}
});

router.get("/search", async (req, res) => {
Expand All @@ -29,9 +44,18 @@ router.get("/search", async (req, res) => {
console.log("search ingredients", term, page);

// return all columns as well as the count of all rows as total_count
const query = 'SELECT *, COUNT(*) OVER() AS total_count from ingredients where title ILIKE $1 OFFSET $2 LIMIT 5';

const values = [`%${term}%`, page * 5];
// make sure to account for pagination and only return 5 rows at a time
const result = await pool.query(query, values);

res.status(501).json({ status: "not implemented", rows: [] });
if ( result ) {
res.status(200).json({ status: "success", rows: result.rows });
}
else {
res.status(500).json({ status: "error", rows: [] });
}
});

/**
Expand Down