Skip to content

Frankie jose branch #332

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

Open
wants to merge 4 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
60 changes: 60 additions & 0 deletions api/recipes/recipes-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const db = require('../../data/db-config');

async function getRecipeById(recipe_id) {
const recipeRows = await db('recipes as r')
.leftJoin('steps as s', 'r.recipe_id', 's.recipe_id')
.leftJoin('step_ingredients as si', 'si.step_id', 's.step_id')
.leftJoin('ingredients as i', 'i.ingredient_id', 'si.ingredient_id')
.select(
'r.recipe_id',
'r.recipe_name',
's.step_id',
's.step_number',
's.step_instructions',
'i.ingredient_id',
'i.ingredient_name',
'si.quantity'
)
.orderBy('s.step_number')
.where('r.recipe_id', recipe_id)

const recipes = {
recipe_id: recipeRows[0].recipe_id,
recipe_name: recipeRows[0].recipe_name,
steps: recipeRows.reduce((acc, rows) => {
if (!rows.ingredient_id) {
return acc.concat({
step_id: rows.step_id,
step_number: rows.step_number,
step_instructions: rows.step_instructions,
})
}
if (rows.ingredient_id && !acc.find(step => step.step_id === rows.step_id)) {
return acc.concat({
step_id: rows.step_id,
step_number: rows.step_number,
step_instructions: rows.step_instructions,
ingredients: [
{
ingredient_id: rows.ingredient_id,
ingredient_name: rows.ingredient_name,
quantity: rows.quantity
}
]
})
}

const currentStep = acc.find(step => step.step_id === rows.step_id)
currentStep.ingredients.push({
ingredient_id: rows.ingredient_id,
ingredient_name: rows.ingredient_name,
quantity: rows.quantity
})
return acc;
}, [])
}

return recipes
}

module.exports = { getRecipeById };
20 changes: 20 additions & 0 deletions api/recipes/recipes-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const router = require('express').Router();
const Recipe = require('./recipes-model');

router.get('/:recipe_id', (req, res, next) => {
Recipe.getRecipeById(req.params.recipe_id)
.then(resource => {
res.status(200).json(resource)
})
.catch(next)
});

router.use((error, req, res, next) => { //eslint-disable-line
res.status(500).json({
customMessage: 'Something went wrong',
message: error.message,
stack: error.stack
})
})

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

const server = express();

server.use(express.json());

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

server.use('*', (req, res) => {
res.json({ api: 'up'})
});

module.exports = server;
Binary file added data/cook_book.db3
Binary file not shown.
5 changes: 5 additions & 0 deletions data/db-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const knex = require('knex');
const configurations = require('../knexfile');
const environment = process.env.NODE_ENV || 'development';

module.exports = knex(configurations[environment]);
50 changes: 50 additions & 0 deletions data/migrations/20231123065543_initial-migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
exports.up = async function(knex) {
await knex.schema
.createTable('recipes', table => {
table.increments('recipe_id')
table.string('recipe_name', 128).notNullable().unique()
})
.createTable('ingredients', table => {
table.increments('ingredient_id');
table.string('ingredient_name', 128).notNullable().unique()
})
.createTable('steps', table => {
table.increments('step_id');
table.string('step_instructions', 128).notNullable()
table.integer('step_number').notNullable()
table.integer('recipe_id')
.unsigned()
.notNullable()
.references('recipe_id')
.inTable('recipes')
.onDelete('RESTRICT')
.onUpdate('RESTRICT')
})
.createTable('step_ingredients', table => {
table.increments('step_ingredient_id')
table.float('quantity').notNullable()
table.integer('step_id')
.unsigned()
.notNullable()
.references('step_id')
.inTable('steps')
.onDelete('RESTRICT')
.onUpdate('RESTRICT')
table.integer('ingredient_id')
.unsigned()
.notNullable()
.references('ingredient_id')
.inTable('ingredients')
.onDelete('RESTRICT')
.onUpdate('RESTRICT')
})
};


exports.down = async function(knex) {
await knex.schema
.dropTableIfExists('step-ingredients')
.dropTableIfExists('steps')
.dropTableIfExists('ingredients')
.dropTableIfExists('recipes')
};
8 changes: 8 additions & 0 deletions data/seeds/01-cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { clean } = require('knex-cleaner');

exports.seed = function(knex) {
return clean(knex, {
mode: 'truncate',
ignoreTables: ['knex_migrations', 'knex_migrations_lock'],
});
};
37 changes: 37 additions & 0 deletions data/seeds/02-make-recipes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const recipes = [
{ recipe_name: 'Chocolate Cake' },
{ recipe_name: 'Spaghetti Bolognese' },
{ recipe_name: 'Chicken Alfredo' },
]

const ingredients = [
{ ingredient_name: 'Flour' },
{ ingredient_name: 'Sugar' },
{ ingredient_name: 'Eggs' },
{ ingredient_name: 'Chocolate' },
{ ingredient_name: 'Tomato Sauce' },
{ ingredient_name: 'Ground Beef' },
{ ingredient_name: 'Chicken' },
{ ingredient_name: 'Cream' },
{ ingredient_name: 'Pasta' },
]

const steps = [
{ step_instructions: 'Preheat oven to 350°F', step_number: 1, recipe_id: 1 },
{ step_instructions: 'Mix flour, sugar, and eggs', step_number: 2, recipe_id: 1 },
{ step_instructions: 'Melt chocolate and add to the mixture', step_number: 3, recipe_id: 1 },
]

const step_ingredients = [
{ step_id: 2, ingredient_id: 1, quantity: 1.4 },
{ step_id: 2, ingredient_id: 2, quantity: 0.5},
{ step_id: 3, ingredient_id: 4, quantity: 2.4},

]

exports.seed = async function(knex) {
await knex('recipes').insert(recipes)
await knex('ingredients').insert(ingredients)
await knex('steps').insert(steps)
await knex('step_ingredients').insert(step_ingredients)
};
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require('dotenv').config();

const server = require("./api/server.js")

const PORT = process.env.PORT || 9000

server.listen(PORT, () => {
console.log(`\n== API running on port ${PORT} ==\n`)
})
19 changes: 19 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const sharedConfig = {
client: 'sqlite3',
migrations: { directory: './data/migrations'},
seeds: { directory: './data/seeds' },
useNullAsDefault: true,
pool: { afterCreate: (conn, done) => conn.run('PRAGMA foreign_keys = ON', done) },
}

module.exports = {
development: {
...sharedConfig,
connection: { filename: './data/cook_book.db3' },
},
testing: {
...sharedConfig,
connection: { filename: './data/cook_book.test.db3' },
},
production: {}
}
Loading