-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created the route to add foods and saving image to firebase and route…
… to get foods added by particular seller
- Loading branch information
1 parent
95096a6
commit 068262e
Showing
16 changed files
with
1,654 additions
and
58 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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
PORT=5000 | ||
MONGO_URI=mongodb://localhost:27017/sem5backend | ||
MONGO_URI=mongodb://localhost:27017/sem5backend | ||
FIREBASE_PRIVATE_KEY= | ||
JWT_SECRET=sdlhfldsfskdfjldjflsdf | ||
JWT_EXPIRES_IN=90d |
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
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,11 @@ | ||
{ | ||
"type": "service_account", | ||
"project_id": "sem5backend", | ||
"private_key_id": "91b69105a4d8c0f6a8b2c28f1dc4b109703999cf", | ||
"client_email": "firebase-adminsdk-t2nvb@sem5backend.iam.gserviceaccount.com", | ||
"client_id": "108687460748205065951", | ||
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
"token_uri": "https://oauth2.googleapis.com/token", | ||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | ||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-t2nvb%40sem5backend.iam.gserviceaccount.com" | ||
} |
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,12 @@ | ||
var admin = require("firebase-admin"); | ||
|
||
var serviceAccount = require("./firebase-key.json"); | ||
|
||
admin.initializeApp({ | ||
credential: admin.credential.cert({...serviceAccount,private_key:process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n')}), | ||
storageBucket: "sem5backend.appspot.com" | ||
}); | ||
|
||
module.exports = { | ||
storage: admin.storage() | ||
}; |
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,16 +1,39 @@ | ||
const Food = require('../model/food'); | ||
|
||
const {storage} = require('../config/firebase') | ||
const log = require('../log'); | ||
const uuidv4 = require('uuid/v4'); | ||
module.exports.addFood = async (req, res) => { | ||
try { | ||
const food = await Food.create({...req.body, belongsTo: req.seller}); | ||
res.status(201).json({ | ||
status: 'Food added successfully', | ||
food, | ||
const bucket = storage.bucket(); | ||
const file = await bucket.upload(req.file.path, { | ||
destination: uuidv4() + req.file.originalname, | ||
metadata: { | ||
contentType: req.file.mimetype, | ||
metadata: { | ||
firebaseStorageDownloadTokens: uuidv4(), | ||
}, | ||
}, | ||
}) | ||
const url = `https://firebasestorage.googleapis.com/v0/b/${bucket.name}/o/${encodeURIComponent(file[0].name)}?alt=media&token=${file[0].metadata.metadata.firebaseStorageDownloadTokens}`; | ||
const food = new Food({ | ||
...req.body, | ||
image: url, | ||
belongsTo: req.seller._id, | ||
}); | ||
await food.save(); | ||
res.status(201).json(food); | ||
} catch (err) { | ||
res.status(400).json({ | ||
status: 'Food not added', | ||
message: err.message, | ||
}); | ||
log.error(err); | ||
res.status(500).json({error: err.message}); | ||
} | ||
}; | ||
|
||
module.exports.getFoods = async (req, res) => { | ||
try { | ||
const foods = await Food.find({belongsTo: req.seller._id}); | ||
res.status(200).json(foods); | ||
} catch (err) { | ||
log.error(err); | ||
res.status(500).json({error: err.message}); | ||
} | ||
} | ||
}; |
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
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
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
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
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
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
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
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
Empty file.
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,26 @@ | ||
const multer = require('multer'); | ||
const storage = multer.diskStorage({ | ||
destination (req, file, cb) { | ||
cb(null, './uploads'); | ||
}, | ||
filename (req, file, cb) { | ||
cb(null, file.originalname); | ||
} | ||
}) | ||
const fileFilter = (req, file, cb) => { | ||
// reject a file | ||
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') { | ||
cb(null, true); | ||
} else { | ||
cb(null, false); | ||
} | ||
}; | ||
|
||
const upload = multer({ | ||
storage: storage, | ||
limits: { | ||
fileSize: 1024 * 1024 * 5 | ||
}, | ||
fileFilter: fileFilter | ||
}) | ||
module.exports = upload; |
Oops, something went wrong.