Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tan Benjamin committed Jun 8, 2019
0 parents commit 9b25c3b
Show file tree
Hide file tree
Showing 11 changed files with 3,843 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "candy-243011"
}
}
65 changes: 65 additions & 0 deletions .gitignore
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
20 changes: 20 additions & 0 deletions firebase.json
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"
}
]
}
}
4 changes: 4 additions & 0 deletions firestore.indexes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"indexes": [],
"fieldOverrides": []
}
7 changes: 7 additions & 0 deletions firestore.rules
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;
}
}
}
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
117 changes: 117 additions & 0 deletions functions/index.js
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;
}
Loading

0 comments on commit 9b25c3b

Please sign in to comment.