Skip to content

will return to this #339

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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

.vscode
7 changes: 7 additions & 0 deletions api/recipes/recipes-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function getRecipeById(recipe_id) {
return Promise.resolve(`recipe with id ${recipe_id}`);
}

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

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

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
console.log(err)
res.status(500).json({
customMessage: 'Something went wrong',
message: err.message,
stack: err.stack,
})
})

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

const server = express();

server.use(express.json());

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


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

module.exports = server;

Empty file added data/cook_book.db3
Empty file.
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.js');
const environment = process.env.NODE_ENV || 'development';

module.exports = knex(config[environment]);
50 changes: 50 additions & 0 deletions data/migrations/4321_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()
})
.createTable('ingredients', table => {
table.increments('ingredient_id')
table.string('ingredient_name', 128).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')
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')
}
10 changes: 10 additions & 0 deletions data/seeds/01-cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { clean } = require('knex-cleaner');

exports.seed = function(knex) {
return clean(knex, {
mode: 'truncate',
ignoreTables: ['knex_migrations', 'knex_migrations_lock'],
});
};


41 changes: 41 additions & 0 deletions data/seeds/02-make-recipes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const recipes = [
{ recipe_name: 'tacos' },
{ recipe_name: 'pizza' },
{ recipe_name: 'spaghetti' },
]

const ingredients = [
{ ingredient_name: 'tortilla', ingredient_unit: 'oz' },
{ ingredient_name: 'cheese', ingredient_unit: 'oz' },
{ ingredient_name: 'sauce', ingredient_unit: 'oz' },
{ ingredient_name: 'noodles', ingredient_unit: 'oz' },
{ ingredient_name: 'meat', ingredient_unit: 'oz' },

]

const step_ingredients = [
{ quantity: 1, step_id: 1, ingredient_id: 1 },
{ quantity: 2, step_id: 1, ingredient_id: 2 },
{ quantity: 3, step_id: 1, ingredient_id: 3 },
{ quantity: 4, step_id: 2, ingredient_id: 4 },
{ quantity: 5, step_id: 2, ingredient_id: 5 },
{ quantity: 6, step_id: 2, ingredient_id: 1 },
{ quantity: 7, step_id: 3, ingredient_id: 2 },
{ quantity: 8, step_id: 3, ingredient_id: 3 },
{ quantity: 9, step_id: 3, ingredient_id: 4 },

]

const steps = [
{ step_text: 'put it together', step_number: 1, recipe_id: 1 },
{ step_text: 'put it together', step_number: 1, recipe_id: 2 },
{ step_text: 'put it together', step_number: 1, 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.js');

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

server.listen(port, () => console.log(`\n** server up on port ${port} **\n`));
20 changes: 20 additions & 0 deletions knex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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' },
},
production: {}
}

Loading