47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
import { Pool } from "pg";
|
|
|
|
const pool = new Pool();
|
|
|
|
export const getAllRecipes = async (req, res) => {
|
|
try {
|
|
const result = await pool.query("SELECT * FROM recipes");
|
|
res.json(result.rows);
|
|
} catch (error) {
|
|
console.error(error);
|
|
res.status(500).json({ message: "Error fetching recipes" });
|
|
}
|
|
};
|
|
|
|
export const addRecipe = async (req, res) => {
|
|
const { title, description, ingredients, tags, image_url } = req.body;
|
|
|
|
if (!title || !description || !ingredients) {
|
|
return res.status(400).json({ message: "Title, description, and ingredients are required" });
|
|
}
|
|
|
|
try {
|
|
const result = await pool.query(
|
|
"INSERT INTO recipes (title, description, ingredients, tags, image_url) VALUES ($1, $2, $3, $4, $5) RETURNING *",
|
|
[title, description, ingredients, tags, image_url]
|
|
);
|
|
res.status(201).json(result.rows[0]);
|
|
} catch (error) {
|
|
console.error(error);
|
|
res.status(500).json({ message: "Error adding recipe" });
|
|
}
|
|
};
|
|
|
|
export const searchRecipesByIngredient = async (req, res) => {
|
|
const { ingredient } = req.query;
|
|
|
|
try {
|
|
const result = await pool.query(
|
|
"SELECT * FROM recipes WHERE $1 = ANY (ingredients)",
|
|
[ingredient]
|
|
);
|
|
res.json(result.rows);
|
|
} catch (error) {
|
|
console.error(error);
|
|
res.status(500).json({ message: "Error searching recipes" });
|
|
}
|
|
};
|