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

Matt Ray submitting 'custom-api' homework #1

Open
wants to merge 2 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
node_modules/
.env
85 changes: 0 additions & 85 deletions README.md

This file was deleted.

11 changes: 11 additions & 0 deletions controllers/addFoodRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express');
const _ = require('lodash');
const router = express.Router();

let db = require('../models');





module.exports = router;
128 changes: 128 additions & 0 deletions controllers/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
const express = require('express');
const _ = require('lodash');
const {ObjectID} = require('mongodb');
const router = express.Router();


let db = require('../models');


router.get('/', (req, res) => {
db.Food.find({}).then((data) => {
if(!data) {
res.status(500).send();
}
res.send(data);
}).catch((err) => {
res.status(400).send();
});
});


router.post('/', (req, res) => {
let body = _.pick(req.body, Object.keys(db.Food.schema.paths));
let food = new db.Food(body);

food.save().then((data) => {
res.send(data);
}).catch((err) => {
res.status(400).send();
});
});


// Find Food by ObjectID
router.get('/:id', (req, res) => {
let id = req.params.id;
if (!ObjectID.isValid(id)) {
return res.status(404).send();
}

db.Food.findById(id).then((foundItem) => {
if (!foundItem) {
return res.status(404).send(`Error finding item with ObjectID: ${id}`);
}
res.send(foundItem);
}).catch((err) => {
res.status(400).send();
});
});


// Update a Food Entry

router.patch('/:id', (req, res) => {
let id = req.params.id;
if (!ObjectID.isValid(id)) {
return res.status(404).send();
}

db.Food.findByIdAndUpdate(id, {
$set: req.body,
new: true
}).then((foundItem) => {
if (!foundItem) {
return res.status(404).send(`Error updating item with ObjectID: ${id}`);
}
res.send(foundItem);
}).catch((err) => {
res.send(400).send();
});
});


// Delete a Food Entry

router.delete('/:id', (req, res) => {
let id = req.params.id;
if (!ObjectID.isValid(id)) {
return res.status(404).send();
}

db.Food.findByIdAndRemove(id).then((removeItem) => {
if (!removeItem) {
return res.status(404).send(`Error removing item with ObejctID: ${id}`);
}

res.send({
'Removed Item': `${removeItem.name}`,
'Object ID': `${id}`,
});
}).catch((err) => {
res.status(400).send();
});
});











// router.get('/addFood', (req, res) => {
// res.render('index');
// });


// router.post('/addFood', (req, res) => {
// let bodyPaths = _.pick(req.body, Object.keys(db.Food.schema.paths));
// let food = new db.Food(bodyPaths);
// let infoPaths = _.pick(req.body, Object.keys(db.NutritionInfo.schema.paths));

// food.NutritionInfo.push(infoPaths);
// food.save().then((food) => {
// res.send();
// }).catch((e) => {
// res.status(400).send("Unable to save to database");
// });
// });





module.exports = router;
39 changes: 39 additions & 0 deletions models/food.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let nutritionInfoSchema = new Schema({
servingSizeAmount: Number,
servingSizeUnit: String,
servingSizeDescription: String,
calories: Number,
totalFat: Number,
sodium: Number,
sugar: Number,
protein: Number,
fiber: Number
});

let foodSchema = new Schema({
name: {
type: String,
required: true
},
imageUrl: {
type: String,
default: ""
},
tags: [{
type: String,
default: ""
}],
NutritionInfo: [nutritionInfoSchema]
});

let NutritionInfo = mongoose.model('NutritionInfo', nutritionInfoSchema);
let Food = mongoose.model('Food', foodSchema);


module.exports = {
NutritionInfo,
Food,
};
28 changes: 17 additions & 11 deletions models/index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
const mongoose = require('mongoose');
require('dotenv').config();
// TODO: include all model files here (and export models together below)
const testModels = require('./test');
mongoose.Promise = global.Promise;

// connect to Mongo DB
mongoose.connection.openUri(process.env.MONGODB_URI || process.env.DB_CONN, {}, function(err, conn) {
if (err) {
console.log('Error connecting to Mongo DB.', err);
} else {
console.log('Mongoose successfully connected to Mongo DB.');
}
const food = require('./food');
const mealEntry = require('./meal-entry');
const mealTracker = require('./meal-tracker');


mongoose.connection.openUri(process.env.MONGODB_URI || process.env.DB_MONGO_URI_LOCAL, {
useMongoClient: true
});



// export
module.exports = {
// TODO: add references to all models here
Test: testModels.Test
NutritionInfo: food.NutritionInfo,
Food: food.Food,
FoodServing: mealEntry.FoodServing,
MealEntry: mealEntry.MealEntry,
MealDay: mealTracker.MealDay,
MealTracker: mealTracker.MealTracker,
};
23 changes: 23 additions & 0 deletions models/meal-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const foodServingSchema = new Schema({
food: {
type: Schema.Types.ObjectId,
ref: 'Food'
},
numServings: Number
});

const mealEntrySchema = new Schema({
time: Date,
foodItems: [foodServingSchema]
});

const FoodServing = mongoose.model('FoodServing', foodServingSchema);
const MealEntry = mongoose.model('MealEntry', mealEntrySchema);

module.exports = {
FoodServing,
MealEntry,
};
24 changes: 24 additions & 0 deletions models/meal-tracker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const mealDaySchema = new Schema({
date: Date,
isCheatDay: Boolean,
meals: [{
type: Schema.Types.ObjectId,
ref: 'MealEntry'
}]
});

const trackerSchema = new Schema({
name: String,
mealDays: [mealDaySchema]
});

const MealDay = mongoose.model('MealDay', mealDaySchema);
const MealTracker = mongoose.model('MealTracker', trackerSchema);

module.exports = {
MealDay: MealDay,
MealTracker: MealTracker,
};
22 changes: 0 additions & 22 deletions models/test.js

This file was deleted.

Loading