diff --git a/controllers/recipe.js b/controllers/recipe.js index 0d945d0..4d5b7d1 100644 --- a/controllers/recipe.js +++ b/controllers/recipe.js @@ -1,10 +1,58 @@ import query from '../config/db.js'; const recipeControllers = { - getAllRecipes: async (req, res) => {}, - getOneRecipe: async (req, res) => {}, + getAllRecipes: async (req, res) => { + try { + const recipes = await Recipe.getAllRecipes(); + res.render('home', { + title: 'Recipes', + path: '/', + recipes: recipes + }); + } catch (err) { + console.error('Error getting recipes:', err); + res.status(500).send('Error getting recipes'); + } + }, + getOneRecipe: async (req, res) => { + const { id } = req.params; + try { + const recipe = await Recipe.getRecipeById(id); + if (!recipe) { + return res.status(404).send('Recipe not found'); + } + res.render('recipe', { + id: id, + title: 'Recipe', + path: '/recipe', + recipe: recipe + }); + } catch (err) { + console.error(`Error getting recipe with ID ${id}:`, err); + res.status(500).send('Error getting recipe'); + } + }, postRecipe: async (req, res) => {}, - updateRecipe: async (req, res) => {}, + updateRecipe: async (req, res) => { const { id } = req.params; + const { updatedTitle } = req.body; + try { + const recipe = await Recipe.updateRecipe(id, updatedTitle); + if (!recipe) { + return res.status(404).send('Recipe not found'); + } + res.render('recipe', { + id: id, + updatedTitle: updatedTitle, + title: 'Recipe', + path: '/recipe', + recipe: recipe + }); + } catch (err) { + console.error(`Error updating recipe with ID ${id}:`, err); + res.status(500).send('Error updating recipe'); + } + + }, deleteRecipe: async (req, res) => {}, }; diff --git a/models/recipe.js b/models/recipe.js index 1db0496..46edaa9 100644 --- a/models/recipe.js +++ b/models/recipe.js @@ -2,7 +2,24 @@ import query from '../config/db.js'; const createRecipeTable = async () => { try { - } catch (err) {} + const sql = ` + CREATE TABLE IF NOT EXISTS recipes ( + id INT AUTO_INCREMENT PRIMARY KEY, + title VARCHAR(255) NOT NULL, + description TEXT, + ingredients TEXT, + steps TEXT, + cookingTime INT, + createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP + ) + `; + await query(sql); + console.log('Recipes table created successfully'); + } catch (err) { + console.error('Error creating recipes table:', err); + throw err; + } }; export default createRecipeTable; \ No newline at end of file