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

Larry weller #322

Open
wants to merge 9 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
7 changes: 5 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"env": {
"node": true,
"browser": true,
"commonjs": true,
"es2021": true,
"node": true
"es2021": true
},
"extends": "eslint:recommended",
"overrides": [
],
"parserOptions": {
"ecmaVersion": "latest"
},
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,6 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*


.vscode
63 changes: 63 additions & 0 deletions api/recipes/recipes-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const db = require('../../data/db-config')

async function getRecipeById(recipes_id){
const recipeRows = await db('recipes as r')
.leftJoin('steps as s', 'r.recipes_id', 's.recipes_id')
.leftJoin('steps_ingredients as si', 'si.steps_id', 's.steps_id')
.leftJoin('ingredients as ing', 'ing.ing_id', 'si.ing_id')
.select(
'r.recipes_id',
'r.recipes_name',
's.steps_id',
's.steps_order',
's.instructions',
'si.ing_id',
'ing.ing_name',
'ing.ing_unit'
)
.orderBy('s.steps_order')
.where('r.recipes_id', recipes_id)


const result = {
recipes_id: recipeRows[0].recipes_id,
recipes_name: recipeRows[0].recipes_name,
steps: recipeRows.reduce((acc, row) => {
if(!row.ingredient_id) {
return acc.concat({
steps_id: row.steps_id,
steps_order: row.steps_order,
instructions: row.instructions
})
}
if(row.ingredient_id && !acc.find(step => step.id === row.step_id)){
return acc.count({
steps_id: row.steps_id,
steps_order: row.steps_order,
instructions: row.instructions,
ingredients: [
{
ing_id: row.ing_id,
ing_name: row.ing_name,
ing_unit: row.ing_unit
}
]
})
}
const currentStep = acc.find(step => step.step_id === row.step_id)
currentStep.ingredients.push({
ing_id: row.ing_id,
ing_name: row.ing_name,
ing_unit: row.ing_unit
})
return acc
}, [])
}


return result
}

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

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




router.use((err, req, res, next) => { // eslint-disable-line
res.status(500).json({
customMessage: "Something Wrong",
message: err.message
})
})

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

const server = express()

server.use(express.json())


server.use('/api/recipes', recipiesRouter )



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

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 enviroment = process.env.NODE_ENV || 'development'

module.exports = knex(configurations[enviroment])
65 changes: 65 additions & 0 deletions data/migrations/20230611182245_initial-migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @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_text', 200).notNullable().unique()
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_ingredients_id')
table.float('quantity').notNullable()
table.integer('step_id').notNullable()
.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')



};
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'],
})
}
51 changes: 51 additions & 0 deletions data/seeds/02-make-recipes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const recipes = [
{ recipe_name: 'Broccolie Pesto Pasta' },
{ recipe_name: 'Lemon Chicken' },
{ recipe_name: 'Salmon en Papillote' },
]

const ingredients = [
{ ingredient_name: 'Brocoli', ingredient_unit: 'lbs'},
{ ingredient_name: 'Pesto', ingredient_unit: 'lbs'},
{ ingredient_name: 'Pasta', ingredient_unit: 'lbs'},
{ ingredient_name: 'Lemon', ingredient_unit: 'slices'},
{ ingredient_name: 'Chicken', ingredient_unit: 'kilos'},
{ ingredient_name: 'Salmon', ingredient_unit: 'grams'},

]

const step_ingredients = [
// Broccoli Pesto Pasta
{ step_id: 2, ingredient_id: 1, quantity: 1 },
{ step_id: 3, ingredient_id: 2, quantity: 1.5 },
{ step_id: 3, ingredient_id: 3, quantity: 2 },
// Lemon Chicken
{ step_id: 5, ingredient_id: 4, quantity: 1 },
{ step_id: 5, ingredient_id: 5, quantity: .4 },
// Salmon en Papillote
{ step_id: 7, ingredient_id: 6, quantity: 1 },
]

const steps = [
// Broccoli Pesto Pasta
{ step_text: 'Heat Pan', step_number: 1, recipe_id: 1 },
{ step_text: 'Add Broccoli', step_number: 2, recipe_id: 1 },
{ step_text: 'Add Pesto mixed with pasta', step_number: 3, recipe_id: 1 },
//Lemon Chicken
{ step_text: 'Heat Oven', step_number: 1, recipe_id: 2 },
{ step_text: 'Put chicken and lemon in oven', step_number: 2, recipe_id: 2 },
{ step_text: 'Put in oven at 500 Degrees', step_number: 3, recipe_id: 2 },
//Salmon en Papillote
{ step_text: 'Fish a salmon in the Bidasoa river', step_number: 1, recipe_id: 3 },
{ step_text: 'Cook Salmon', step_number: 2, recipe_id: 3 },

]


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)

}
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('dotenv').config()

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

const port = process.env.PORT

server.listen(port, () => console.log(`server 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',
useNullAsDefault: true,
migrations: { directory: './data/migrations' },
pool: { afterCreate: (conn, done) => conn.run('PRAGMA foreign_keys = ON', done) },
}

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