Skip to content

completed mvp #331

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 1 commit 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
57 changes: 57 additions & 0 deletions api/recipes/recipes-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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_text',
'i.ingredient_id',
'i.ingredient_name',
'si.quantity'
)
.where('recipe_id', recipe_id)

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

return acc.concat({
step_id: row.step_id,
step_number: row.step_number,
step_text: row.step_text,
ingredients: [{
ingredient_id: row.ingredient_id,
ingredient_name: row.ingredient_name,
quantity: row.quantity,
}]
})
}

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

module.exports = { getRecipeById }
24 changes: 24 additions & 0 deletions api/recipes/recipes-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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((err, req, res, next) => { //eslint-disble-line
res.status(500).json({
customMessage: 'something went wrong',
message: err.message,
stack: err.stack,
})
})

module.exports = router
13 changes: 13 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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
Empty file added data/cook_book.db3
Empty file.
7 changes: 7 additions & 0 deletions data/db-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const knex = require('knex')

const configurations = require('../knexfile.js')

const environment = process.env.NODE_ENV

module.exports = knex(configurations[environment])
51 changes: 51 additions & 0 deletions data/migrations/20231016203720_initial-migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @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()
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')
})
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
await knex.schema
.dropTableIfExists('steps_ingredients')
.dropTableIfExists('steps')
.dropTableIfExists('ingredients')
.dropTableIfExists('recipes')
};
Empty file added data/seeds/01-cleanup.js
Empty file.
53 changes: 53 additions & 0 deletions data/seeds/02-make-recipes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const recipes = [
{ recipe_name: 'Kylling Pasta' },
{ recipe_name: 'Choko Smaakager'},
{ recipe_name: 'Roede Groede med Floede'},
]

const ingredients = [
{ ingredient_name: 'Kylling', ingredient_unit: 'lbs'},
{ ingredient_name: 'Pasta', ingredient_unit: 'lbs'},
{ ingredient_name: 'Chokolade', ingredient_unit: 'cups'},
{ ingredient_name: 'Smaakager', ingredient_unit: 'cups'},
{ ingredient_name: 'Frugt', ingredient_unit: 'lbs'},
{ ingredient_name: 'Floede', ingredient_unit: 'cups'},

]

const step_ingredients = [
//kylling
{ step_id: 1, ingredient_id: 1, quantity: 1},
{ step_id: 2, ingredient_id: 2, quantity: 2},
//smaakager
{ step_id: 1, ingredient_id: 3, quantity: 1},
{ step_id: 2, ingredient_id: 4, quantity: 3},
//tonguetwister
{ step_id: 1, ingredient_id: 5, quantity: 1},
{ step_id: 2, ingredient_id: 5, quantity: 1},
{ step_id: 3, ingredient_id: 6, quantity: 1},
]

const steps = [
//kylling
{ step_text: 'Grill the chicken', step_number: 1, recipe_id: 1 },
{ step_text: 'Boil the pasta', step_number: 2, recipe_id: 1 },
{ step_text: 'Create the sauce', step_number: 3, recipe_id: 1 },
{ step_text: 'Combine the components', step_number: 4, recipe_id: 1 },
//smaakager
{ step_text: 'Chop the chocolate', step_number: 1, recipe_id: 2 },
{ step_text: 'Mix the dough', step_number: 2, recipe_id: 2 },
{ step_text: 'Bake the cookies', step_number: 3, recipe_id: 2 },
{ step_text: 'Impress the cookies', step_number: 4, recipe_id: 2 },
//tonguetwister
{ step_text: 'Mascerate the berries', step_number: 1, recipe_id: 3 },
{ step_text: 'Mash the berries', step_number: 2, recipe_id: 3 },
{ step_text: 'Add the cream', step_number: 3, 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_ingredient').insert(step_ingredient)
}
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const server = require("./api/server.js");

const port = process.env.PORT || 5000;

server.listen(port, () => console.log(`\nAPI running on port ${port}\n`))
18 changes: 18 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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/cook_book.test.db3'},
},
}
Loading