-
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.
Merge branch 'main' of https://github.com/H1B0B0/Projet-JSF-STG16
- Loading branch information
Showing
11 changed files
with
191 additions
and
31 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,30 @@ | ||
import { Schema, model, Document, Model } from "mongoose"; | ||
|
||
interface ISingleUser extends Document { | ||
username: string; | ||
socketId: string; | ||
} | ||
|
||
interface ISingleUserModel extends Model<ISingleUser> { | ||
findAll(): Promise<ISingleUser[]>; | ||
} | ||
|
||
const singleUserSchema = new Schema<ISingleUser>({ | ||
username: { type: String, required: true, unique: true }, | ||
socketId: { type: String, required: true }, | ||
}); | ||
|
||
singleUserSchema.statics.findAll = function (): Promise<ISingleUser[]> { | ||
return this.find().exec(); | ||
}; | ||
|
||
singleUserSchema.statics.findOneByUsername = function ( | ||
socketId: string | ||
): Promise<ISingleUser | null> { | ||
return this.findOne({ socketId }).exec(); | ||
}; | ||
|
||
export const SingleUser = model<ISingleUser, ISingleUserModel>( | ||
"SingleUser", | ||
singleUserSchema | ||
); |
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,74 @@ | ||
import express, { Request, Response } from "express"; | ||
export const router = express.Router(); | ||
import { SingleUser } from "../models/single_user.js"; | ||
|
||
router.post("/newuser", async (req: Request, res: Response) => { | ||
const { username, socketId } = req.body; | ||
|
||
console.log(req.body); // Log the request body | ||
|
||
try { | ||
const existingUser = await SingleUser.findOne({ | ||
$or: [{ username }, { socketId }], | ||
}); | ||
if (existingUser) { | ||
return res | ||
.status(400) | ||
.json({ message: "Username or SocketId already exists" }); | ||
} | ||
|
||
const newUser = await SingleUser.create({ | ||
username, | ||
socketId, | ||
}); | ||
|
||
res.status(201).json(newUser); | ||
} catch (error: any) { | ||
console.error(error); // Log the entire error | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}); | ||
|
||
router.get("/", async (req: Request, res: Response) => { | ||
try { | ||
const users = await SingleUser.find(); | ||
res.status(200).json(users); | ||
} catch (error: any) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}); | ||
|
||
router.get("/:socketId", async (req: Request, res: Response) => { | ||
const { socketId } = req.params; | ||
|
||
try { | ||
const user = await SingleUser.findOne({ socketId }); | ||
|
||
if (!user) { | ||
return res.status(404).json({ message: "User not found" }); | ||
} | ||
|
||
res.status(200).json(user); | ||
} catch (error: any) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}); | ||
|
||
router.put("/updateusername", async (req: Request, res: Response) => { | ||
const { socketId, newUsername } = req.body; | ||
|
||
try { | ||
const user = await SingleUser.findOne({ socketId }); | ||
|
||
if (!user) { | ||
return res.status(404).json({ message: "User not found" }); | ||
} | ||
|
||
user.username = newUsername; | ||
const updatedUser = await user.save(); | ||
|
||
res.status(200).json(updatedUser); | ||
} catch (error: any) { | ||
res.status(500).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
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
Oops, something went wrong.