Skip to content

Commit

Permalink
26 url slug (#28)
Browse files Browse the repository at this point in the history
* 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
jipsonminibhavan authored Jan 15, 2024
1 parent f43d587 commit a1929eb
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 25 deletions.
41 changes: 41 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"express-session": "^1.17.3",
"jsonwebtoken": "^9.0.2",
"jwt-simple": "^0.5.6",
"mongoose-slug-generator": "^1.0.4",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
Expand Down
59 changes: 59 additions & 0 deletions backend/src/controller/linkController.ts
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 });
}
},
};
33 changes: 8 additions & 25 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import express, { Request, Response, NextFunction } from "express";
import express, { Request, Response } from "express";
import bodyParser from "body-parser";
import cors from "cors";
import connectToDB from "./db";
import { PORT, PASSPORT_SECRET } from "./config";
import passport from "passport";
import { Strategy as LocalStrategy } from "passport-local";
import { UserModel } from "./models/Users";
import accountController from "./controller/accountController";
import initializePassport from "./middleware/auth";
import session from "express-session";
import userRoutes from "./routes/userRoutes";
import statusRoutes from "./routes/statusRoutes";
import linkRoutes from "./routes/linkRoutes";

const app = express();
app.use(express.json());

Expand Down Expand Up @@ -40,29 +43,9 @@ passport.use(new LocalStrategy(UserModel.authenticate()));
passport.serializeUser(UserModel.serializeUser());
passport.deserializeUser(UserModel.deserializeUser());

app.get("/", (req, res) => {
res.send("Introduction JWT Auth");
});

app.post("/login", passport.authenticate("local"), accountController.login);
app.post("/register", accountController.register);
app.get(
"/status",
passport.authenticate("jwt", { session: false }),
accountController.getStatus
);
app.post("/logout", function (req, res, next) {
console.log(req.session);

req.session.destroy(function (err) {
if (err) {
return next(err);
}
res.json({
message: "User successfully logout!",
});
});
});
app.use("/", userRoutes);
app.use("/", statusRoutes);
app.use("/", linkRoutes);

//Express-Server
app.listen(PORT, () => {
Expand Down
22 changes: 22 additions & 0 deletions backend/src/models/Link.ts
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;
48 changes: 48 additions & 0 deletions backend/src/routes/linkRoutes.ts
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;
12 changes: 12 additions & 0 deletions backend/src/routes/statusRoutes.ts
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;
22 changes: 22 additions & 0 deletions backend/src/routes/userRoutes.ts
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;
Loading

0 comments on commit a1929eb

Please sign in to comment.