-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
a3dda57
commit bc832c6
Showing
4 changed files
with
68 additions
and
93 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
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,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, | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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