-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,510 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const Dish = require("../Models/dishModel"); | ||
const uploadImage = require("../Database/uploadImage"); | ||
|
||
exports.get_all_dishes = async (req, res, next) => { | ||
res.header("Content-Type", "application/json"); | ||
try { | ||
const dishes = await Dish.find(); | ||
res.send(JSON.stringify(dishes, null, 4)); | ||
} catch (err) { | ||
res.status(500).send(JSON.stringify({ message: err.message }, null, 4)); | ||
} | ||
}; | ||
|
||
exports.get_dishes_by_ID = async (req, res, next) => { | ||
//to be refactored | ||
const doesDishExist = await Dish.exists({ _id: req.params.id }); | ||
if (doesDishExist) { | ||
try { | ||
const dish = await Dish.findOne({ _id: req.params.id }); | ||
res.json(dish); | ||
} catch (err) { | ||
res.status(500).json({ message: err.message }); | ||
} | ||
} else { | ||
res | ||
.status(404) | ||
.json({ message: `dish with ID of ${req.params.id} not found` }); | ||
} | ||
}; | ||
|
||
exports.add_dish = async (req, res, next) => { | ||
const { | ||
chef, | ||
name, | ||
description, | ||
ingredients, | ||
steps, | ||
healthBenefits, | ||
} = req.body; | ||
const dishImages = req.files; | ||
const dishImagesLinks = []; | ||
|
||
try { | ||
dishImages.forEach((dishImage) => { | ||
dishImagesLinks.push(uploadImage(dishImage)); | ||
}); | ||
console.log(dishImagesLinks); | ||
const newDish = new Dish({ | ||
chef, | ||
name, | ||
chefImage, | ||
dishImages, | ||
description, | ||
ingredients, | ||
steps, | ||
healthBenefits, | ||
}); | ||
await newDish.save(); | ||
res.status(201); | ||
} catch (err) { | ||
res.status(500).json({ message: err.message }); | ||
} | ||
}; | ||
|
||
|
||
exports.delete_dish = (req, res, next) => { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const User = require("../Models/userModel"); | ||
|
||
exports.get_all_users = async (req, res, next) => { | ||
try { | ||
const users = await User.find(); | ||
res.json(users); | ||
} catch (err) { | ||
res.status(500).json({ message: err.message }); | ||
} | ||
}; | ||
|
||
exports.get_user_by_id = async (req, res, next) => { | ||
const doesUserExist = await User.exists({_id: req.params.id}); | ||
if(doesUserExist){ | ||
try { | ||
const user = await User.findOne({_id: req.params.id}) | ||
res.json(user) | ||
} catch (err) { | ||
res.status(500).json({message: err.message}) | ||
} | ||
} else { | ||
res.status(404).json({message: `user with ID of ${req.params.id} not found`}) | ||
} | ||
} | ||
|
||
//patch request | ||
//endpoint : /api/users/{id}/favourites - patch request | ||
exports.update_user_favourites = (req, res, next) => { | ||
User.findOneAndUpdate( | ||
{ _id: req.params.id }, | ||
{ $push: { favourites: req.body.dishId } }, | ||
null, | ||
(err, result) => { | ||
if (err) { | ||
res.status(500).json({ message: err.message }); | ||
} else { | ||
console.log(result); | ||
res.status(200).json({ message: "success" }); | ||
} | ||
} | ||
); | ||
}; | ||
|
||
// /api/users/id/followers - get | ||
exports.get_followers = (req, res, next) => { | ||
|
||
} | ||
|
||
// /api/users/id/following - get | ||
exports.get_following = (req, res, next) => { | ||
|
||
} | ||
|
||
// /api/users/id/follow - post | ||
exports.followUser = (req, res, next) => { | ||
|
||
} | ||
|
||
// /api/users/id/unfollow - post | ||
exports.unfollowUser = (req, res, next) => { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const AWS = require('aws-sdk') | ||
const path = require('path') | ||
const uuid = require("uuid") | ||
const s3 = new AWS.S3({ | ||
accessKeyId: process.env.AKIAZXTF3HPBUHHPX577, | ||
secretAccessKey:process.env.AWS_SECRET_ACCESS_KEY | ||
}) | ||
|
||
|
||
module.exports = (file) => { | ||
if (file.size > 512,000){ | ||
throw "One of your images is greater than 500kb" | ||
} | ||
let location = ""; | ||
const params = { | ||
ACL: "public-read", | ||
Bucket: "ichop-mobileforce", | ||
Key: uuid.v4().toString() + path.extname(file.originalname), | ||
Body: file.buffer, | ||
ContentLength: file.size | ||
} | ||
|
||
s3.upload(params, (err, data) => { | ||
if (err){ | ||
throw err; | ||
} | ||
console.log("file uploaded") | ||
location = data.Location | ||
}) | ||
|
||
return location | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const mongoose = require("mongoose"); | ||
const schema = mongoose.Schema; | ||
|
||
const dishSchema = new schema({ | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
dishImages: [String], | ||
description: String, | ||
likes: { type: Number, min: 0, default: 0 }, | ||
ingredients: { | ||
type: [String], | ||
required: true | ||
}, | ||
steps: { | ||
type: [String], | ||
required: true | ||
}, | ||
healthBenefits: [String], | ||
comments: [{ | ||
chefEmail: String, | ||
chefName: String, | ||
comment: String, | ||
}], | ||
}); | ||
|
||
const Dish = mongoose.model("Dish", dishSchema); | ||
|
||
module.exports = Dish; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const mongoose = require("mongoose"); | ||
const schema = mongoose.Schema; | ||
|
||
const userSchema = new schema({ | ||
name: { | ||
type: String, | ||
trim: true, | ||
}, | ||
email: { | ||
type: String, | ||
unique: true, | ||
trim: true | ||
}, | ||
userImage: String, | ||
country: String, | ||
phoneNumber: String, | ||
dishes: [String], //array of dishes posted by this user | ||
favourites: [String], //array of dish IDs | ||
followers: [{ | ||
name: String, | ||
id: schema.Types.ObjectId | ||
}], | ||
following: [{ | ||
name: String, | ||
id: schema.Types.ObjectId | ||
}], | ||
}); | ||
|
||
const User = mongoose.model("User", userSchema); | ||
|
||
|
||
|
||
module.exports = User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const dish_controller = require('../Controllers/dishController') | ||
const multer = require('multer') | ||
const storage = multer.memoryStorage() | ||
const upload = multer({storage: storage}) | ||
|
||
router.get('/', dish_controller.get_all_dishes) | ||
|
||
router.get('/:id', dish_controller.get_dishes_by_ID) | ||
|
||
router.post('/', upload.single('dishImage'), dish_controller.add_dish) | ||
|
||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const user_controller = require('../Controllers/userController') | ||
|
||
|
||
// get all users | ||
router.get('/', user_controller.get_all_users) | ||
|
||
//get user by id | ||
router.get('/:id', user_controller.get_user_by_id) | ||
|
||
//update user favourite | ||
router.patch('/:id/favourites', user_controller.update_user_favourites) | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,37 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const mongoose = require('mongoose'); | ||
const express = require("express"); | ||
require('dotenv').config() | ||
|
||
//const bodyParser = require("body-parser"); | ||
const mongoose = require("mongoose"); | ||
const cors = require('cors') | ||
|
||
const app = express(); | ||
|
||
app.use(bodyParser.json()); | ||
|
||
|
||
|
||
//init middleware | ||
//1. Body Parser | ||
app.use(express.json()) | ||
app.use(express.urlencoded({extended: false})) | ||
//2. CORS | ||
app.use(cors()) | ||
|
||
//connect to mongoDB Atlas | ||
mongoose | ||
.connect( | ||
"mongodb+srv://GoZaddy:[email protected]/iChop?retryWrites=true&w=majority", | ||
{ useNewUrlParser: true, useUnifiedTopology: true } | ||
) | ||
.then(result => { | ||
console.log("Database connected"); | ||
//TODO: put mongodb URI in .env file | ||
mongoose.connect( | ||
"mongodb+srv://GoZaddy:[email protected]/iChop?retryWrites=true&w=majority", | ||
{ useNewUrlParser: true, useUnifiedTopology: true } | ||
); | ||
const db = mongoose.connection; | ||
db.on("error", console.error.bind(console, "connection error: ")); | ||
db.once("open", () => { | ||
console.log("connected to mongodb"); | ||
}); | ||
|
||
|
||
//api routes | ||
app.use("/api/users", require("./Routes/userRoute")) | ||
app.use("/api/dishes", require("./Routes/dishRoute")) | ||
|
||
//connect to server locally on port 3000 | ||
const port = process.env.PORT || 3000; | ||
app.listen(port); | ||
console.log(`Server listening at ${port}`); | ||
app.listen(port, () => { | ||
console.log(`Server listening at ${port}`) | ||
}) |
Oops, something went wrong.