forked from RivaanRanawat/flutter-google-docs-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.js
51 lines (44 loc) · 1.25 KB
/
document.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const express = require("express");
const Document = require("../models/document");
const documentRouter = express.Router();
const auth = require("../middlewares/auth");
documentRouter.post("/doc/create", auth, async (req, res) => {
try {
const { createdAt } = req.body;
let document = new Document({
uid: req.user,
title: "Untitled Document",
createdAt,
});
document = await document.save();
res.json(document);
} catch (e) {
res.status(500).json({ error: e.message });
}
});
documentRouter.get("/docs/me", auth, async (req, res) => {
try {
let documents = await Document.find({ uid: req.user });
res.json(documents);
} catch (e) {
res.status(500).json({ error: e.message });
}
});
documentRouter.post("/doc/title", auth, async (req, res) => {
try {
const { id, title } = req.body;
const document = await Document.findByIdAndUpdate(id, { title });
res.json(document);
} catch (e) {
res.status(500).json({ error: e.message });
}
});
documentRouter.get("/doc/:id", auth, async (req, res) => {
try {
const document = await Document.findById(req.params.id);
res.json(document);
} catch (e) {
res.status(500).json({ error: e.message });
}
});
module.exports = documentRouter;