Skip to content

Commit

Permalink
update and fix functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanBonsignori committed Mar 11, 2024
1 parent a3dda57 commit bc832c6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 93 deletions.
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@
"WithStatement",
"BinaryExpression[operator=\"in\"]"
],
"arrow-parens": 0
"arrow-parens": 0,
"import/no-unresolved": [
"error",
{
"ignore": ["^firebase-admin/.+", "^firebase-functions/.+"]
}
]
},
"settings": {
"react": {
Expand Down
86 changes: 33 additions & 53 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,54 @@
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const cors = require('cors')({ origin: true });
const { onRequest } = require('firebase-functions/v2/https');
const { initializeApp } = require('firebase-admin/app');
const { getFirestore } = require('firebase-admin/firestore');

// Init Express App
const app = express();
app.use(cors);
initializeApp();
const db = getFirestore();

admin.initializeApp();

exports.blog = functions.https.onRequest(app);

const db = admin.firestore();

const blogCol = db.collection('blog');
const increment = admin.firestore.FieldValue.increment(1);
const decrement = admin.firestore.FieldValue.increment(-1);

function createNewDoc(blog) {
async function createNewDoc(blog) {
db.collection('blog').doc(blog).set({ likes: 0 });
}

app.get('/:blog', (req, res) => {
const { blog } = req.params;
db.collection('blog')
.doc(blog)
.get()
.then((doc) => {
if (doc.exists) {
return res.status(200).json(doc.data());
}
createNewDoc(blog);
return res.status(200).json({ likes: 0 });
})
.catch((error) =>
res.status(500).json({ message: 'Error in GET /:blog', error }),
);
});

app.put('/:blog', (req, res) => {
const { blog } = req.params;
exports.getBlog = onRequest(async (req, res) => {
const blog = req.params[0];
db.collection('blog')
.doc(blog)
.get()
.then((doc) => {
if (doc.exists) {
blogCol.doc(blog).update({ likes: increment });
return res.status(200).json({ success: true });
if (!doc.exists) {
createNewDoc(blog);
return res.status(200).json({ success: true, likes: doc.data().likes });
}
createNewDoc(blog);
return res.status(200).json({ success: true });
return res.status(200).json({ success: true, likes: doc.data().likes });
})
.catch((error) =>
res.status(500).json({ message: 'Error in PUT /:blog', error }),
);
.catch((error) => {
return res.status(500).json({
message: 'Error in blog onRequest /:blog',
error: error.message,
});
});
});

app.put('/:blog/unlike', (req, res) => {
const { blog } = req.params;
exports.likeBlog = onRequest(async (req, res) => {
const blog = req.params[0];
db.collection('blog')
.doc(blog)
.get()
.then((doc) => {
if (doc.exists) {
blogCol.doc(blog).update({ likes: decrement });
return res.status(200).json({ success: true });
if (!doc.exists) {
createNewDoc(blog);
return res
.status(200)
.json({ success: true, likes: doc.data().likes + 1 });
}
return res.status(200).json({ success: true });
doc.ref.update({ likes: doc.data().likes + 1 });
return res.status(200).json({ success: true, likes: doc.data().likes });
})
.catch((error) =>
res.status(500).json({ message: 'Error in PUT /:blog/unlike', error }),
);
.catch((error) => {
return res.status(500).json({
message: 'Error in blog onRequest /:blog',
error: error.message,
});
});
});
61 changes: 25 additions & 36 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
"logs": "firebase functions:log"
},
"engines": {
"node": "14"
"node": "20"
},
"main": "index.js",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"express": "^4.18.3",
"firebase-admin": "^12.0.0",
"firebase-functions": "^3.11.0"
"firebase-functions": "^4.8.0"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
Expand Down

0 comments on commit bc832c6

Please sign in to comment.