-
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.
Refactor MongoDB connection and update code formatting
- Loading branch information
Showing
3 changed files
with
91 additions
and
26 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,35 @@ | ||
import express, { Request, Response } from "express"; | ||
import MessageModel from "../models/Message"; | ||
|
||
const router = express.Router(); | ||
|
||
// Route pour récupérer tous les messages | ||
router.get("/messages", async (req: Request, res: Response) => { | ||
try { | ||
const messages = await MessageModel.find({}); | ||
res.json(messages); | ||
} catch (error) { | ||
res.status(500).json({ message: (error as Error).message }); | ||
} | ||
}); | ||
|
||
// Route pour ajouter un nouveau message | ||
router.post("/messages", async (req: Request, res: Response) => { | ||
const newMessage = { | ||
timestamp: new Date(), // ou req.body.timestamp | ||
content: req.body.content, | ||
}; | ||
|
||
try { | ||
const updatedDocument = await MessageModel.findOneAndUpdate( | ||
{}, | ||
{ $push: { messages: newMessage } }, | ||
{ new: true, upsert: true } | ||
); | ||
res.status(201).json(updatedDocument); | ||
} catch (error) { | ||
res.status(400).json({ message: (error as Error).message }); | ||
} | ||
}); | ||
|
||
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,27 @@ | ||
import express, { Request, Response } from "express"; | ||
import UserModel from "../models/Users"; | ||
|
||
const router = express.Router(); | ||
|
||
// Route pour créer un nouvel utilisateur | ||
router.post("/users", async (req: Request, res: Response) => { | ||
const newUser = new UserModel({ | ||
username: req.body.username, | ||
password: req.body.password, // Vous devriez hasher le mot de passe | ||
}); | ||
|
||
try { | ||
const savedUser = await newUser.save(); | ||
res.status(201).json(savedUser); | ||
} catch (error) { | ||
res.status(400).json({ message: (error as Error).message }); | ||
} | ||
}); | ||
|
||
// Route pour authentifier un utilisateur | ||
// Ajoutez ici la logique d'authentification | ||
router.post("/users/authenticate", async (req: Request, res: Response) => { | ||
// Implémentation de l'authentification | ||
}); | ||
|
||
export default router; |