Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pamir perdeci #333

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
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
39 changes: 39 additions & 0 deletions api/recipes/recipes-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const knex = require('knex')(require('../../knexfile').development);

async function getRecipeById(recipe_id) {
const recipe = await knex('recipes').where({ recipe_id }).first();
if (!recipe) {
return null; // Recipe not found
}

const steps = await knex('steps').where({ recipe_id }).orderBy('step_number', 'asc');

for (let step of steps) {
const ingredientRows = await knex('step_ingredients')
.join('ingredients', 'step_ingredients.ingredient_id', 'ingredients.ingredient_id')
.where({ step_id: step.step_id })
.select('ingredients.ingredient_id', 'quantity');

step.ingredients = ingredientRows.map(row => ({
ingredient_id: row.ingredient_id,
quantity: row.quantity
}));

// Remove unwanted properties from step object
delete step.recipe_id;
delete step.step_id;
}

return {
recipe_name: recipe.recipe_name,
steps: steps.map(({ step_number, step_instructions, ingredients }) => ({
step_number,
step_instructions,
ingredients
}))
};
}

module.exports = {
getRecipeById
};
22 changes: 22 additions & 0 deletions api/recipes/recipes-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const express = require('express');
const router = express.Router();
const Recipes = require('./recipes-model'); // Adjust the path to your dataAccess file


// GET recipe by ID
router.get('/:id', async (req, res) => {
try {
const recipeId = parseInt(req.params.id);
const recipe = await Recipes.getRecipeById(recipeId);

if (!recipe) {
return res.status(404).json({ message: 'Recipe not found' });
}

res.json(recipe);
} catch (error) {
res.status(500).json({ message: 'Error retrieving recipe', error: error.message });
}
});

module.exports = router;
11 changes: 11 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express');
const server = express();
const recipesRouter = require('./recipes/recipes-router'); // Adjust the path to your router file

server.use(express.json());

server.use('/api/recipes', recipesRouter);



module.exports = server;
19 changes: 19 additions & 0 deletions data/migrations/20231203153804_recipes_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
return knex.schema.createTable('recipes', table => {
table.increments('recipe_id'); // Primary key
table.string('recipe_name').unique().notNullable();
table.timestamp('created_at').defaultTo(knex.fn.now());
}); // Added missing closing parenthesis here
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
return knex.schema.dropTableIfExists('recipes');
};
21 changes: 21 additions & 0 deletions data/migrations/20231203153924_steps_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
return knex.schema.createTable('steps', table => {
table.increments('step_id'); // Primary key
table.integer('recipe_id').unsigned().notNullable();
table.foreign('recipe_id').references('recipe_id').inTable('recipes');
table.integer('step_number').notNullable();
table.text('step_instructions').notNullable();
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
return knex.schema.dropTableIfExists('steps');
};
18 changes: 18 additions & 0 deletions data/migrations/20231203154014_ingridients_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
return knex.schema.createTable('ingredients', table => {
table.increments('ingredient_id'); // Primary key
table.string('ingredient_name').notNullable();
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
return knex.schema.dropTableIfExists('ingredients');
};
23 changes: 23 additions & 0 deletions data/migrations/20231203155732_create_step_ingredients_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
return knex.schema.createTable('step_ingredients', table => {
table.integer('step_id').unsigned().notNullable();
table.foreign('step_id').references('step_id').inTable('steps');
table.integer('ingredient_id').unsigned().notNullable();
table.foreign('ingredient_id').references('ingredient_id').inTable('ingredients');
table.float('quantity').notNullable();
table.primary(['step_id', 'ingredient_id']); // Composite primary key
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
return knex.schema.dropTableIfExists('step_ingredients');

};
16 changes: 16 additions & 0 deletions data/seeds/01_recipes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/




exports.seed = async function(knex) {
// Deletes ALL existing entries
await knex('recipes').del();
await knex('recipes').insert([
{ recipe_name: 'Spaghetti Bolognese', created_at: new Date() },
{ recipe_name: 'Chicken Curry', created_at: new Date() }
]);
};
14 changes: 14 additions & 0 deletions data/seeds/02_steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.seed = async function(knex) {
// Deletes ALL existing entries
await knex('steps').del();
await knex('steps').insert([
{ recipe_id: 1, step_number: 1, step_instructions: 'Boil water for spaghetti' },
{ recipe_id: 1, step_number: 2, step_instructions: 'Cook spaghetti for 10 minutes' },
{ recipe_id: 2, step_number: 1, step_instructions: 'Dice the chicken' },
{ recipe_id: 2, step_number: 2, step_instructions: 'Cook chicken in a pan' }
]);
};
14 changes: 14 additions & 0 deletions data/seeds/03_ingredients.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.seed = async function(knex) {
// Deletes ALL existing entries
await knex('ingredients').del();
await knex('ingredients').insert([
{ ingredient_name: 'Spaghetti' },
{ ingredient_name: 'Ground Beef' },
{ ingredient_name: 'Chicken Breast' },
{ ingredient_name: 'Curry Powder' }
]);
};
13 changes: 13 additions & 0 deletions data/seeds/04_step_ingredients.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.seed = async function(knex) {
// Deletes ALL existing entries
await knex('step_ingredients').del()
await knex('step_ingredients').insert([
{step_id: 1, ingredient_id: 1, quantity: 100}, // e.g., 100g of ingredient 1 used in step 1
{step_id: 1, ingredient_id: 2, quantity: 50}, // e.g., 50g of ingredient 2 used in step 1
{step_id: 2, ingredient_id: 3, quantity: 5} // e.g., 5ml of ingredient 3 used in step 2
]);
};
Binary file added dev.sqlite3
Binary file not shown.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const server = require('./api/server');



const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
35 changes: 35 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const sharedConfig = {
client: 'sqlite3',
connection: {
filename: './dev.sqlite3' // Your specified database file
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations' // Your specified migrations directory
},
seeds: {
directory: './data/seeds' // Your specified seeds directory
},
pool: {
afterCreate: (conn, done) => conn.run('PRAGMA foreign_keys = ON', done)
}
};

module.exports = {
development: {
...sharedConfig
},
testing: {
...sharedConfig,
connection: {
filename: './dev.sqlite3' // Change this if you have a separate test database
}
},
production: {
...sharedConfig,
connection: {
filename: './dev.sqlite3' // Change this for your production database settings
}
}
};

Loading