-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* install slug * -- * modelldefinition-mit-mongoose * add routes * -- * GET-Route/POST-Route * cancel slug gen. / add linkController * add deleteLink * add PUT-Route * -- * -- * aktuell. Datensatz als Antwort zurückzusenden
- Loading branch information
1 parent
f43d587
commit a1929eb
Showing
9 changed files
with
516 additions
and
25 deletions.
There are no files selected for viewing
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
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,59 @@ | ||
import { Request, Response } from "express"; | ||
import Link from "../models/Link"; | ||
|
||
export default { | ||
// GET-Route zum Abrufen aller Links | ||
getAllLinks: async (req: Request, res: Response) => { | ||
try { | ||
const links = await Link.find(); | ||
console.log(links); | ||
res.json(links); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}, | ||
|
||
// POST-Route zum Erstellen eines neuen Links | ||
createLink: async (req: Request, res: Response) => { | ||
try { | ||
const link = new Link(req.body); | ||
const newLink = await link.save(); | ||
console.log(newLink); | ||
res.status(201).json(newLink); | ||
} catch (error) { | ||
res.status(400).json({ message: error.message }); | ||
} | ||
}, | ||
|
||
// DELETE-Route zum Löschen eines Links | ||
deleteLink: async (req: Request, res: Response) => { | ||
console.log(req.params); | ||
try { | ||
const { id } = req.params; | ||
const link = await Link.findByIdAndDelete(id); | ||
|
||
if (!link) { | ||
return res.status(404).json({ message: "Link not found" }); | ||
} | ||
res.status(200).json({ message: "Link deleted succesfully" }); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}, | ||
|
||
// PUT-Route zum Aktualisieren eines Links | ||
updateLink: async (req: Request, res: Response) => { | ||
console.log(req.body); | ||
try { | ||
const { id } = req.params; | ||
const link = await Link.findByIdAndUpdate(id, req.body, { new: true }); | ||
if (!link) { | ||
return res.status(404).json({ message: "Link not found" }); | ||
} | ||
console.log("Updated link data:", link); | ||
res.status(200).json({ message: "Link updated succesfully", link: link }); | ||
} catch (error) { | ||
res.status(400).json({ message: error.message }); | ||
} | ||
}, | ||
}; |
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,22 @@ | ||
import mongoose, { Document, Schema } from "mongoose"; | ||
export interface Link extends Document { | ||
url: string; | ||
path: string; | ||
created: Date; | ||
modified: Date; | ||
} | ||
|
||
const linkSchema: Schema = new Schema( | ||
{ | ||
url: { type: String, required: true }, | ||
path: { type: String, required: true, unique: true }, | ||
//slug: { type: String, slug: "path", unique: true }, | ||
}, | ||
{ | ||
timestamps: { createdAt: "created", updatedAt: "modified" }, | ||
} | ||
); | ||
|
||
const Link = mongoose.model<Link>("Link", linkSchema); | ||
|
||
export default Link; |
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,48 @@ | ||
import express from "express"; | ||
import passport from "passport"; | ||
import linkController from "../controller/linkController"; | ||
|
||
const router = express.Router(); | ||
|
||
// GET-Route zum Abrufen aller Links | ||
router.get( | ||
"/link", | ||
passport.authenticate("jwt", { session: false }), | ||
linkController.getAllLinks | ||
); | ||
|
||
// POST-Route zum Erstellen eines neuen Links | ||
router.post( | ||
"/link", | ||
passport.authenticate("jwt", { session: false }), | ||
linkController.createLink | ||
); | ||
|
||
// DELETE-Route zum Löschen eines Links | ||
router.delete( | ||
"/link/:id", | ||
passport.authenticate("jwt", { session: false }), | ||
linkController.deleteLink | ||
); | ||
|
||
// PUT-Route zum Aktualisieren eines Links | ||
router.put( | ||
"/link/:id", | ||
passport.authenticate("jwt", { session: false }), | ||
linkController.updateLink | ||
); | ||
|
||
// Test Routen | ||
router.post("/test", (req, res) => { | ||
res.json({ message: "Test route works!" }); | ||
}); | ||
router.get("/test", (req, res) => { | ||
res.json({ message: "Test route works!" }); | ||
}); | ||
router.put("/test", (req, res) => { | ||
res.json({ message: "Test route works!" }); | ||
}); | ||
router.delete("/test", (req, res) => { | ||
res.json({ message: "Test route works!" }); | ||
}); | ||
export default router; |
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,12 @@ | ||
import express from "express"; | ||
import passport from "passport"; | ||
import accountController from "../controller/accountController"; | ||
|
||
const router = express.Router(); | ||
router.get( | ||
"/status", | ||
passport.authenticate("jwt", { session: false }), | ||
accountController.getStatus | ||
); | ||
|
||
export default router; |
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,22 @@ | ||
import express from "express"; | ||
import passport from "passport"; | ||
import accountController from "../controller/accountController"; | ||
|
||
const router = express.Router(); | ||
|
||
router.post("/login", passport.authenticate("local"), accountController.login); | ||
|
||
router.post("/register", accountController.register); | ||
|
||
router.post("/logout", function (req, res, next) { | ||
req.session.destroy(function (err) { | ||
if (err) { | ||
return next(err); | ||
} | ||
res.json({ | ||
message: "User successfully logout!", | ||
}); | ||
}); | ||
}); | ||
|
||
export default router; |
Oops, something went wrong.