Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=1234
NODE_ENV=development
101 changes: 101 additions & 0 deletions api/recipes/jokes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const request = require('supertest')
const db = require('../../data/db-config')
const server = require('../server')
const Recipe = require('../recipes/recipes-model')

const food1 = {recipe_name: 'Fried pickles'}
const food2 = {recipe_name: 'Butter beans'}


beforeAll(async () => {
await db.migrate.rollback()
await db.migrate.latest()
})

beforeEach(async () => {
await db('recipes').truncate()
})

beforeEach(async () => {
await db.seed.run()
})



afterAll(async () => {
await db.destroy()
})


it('correct env', () => {
expect(process.env.NODE_ENV).toBe('testing')
})

describe('create', () => {
it('proof that the insert works', async () => {
let recipes
await Recipe.create(food1)
recipes = await db('recipes')
expect(recipes).toHaveLength(6)
})
it('proof that you can add multiple food and work', async () => {
let recipes
await Recipe.create(food1)
await Recipe.create(food2)
recipes = await db('recipes')
expect(recipes).toHaveLength(7)
})
it('check if it matches the contents', async () => {
const recipe = await Recipe.create(food1)
expect(recipe).toMatchObject({...recipe})
})
})

describe('getAll', () => {
it('it gets an array of objects back', async () => {
const recipes = await Recipe.getAllRecipes()
expect(recipes).toHaveLength(5)
})
})
describe('getById', () => {
it('it gets the first obj back', async () =>{
const recipe = await Recipe.getRecipeId(1)
expect(recipe).toMatchObject({...recipe})
})
})
describe('delete', () => {
it('check the array after the removing the id', async () => {
await Recipe.deleteRecipe(1)
const recipes = await Recipe.getAllRecipes()
expect(recipes).toHaveLength(4)
})
})

describe('[DELETE] / - delete recipe', () => {
it('removes recipes from db', async () => {
const [recipe_id] = await db('recipes').insert(food1)
let recipe = await db('recipes').where({recipe_id}).first()
expect(recipe).toBeTruthy

await request(server).delete('/recipes/'+ recipe_id)
recipe = await db('recipes').where({recipe_id}).first()
expect(recipe).toBeFalsy
})
})

describe('[GET] get all recipes', () => {
it('get all recipes from db', async () => {
await request(server).get('/recipes')
})
})

describe('[GET] by id', () => {
it('gets a recipe', async () => {
const [recipe_id] = await db('recipes').insert(food1)
let recipe = await db('recipes').where({recipe_id}).first()
expect(recipe).toBeTruthy

const singleRecipe = await request(server).get('/recipes/'+recipe_id)
expect(singleRecipe).toBeTruthy
})
})
32 changes: 32 additions & 0 deletions api/recipes/recipes-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const db = require('../../data/db-config')

function getAllRecipes(){
return db('recipes').select('recipe_name')
}

function getRecipeId(recipe_id){
return db('recipes').where('recipe_id', recipe_id)
.first()
}

async function create(information){
const [id] = await db('recipes').insert(information)
return getRecipeId(id)
}

async function updateRecipe(recipe_id, information){
await db('recipes').where('recipe_id', recipe_id).update(information)
return getRecipeId(recipe_id)
}

function deleteRecipe (recipe_id){
return db("recipes").where('recipe_id', recipe_id).del()
}

module.exports = {
getAllRecipes,
getRecipeId,
create,
updateRecipe,
deleteRecipe,
}
58 changes: 58 additions & 0 deletions api/recipes/recipes-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const router = require('express').Router()

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

// router.use('*', (req, res, next) => {
// res.json({api:'up'})
// })
router.get('/', (req, res, next) => {
Recipe.getAllRecipes()
.then(resources => {
res.status(200).json(resources)
})
.catch(next)
})
router.get("/:recipe_id", (req, res, next) => {
Recipe.getRecipeId(req.params.recipe_id)
.then(resource => {
res.status(200).json(resource)
})
.catch(next)
})

router.post('/', async (req,res,next) => {
try {
const newAccount = await Recipe.create(req.body)
res.status(201).json(newAccount)
}
catch (err) {
next(err)
}
})

router.put('/:recipe_id', (req, res, next) => {
const {recipe_id} = req.params
Recipe.updateRecipe(recipe_id, req.body)
.then(result => {
res.status(200).json(result)
})
.catch(next)
})

router.delete('/:recipe_id', (req, res, next) => {
const {recipe_id} = req.params
Recipe.deleteRecipe(recipe_id)
.then(resource => {
res.json({message: 'Resource has been deleted'})
})
.catch(next)
})

router.use((err, req, res, next) => {
res.status(err.status || 500).json({
err: err.message,
stack: err.stack
})
})

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

const server = express()

server.use(express.json())

server.use('/recipes', serverRouter)

module.exports = server
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')

const enviroment = process.env.NODE_ENV || 'development'

module.exports = knex(configurations[enviroment])
22 changes: 22 additions & 0 deletions data/migrations/20230525202003_inital-migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @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', 128)
.notNullable()
.unique()
})
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
await knex.schema
.dropTableIfExists('recipes')
};
Binary file added data/recipes.db3
Binary file not shown.
Binary file added data/recipes.test.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 {clean} = require('knex-cleaner')

exports.seed = function (knex){
return clean(knex, {
mode: 'truncate',
ignoreTables: ['knex_migrations', 'knex_migrations_lock']
})
}
11 changes: 11 additions & 0 deletions data/seeds/02-recipes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const recipes = [
{recipe_name: 'Chicken sandwich'},
{recipe_name: 'Spaghetti'},
{recipe_name: 'Homemade meatballs'},
{recipe_name: 'Ribs'},
{recipe_name: 'Lemon butter chicken'}
]

exports.seed = async function (knex) {
await knex('recipes').insert(recipes)
}
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 || 5000

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

module.exports = {
development:{
...sharedConfig,
connection: { filename: './data/recipes.db3'},
},
testing:{
...sharedConfig,
connection: {filename: './data/recipes.test.db3'},
}
}
12 changes: 12 additions & 0 deletions node_modules/.bin/acorn

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions node_modules/.bin/acorn.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/.bin/acorn.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions node_modules/.bin/browserslist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions node_modules/.bin/browserslist.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading