-
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.
- Loading branch information
Tan Benjamin
committed
Jun 8, 2019
0 parents
commit 9b25c3b
Showing
11 changed files
with
3,843 additions
and
0 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,5 @@ | ||
{ | ||
"projects": { | ||
"default": "candy-243011" | ||
} | ||
} |
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,65 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
firebase-debug.log* | ||
|
||
# Firebase cache | ||
.firebase/ | ||
|
||
# Firebase config | ||
|
||
# Uncomment this if you'd like others to create their own Firebase project. | ||
# For a team working on the same Firebase project(s), it is recommended to leave | ||
# it commented so all members can deploy to the same project(s) in .firebaserc. | ||
# .firebaserc | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env |
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,20 @@ | ||
{ | ||
"firestore": { | ||
"rules": "firestore.rules", | ||
"indexes": "firestore.indexes.json" | ||
}, | ||
"hosting": { | ||
"public": "public", | ||
"ignore": [ | ||
"firebase.json", | ||
"**/.*", | ||
"**/node_modules/**" | ||
], | ||
"rewrites": [ | ||
{ | ||
"source": "/api/v1/**", | ||
"function": "webApi" | ||
} | ||
] | ||
} | ||
} |
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,4 @@ | ||
{ | ||
"indexes": [], | ||
"fieldOverrides": [] | ||
} |
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,7 @@ | ||
service cloud.firestore { | ||
match /databases/{database}/documents { | ||
match /{document=**} { | ||
allow read, write: if false; | ||
} | ||
} | ||
} |
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 @@ | ||
node_modules/ |
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,117 @@ | ||
const functions = require('firebase-functions'); | ||
|
||
const admin = require('firebase-admin'); | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const cors = require('cors'); | ||
|
||
admin.initializeApp(functions.config().firebase); | ||
|
||
const db = admin.firestore(); | ||
|
||
const app = express(); | ||
const main = express(); | ||
|
||
const postingsCollection = 'postings'; | ||
|
||
app.use(cors({ origin: true })); | ||
main.use('/api/v1', app); | ||
main.use(bodyParser.json()); | ||
main.use(bodyParser.urlencoded({ extended: false })); | ||
|
||
// webApi is your functions name, and you will pass main as | ||
// a parameter | ||
exports.webApi = functions.https.onRequest(main); | ||
|
||
|
||
|
||
// CRUD Postings | ||
// Add new posting | ||
app.post('/postings', (req, res) => { | ||
const validationError = validatePostingInput(req.body); | ||
if (!validationError) { | ||
db.collection('postings').add(req.body) | ||
.then(ref => { | ||
res.status(200).send({ id: ref.id }) | ||
}) | ||
.catch(err => { | ||
res.status(400).send({ | ||
message: "An error has occured when adding a posting " + err | ||
}) | ||
}); | ||
} else { | ||
res.status(400).send({ | ||
message: "Input is invalid. " + validationError | ||
}); | ||
} | ||
}) | ||
// Get all postings | ||
app.get('/postings', (req, res) => { | ||
db.collection('postings').get() | ||
.then(ref => { | ||
if (ref.empty) { | ||
res.status(400).send({ | ||
message: "No matching posting documents." | ||
}); | ||
} | ||
var postings = []; | ||
ref.forEach(doc => { | ||
var data = doc.data(); | ||
data.id = doc.id; | ||
postings.push(data); | ||
}); | ||
res.status(200).send(postings); | ||
}) | ||
.catch(err => { | ||
res.status(400).send({ | ||
message: "An error has occured. " + err | ||
}); | ||
}); | ||
}) | ||
// Get a posting | ||
app.get('/postings/:postingId', (req, res) => { | ||
db.collection('postings').doc(req.params.postingId).get() | ||
.then(ref => { | ||
var data = ref.data(); | ||
data.id = ref.id; | ||
res.status(200).send(data) | ||
}).catch(err => { | ||
res.status(400).send({ | ||
message: "An error has occured. " + err | ||
}); | ||
}); | ||
}) | ||
// Update a posting | ||
app.put('/postings/:postingId', (req, res) => { | ||
const validationError = validatePostingInput(req.body); | ||
if (!validationError) { | ||
db.collection('postings').doc(req.params.postingId).update(req.body) | ||
.then(ref => { | ||
res.status(200).send(ref) | ||
}).catch(err => { | ||
res.status(400).send({ | ||
message: "An error has occured updating the posting. " + err | ||
}); | ||
}); | ||
} else { | ||
res.status(400).send({ | ||
message: "Input is invalid. " + validationError | ||
}); | ||
} | ||
}) | ||
// Delete a posting | ||
app.delete('/postings/:postingId', (req, res) => { | ||
db.collection('postings').doc(req.params.postingId).delete() | ||
.then(ref => { | ||
res.status(200).send(ref) | ||
}).catch(err => { | ||
res.status(400).send({ | ||
message: "An error has occured deleting the posting. " + err | ||
}); | ||
}); | ||
}) | ||
// Validation helper function for postings | ||
function validatePostingInput(body) { | ||
if (!body.jobTitle) return "Job Title cannot be empty"; | ||
return null; | ||
} |
Oops, something went wrong.