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

Sara dawson #318

Open
wants to merge 10 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
6 changes: 4 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true,
"node": true
"es2021": true
},
"extends": "eslint:recommended",
"overrides": [
],
"parserOptions": {
"ecmaVersion": "latest"
},
Expand Down
48 changes: 48 additions & 0 deletions api/recipes/recipes-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const db = require('../../data/db-config');

async function getRecipeById(recipe_id) {
const recipesRows = 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_name',
'si.quantity'
)
.orderBy('step_number')
.where('r.recipe_id', recipe_id)

const recipes = {
recipe_name: recipesRows[0].recipe_name,
steps: recipesRows.reduce((acc, row) => {
if(!row.ingredient_name){
return acc.concat({
step_number: row.step_number,
step_instructions: row.step_instructions
})
}else{
return acc.concat({
step_number: row.step_number,
step_instructions: row.step_instructions,
ingredients: [
{
ingredient_name: row.ingredient_name,
quantity: row.quantity
}
]
})
}
}, [])
}

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 express = require('express');
const Recipes = require('./recipes-model');
const router = express.Router();

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

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

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({
message: "Router is working"
});
});

module.exports = server;
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 config = require('../knexfile');

module.exports = knex(config.development);
60 changes: 60 additions & 0 deletions data/migrations/20221028202340_initial-migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function(knex) {
await knex.schema
.createTable('recipes', table => {
table.increments('recipe_id')
table.string('recipe_name', 200).notNullable().unique()
})
.createTable('ingredients', table => {
table.increments('ingredient_id')
table.string('ingredient_name', 200).notNullable().unique()
table.string('ingredient_unit', 50)
})
.createTable('steps', table => {
table.increments('step_id')
table.string('step_instructions', 200).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')
})

};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
await knex.schema
.dropTableIfExists('step_ingredients')
.dropTableIfExists('steps')
.dropTableIfExists('ingredients')
.dropTableIfExists('recipes')

};
Binary file added data/recipes.db3
Binary file not shown.
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 cleaner = require('knex-cleaner');

exports.seed = function(knex) {
return cleaner.clean(knex , {
mode: 'truncate',
ignoreTables: ['knex_migrations', 'knex_migrations_lock']
})
}
56 changes: 56 additions & 0 deletions data/seeds/02-make-recipes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const recipes = [
{ recipe_name: 'Broccoli Rice Casserole' },
{ recipe_name: 'Spaghetti' },
{ recipe_name: 'Salmon' }
]

const ingredients = [
{ ingredient_name: 'Broccoli', ingredient_unit: 'lbs' },
{ ingredient_name: 'Rice', ingredient_unit: 'lbs' },
{ ingredient_name: 'Cream Of Chicken Soup', ingredient_unit: 'can' },
{ ingredient_name: 'Noodles', ingredient_unit: 'box' },
{ ingredient_name: 'Marinara', ingredient_unit: 'jar' },
{ ingredient_name: 'Salmon', ingredient_unit: 'lbs' },
{ ingredient_name: 'Lemon', ingredient_unit: 'slices' },
{ ingredient_name: 'Olive Oil', ingredient_unit: 'Tbs' }
]

const steps = [
//broccoli rice casserole
{ step_instructions: 'Preheat oven to 450 degrees', step_number: 1, recipe_id: 1 },
{ step_instructions: 'Cook rice', step_number: 2, recipe_id: 1 },
{ step_instructions: 'Add rice to pan', step_number: 3, recipe_id: 1 },
{ step_instructions: 'Add broccoli', step_number: 4, recipe_id: 1 },
{ step_instructions: 'Add soup', step_number: 5, recipe_id: 1 },
{ step_instructions: 'Cook for 30 mins', step_number: 6, recipe_id: 1 },
//spaghetti
{ step_instructions: 'Cook noodles', step_number: 1, recipe_id: 2 },
{ step_instructions: 'Warm up marinara sauce', step_number: 2, recipe_id: 2 },
{ step_instructions: 'Add marinara to noodles', step_number: 3, recipe_id: 2 },
//salmon
{ step_instructions: 'Skin salmon', step_number: 1, recipe_id: 3 },
{ step_instructions: 'Add olive oil to pan', step_number: 2, recipe_id: 3 },
{ step_instructions: 'Cook salmon in pan for 15 mins', step_number: 3, recipe_id: 3 },
{ step_instructions: 'Plate salmon and add lemon', step_number: 4, recipe_id: 3 }
]

const step_ingredients = [
//broccoli rice casserole
{ step_id: 2, ingredient_id: 2, quantity: 2 },
{ step_id: 4, ingredient_id: 1, quantity: 1 },
{ step_id: 5, ingredient_id: 2, quantity: 2 },
//spaghetti
{ step_id: 7, ingredient_id: 4, quantity: 1 },
{ step_id: 8, ingredient_id: 5, quantity: 1 },
//salmon
{ step_id: 10, ingredient_id: 6, quantity: 2 },
{ step_id: 11, ingredient_id: 8, quantity: 2 },
{ step_id: 13, ingredient_id: 7, quantity: 2 }
]

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)
}
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');
require('dotenv').config();

const PORT = process.env.PORT || 9000; //eslint-disable-line

server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
});
27 changes: 27 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Update with your config settings.

/**
* @type { Object.<string, import("knex").Knex.Config> }
*/
module.exports = {

development: {
client: 'sqlite3',
connection: {
filename: './data/recipes.db3'
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/seeds'
},
pool: {
afterCreate: (conn, done) => {
conn.run('PRAGMA foreign_keys = ON', done);
},
},
},

};
Loading