Skip to content

WIP #313

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

WIP #313

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


async function getRecipeById (recipe_id) {
const recipeRow = await db('recipes as r')
.where('recipe_id', recipe_id);
return recipeRow;
}

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

const router = express.Router();

const Recipe = require('./recipes-model');


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



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


module.exports = router;
15 changes: 15 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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: 'it seems to be workin'})
})

module.exports = server;
Binary file added data/cookbook.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 configs = require('../knexfile')
const environment = process.env.NODE_ENV || 'development'

module.exports = knex(configs[environment])
60 changes: 60 additions & 0 deletions data/migrations/20220617004241_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', tbl => {
tbl.increments('recipe_id');
tbl.text('recipe_name', 128).notNullable().unique();
})
.createTable('ingredients', tbl => {
tbl.increments('ingredient_id');
tbl.text('ingredient_name', 128).notNullable().unique();
tbl.text('ingredient_unit', 128).notNullable();
})
.createTable('steps', tbl => {
tbl.increments('step_id');
tbl.text('step_text', 256).notNullable();
tbl.integer('step_order')
.notNullable()
.unsigned()
tbl.integer('recipe_id')
.unsigned()
.notNullable()
.references('recipe_id')
.inTable('recipes')
.onUpdate('RESTRICT')
.onDelete('RESTRICT');
})
.createTable('step_ingredients', tbl => {
tbl.increments('step_ingredient_id');
tbl.float('quantity').notNullable();
tbl.integer('step_id')
.unsigned()
.notNullable()
.references('step_id')
.inTable('steps')
.onUpdate('RESTRICT')
.onDelete('RESTRICT');
tbl.integer('ingredient_id')
.unsigned()
.notNullable()
.references('ingredient_id')
.inTable('ingredients')
.onUpdate('RESTRICT')
.onDelete('RESTRICT');
})
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
return 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'],
});
};
44 changes: 44 additions & 0 deletions data/seeds/02-make-recipes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const recipes = [
{ recipe_name: 'Caferio roast' },
{ recipe_name: 'BBQ Chicken' },
{ recipe_name: 'Burger' }
]

const ingredients = [
{ingredient_name: 'pork_roast', ingredient_unit: 'lbs'},
{ingredient_name: 'brown_sugar', ingredient_unit: 'cups'},
{ingredient_name: 'BBQ_sauce', ingredient_unit: 'tbls'},
{ingredient_name: 'chicken', ingredient_unit: 'lbs'},
{ingredient_name: 'beef', ingredient_unit: 'lbs'},
{ingredient_name: 'bun', ingredient_unit: 'lbs'},
]

const steps = [
// Caferio roast
{step_text: 'Place roast in instapot and cook for 50 minutes', step_order: '1', recipe_id: '1'},
{step_text: 'Place brown sugar in instapot and stir until disolved', step_order: '2', recipe_id: '1'},
// BBQ Chicken
{step_text: 'Grill Chicken', step_order: '1', recipe_id: '2'},
{step_text: 'Add BBQ sauce to the chicken', step_order: '2', recipe_id: '2'},
// Burger
{step_text: 'Butcher the cow and get the beef', step_order: '1', recipe_id: '3'},
{step_text: 'Grill the Beef', step_order: '2', recipe_id: '3'},
{step_text: 'Add the beef to the bun', step_order: '3', recipe_id: '3'},
]

const step_ingredients = [
// Caferio roast
{ step_id: '1', ingredient_id: '1', quantity: '5' },
{ step_id: '2', ingredient_id: '2', quantity: '1' },
{ step_id: '3', ingredient_id: '4', quantity: '3' },
{ step_id: '4', ingredient_id: '3', quantity: '5' },
{ step_id: '2', ingredient_id: '5', quantity: '2' },
{ step_id: '7', ingredient_id: '6', quantity: '1' },
]

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;

server.listen(PORT, () => {
console.log(`Listening on port ${PORT}...`);
});
22 changes: 22 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
development: {
client: 'sqlite3',
useNullAsDefault: true, // needed for sqlite
connection: {
filename: './data/cookbook.db3',
},
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/seeds'
},
pool: {
afterCreate: (conn, done) => {
conn.run('PRAGMA foreign_keys = ON', done);
}
}
},
testing: {},
production: {},
};
Loading