-
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
3a68c74
commit 6bc2b08
Showing
6 changed files
with
136 additions
and
94 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import crypto from "crypto"; | ||
|
||
const secret = crypto.randomBytes(32).toString("hex"); | ||
console.log(secret); |
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
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,98 +1,127 @@ | ||
import { Router } from "express"; | ||
import { Router, type Response, type Request } from "express"; | ||
import DocumentModel from "../models/document.model"; | ||
import { verifyToken } from "../middleware/authMiddleware"; | ||
import { verifyToken } from "../middleware/verifyToken"; | ||
|
||
const router = Router(); | ||
|
||
router.post("/", verifyToken, async (req, res) => { | ||
const { title, content } = req.body; | ||
|
||
try { | ||
const newDocument = new DocumentModel({ title, content }); | ||
await newDocument.save(); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to create document", error }); | ||
router.post( | ||
"/", | ||
verifyToken, | ||
async (req: Request, res: Response): Promise<void> => { | ||
const { title, content } = req.body; | ||
|
||
try { | ||
const newDocument = new DocumentModel({ title, content }); | ||
await newDocument.save(); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to create document", error }); | ||
} | ||
} | ||
}); | ||
|
||
router.get("/:id", verifyToken, async (req, res) => { | ||
try { | ||
const document = await DocumentModel.findById(req.params.id); | ||
if (!document) { | ||
return res.status(404).json({ message: "Document not found" }); | ||
); | ||
|
||
router.get( | ||
"/:id", | ||
verifyToken, | ||
async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const document = await DocumentModel.findById(req.params.id); | ||
if (!document) { | ||
res.status(404).json({ message: "Document not found" }); | ||
return; | ||
} | ||
|
||
res.json(document); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to get document", error }); | ||
} | ||
|
||
res.json(document); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to get document", error }); | ||
} | ||
}); | ||
|
||
router.put("/:id", verifyToken, async (req, res) => { | ||
const { content } = req.body; | ||
try { | ||
const document = await DocumentModel.findByIdAndUpdate( | ||
req.params.id, | ||
{ content, $push: { versions: { content } } }, | ||
{ new: true } | ||
); | ||
|
||
if (!document) { | ||
return res.status(404).json({ message: "Document not found" }); | ||
); | ||
|
||
router.put( | ||
"/:id", | ||
verifyToken, | ||
async (req: Request, res: Response): Promise<void> => { | ||
const { content } = req.body; | ||
try { | ||
const document = await DocumentModel.findByIdAndUpdate( | ||
req.params.id, | ||
{ content, $push: { versions: { content } } }, | ||
{ new: true } | ||
); | ||
|
||
if (!document) { | ||
res.status(404).json({ message: "Document not found" }); | ||
return; | ||
} | ||
|
||
res.json(document); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to update document", error }); | ||
} | ||
|
||
res.json(document); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to update document", error }); | ||
} | ||
}); | ||
|
||
router.delete("/:id", verifyToken, async (req, res) => { | ||
try { | ||
const document = await DocumentModel.findByIdAndDelete(req.params.id); | ||
if (!document) { | ||
return res.status(404).json({ message: "Document not found" }); | ||
); | ||
|
||
router.delete( | ||
"/:id", | ||
verifyToken, | ||
async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const document = await DocumentModel.findByIdAndDelete(req.params.id); | ||
if (!document) { | ||
res.status(404).json({ message: "Document not found" }); | ||
return; | ||
} | ||
|
||
res.json({ message: "Document deleted successfully" }); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to delete document", error }); | ||
} | ||
|
||
res.json({ message: "Document deleted successfully" }); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to delete document", error }); | ||
} | ||
}); | ||
|
||
router.get("/:id/versions", verifyToken, async (req, res) => { | ||
try { | ||
const document = await DocumentModel.findById(req.params.id); | ||
if (!document) { | ||
return res.status(404).json({ message: "Document not found" }); | ||
); | ||
|
||
router.get( | ||
"/:id/versions", | ||
verifyToken, | ||
async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const document = await DocumentModel.findById(req.params.id); | ||
if (!document) { | ||
res.status(404).json({ message: "Document not found" }); | ||
return; | ||
} | ||
|
||
res.json(document.versions); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to fetch versions", error }); | ||
} | ||
|
||
res.json(document.versions); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to fetch versions", error }); | ||
} | ||
}); | ||
|
||
router.put("/:id/restore", verifyToken, async (req, res) => { | ||
const { versionIndex } = req.body; | ||
|
||
try { | ||
const document = await DocumentModel.findByIdAndUpdate(req.params.id); | ||
if ( | ||
!document || | ||
versionIndex < 0 || | ||
versionIndex >= document.versions.length | ||
) { | ||
return res.status(400).json({ message: "Invalid version index" }); | ||
); | ||
|
||
router.put( | ||
"/:id/restore", | ||
verifyToken, | ||
async (req: Request, res: Response): Promise<void> => { | ||
const { versionIndex } = req.body; | ||
|
||
try { | ||
const document = await DocumentModel.findByIdAndUpdate(req.params.id); | ||
if ( | ||
!document || | ||
versionIndex < 0 || | ||
versionIndex >= document.versions.length | ||
) { | ||
res.status(400).json({ message: "Invalid version index" }); | ||
return; | ||
} | ||
|
||
document.content = document.versions[versionIndex].content; | ||
await document.save(); | ||
|
||
res.json(document); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to restore version:", error }); | ||
} | ||
|
||
document.content = document.versions[versionIndex].content; | ||
await document.save(); | ||
|
||
res.json(document); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to restore version:", error }); | ||
} | ||
}); | ||
); | ||
|
||
export default router; |