Last active
September 8, 2019 08:38
-
-
Save alfianlosari/e253f694c963e8dee71e2010f9fde724 to your computer and use it in GitHub Desktop.
route index
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
import express, { Router, Request } from 'express'; | |
import admin from 'firebase-admin'; | |
admin.initializeApp({ | |
credential: admin.credential.applicationDefault() | |
}); | |
const db = admin.firestore(); | |
const router = Router() | |
router.get('/', async (req, res, next) => { | |
try { | |
const noteSnapshot = await db.collection('notes').get(); | |
const notes = []; | |
noteSnapshot.forEach((doc) => { | |
notes.push({ | |
id: doc.id, | |
data: doc.data() | |
}); | |
}); | |
res.json(notes); | |
} catch(e) { | |
next(e); | |
} | |
}); | |
router.get('/:id', async(req, res, next) => { | |
try { | |
const id = req.params.id; | |
if (!id) throw new Error('id is blank'); | |
const note = await db.collection('notes').doc(id).get(); | |
if (!note.exists) { | |
throw new Error('note does not exists'); | |
} | |
res.json({ | |
id: note.id, | |
data: note.data() | |
}); | |
} catch(e) { | |
next(e); | |
} | |
}) | |
router.post('/', async (req, res, next) => { | |
try { | |
const text = req.body.text; | |
if (!text) throw new Error('Text is blank'); | |
const data = { text }; | |
const ref = await db.collection('notes').add(data); | |
res.json({ | |
id: ref.id, | |
data | |
}); | |
} catch(e) { | |
next(e); | |
} | |
}); | |
router.put('/:id', async (req, res, next) => { | |
try { | |
const id = req.params.id; | |
const text = req.body.text; | |
if (!id) throw new Error('id is blank'); | |
if (!text) throw new Error('Text is blank'); | |
const data = { text }; | |
const ref = await db.collection('notes').doc(id).set(data, { merge: true }); | |
res.json({ | |
id, | |
data | |
}); | |
} catch(e) { | |
next(e); | |
} | |
}); | |
router.delete('/:id', async (req, res, next) => { | |
try { | |
const id = req.params.id; | |
if (!id) throw new Error('id is blank'); | |
await db.collection('notes').doc(id).delete(); | |
res.json({ | |
id | |
}); | |
} catch(e) { | |
next(e); | |
} | |
}); | |
export default router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment