Skip to content

Instantly share code, notes, and snippets.

@bankchart
Last active September 3, 2019 09:46
Show Gist options
  • Select an option

  • Save bankchart/d3b2bf91072349c97a8174e9d5fa76f3 to your computer and use it in GitHub Desktop.

Select an option

Save bankchart/d3b2bf91072349c97a8174e9d5fa76f3 to your computer and use it in GitHub Desktop.
cloud functions code
const admin = require('firebase-admin');
const appConstant = require('../../const');
exports = module.exports = async (req, res) => {
let response = {};
try {
const db = admin.firestore();
const postId = uuid4();
const currentDatetime = new Date();
await db.collection('post').doc(postId)
.set({
id: postId,
owner: {
displayName: 'bank'
},
subject: req.body.subject,
body: req.body.content,
postDatetime: currentDatetime,
createdBy: 'bank',
createdDatetime: currentDatetime,
updatedBy: 'bank',
updatedDatetime: currentDatetime
});
res.json(appConstant.buildServiceSuccess(response));
} catch (err) {
console.error(err);
res.json(appConstant.buildServiceError(err, response));
}
}
const admin = require('firebase-admin');
const uuid4 = require('uuid/v4');
const appConstant = require('../../const');
exports = module.exports = async (req, res) => {
let response = {};
try {
const db = admin.firestore();
const postId = req.query.postId;
if (!postId || !postId.trim()) {
throw appConstant.B_ERROR.RequiredPostID();
}
const postDoc = await db.collection('post').doc(postId).get();
const postDocResult = postDoc.data();
if (!postDocResult) {
throw appConstant.B_ERROR.NotFoundPost();
}
response.data = {};
Object.assign(response.data, postDocResult);
res.json(appConstant.buildServiceSuccess(response));
} catch (err) {
console.error(err);
res.json(appConstant.buildServiceError(err, response));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment