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

Hwee Meng almost completed mvp #138

Open
wants to merge 4 commits into
base: master
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
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.
1 change: 1 addition & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"recipes":[{"id":2,"title":"addnew","ingredients":"addnew","instructions":"add new"}]}
141 changes: 141 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
const jsonfile = require('jsonfile');
const express = require('express');
const app = express();
app.use(express.static(__dirname+'/public/'));
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));

const reactEngine = require('express-react-views').createEngine();
app.engine('jsx', reactEngine);
app.set('views', __dirname + '/views');
app.set('view engine', 'jsx');

var file = 'data.json';
var ingredients = 'ingredient.json';

const methodOverride = require('method-override')
app.use(methodOverride('_method'));

app.get('/home', (req,res) => {
res.render('home')
})

app.get('/recipes/create', (req,res) => {
res.render('create')
})

app.get('/recipes/:id/delete', (req,res) => {
res.render('delete')
})

app.get('/recipes/:id/edit', (req,res) => {
jsonfile.readFile(file, (err,obj) => {
console.log("error of readfile is: =============");
console.log(err);
console.log("obj of readfile is: =============");
console.log(obj);
var recipeHolder= {};
recipeHolder = obj.recipes[parseInt(req.params.id)-1];
res.render('edit', recipeHolder);
});
});

app.delete('/recipes/:id', (req,res) => {
jsonfile.readFile(file, (err,obj) => {
const parsedNum = parseInt(req.params.id)-1;
console.log("***************************parseint");
console.log(req.params);
obj.recipes.splice(parsedNum, 1);
console.log("==============delete look here")
console.log("************************")
jsonfile.writeFile(file, obj, (err)=>{
console.log("error of writefile is: ==========")
console.log(err);
console.log(obj);
});
res.redirect('/home');
});
});

app.put('/recipes/:id', (req,res) => {
jsonfile.readFile(file, (err,obj) => {
console.log("error of readfile is: =============");
console.log(err);
console.log("obj of readfile is: =============");
var parsedNum = parseInt(req.params.id)-1
var recipeHolder = {};
recipeHolder=obj.recipes[parsedNum];
console.log("================ putreq before")
console.log(recipeHolder)
recipeHolder.title = req.body.title;
recipeHolder.ingredients = req.body.ingredients;
recipeHolder.instructions = req.body.instructions;
console.log("================ putreq after")
console.log(recipeHolder)
jsonfile.writeFile(file, obj, (err)=>{
console.log("error of writefile is: ==========")
console.log(err);
console.log(obj);
});
res.render('single', recipeHolder);
});
});

app.get('/recipes/:id', (req,res) => {
jsonfile.readFile(file, (err,obj) => {
console.log("error of readfile is: =============");
console.log(err);
console.log("obj of readfile is: =============");
console.log(obj);
var recipeHolder= {};
recipeHolder = obj.recipes[parseInt(req.params.id)-1];
res.render('single', recipeHolder);
});
});

app.post('/recipes', (req,res)=>{
jsonfile.readFile(file, (err,obj) => {
console.log("error of readfile is: =============");
console.log(err);
console.log("obj of readfile is: =============");
console.log(obj);
const addNewRecipe = {
id: (parseInt(obj.recipes.length) +1),
title: req.body.title,
ingredients: req.body.ingredients,
instructions: req.body.instructions,
};
obj.recipes.push(addNewRecipe);
jsonfile.writeFile(file, obj, (err)=>{
console.log("error of writefile is: ==========")
console.log(err);
console.log(obj);
});
});
res.redirect('/home');
});






















app.listen(3000, () => console.log('~~~ Tuning in to the waves of port 3000 ~~~'));
1 change: 1 addition & 0 deletions node_modules/.bin/browserslist

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

1 change: 1 addition & 0 deletions node_modules/.bin/css-beautify

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

1 change: 1 addition & 0 deletions node_modules/.bin/editorconfig

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

1 change: 1 addition & 0 deletions node_modules/.bin/html-beautify

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

1 change: 1 addition & 0 deletions node_modules/.bin/js-beautify

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

1 change: 1 addition & 0 deletions node_modules/.bin/jsesc

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

1 change: 1 addition & 0 deletions node_modules/.bin/json5

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

1 change: 1 addition & 0 deletions node_modules/.bin/loose-envify

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

1 change: 1 addition & 0 deletions node_modules/.bin/mime

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

1 change: 1 addition & 0 deletions node_modules/.bin/mkdirp

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

1 change: 1 addition & 0 deletions node_modules/.bin/nopt

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

1 change: 1 addition & 0 deletions node_modules/.bin/parser

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

1 change: 1 addition & 0 deletions node_modules/.bin/regjsparser

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

1 change: 1 addition & 0 deletions node_modules/.bin/semver

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

Loading