Changes in this commit:
- New model added for games played - Game model ref added to User model - New APIs created to create and get games
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
const { Schema, default: mongoose } = require("mongoose");
|
||||
const { String, ObjectId, Number } = Schema.Types;
|
||||
|
||||
const gameSchema = new Schema({
|
||||
white: {
|
||||
type: ObjectId,
|
||||
ref: "User",
|
||||
},
|
||||
black: {
|
||||
type: ObjectId,
|
||||
ref: "User",
|
||||
},
|
||||
timeLimit: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
roomID: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
pgn: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
winner: {
|
||||
type: String,
|
||||
enum: ["b", "w", "n"],
|
||||
},
|
||||
});
|
||||
|
||||
const Game = mongoose.model("Game", gameSchema);
|
||||
module.exports = { Game };
|
||||
@@ -27,6 +27,12 @@ const userSchema = new Schema(
|
||||
ref: "User",
|
||||
},
|
||||
],
|
||||
games: [
|
||||
{
|
||||
type: ObjectId,
|
||||
ref: "Game",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
virtuals: {
|
||||
@@ -44,9 +50,14 @@ const userSchema = new Schema(
|
||||
return {
|
||||
username: friend.username,
|
||||
email: friend.email,
|
||||
id: friend.id,
|
||||
};
|
||||
});
|
||||
},
|
||||
async getGames() {
|
||||
await this.populate("games");
|
||||
return this.games;
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
+21
-2
@@ -1,5 +1,6 @@
|
||||
const router = require("express").Router();
|
||||
const { Challenge } = require("../models/challenge");
|
||||
const { Game } = require("../models/game");
|
||||
const { User } = require("../models/user");
|
||||
const { checkAuth } = require("../util/auth");
|
||||
const { catchAsync } = require("../util/errors");
|
||||
@@ -174,16 +175,34 @@ router.get(
|
||||
"/:userid/games",
|
||||
checkAuth,
|
||||
catchAsync(async (req, res, next) => {
|
||||
res.send("TODO");
|
||||
const userData = await User.findOne();
|
||||
let gamesData = await userData.getGames();
|
||||
if (!gamesData) gamesData = [];
|
||||
return res.status(200).json({ success: true, data: gamesData });
|
||||
})
|
||||
);
|
||||
|
||||
// TODO
|
||||
// add a game
|
||||
router.post("/:userid/game", checkAuth, async (req, res, next) => {
|
||||
const gameData = req.body;
|
||||
const gameDoc = await Game.create(gameData);
|
||||
return res.json({ success: true, data: gameDoc });
|
||||
});
|
||||
|
||||
// TODO
|
||||
// get a particular game
|
||||
router.get(
|
||||
"/:userid/games/:gameid",
|
||||
checkAuth,
|
||||
catchAsync(async (req, res, next) => {
|
||||
res.send("TODO");
|
||||
const { gameid } = req.params;
|
||||
const gameData = await Game.findById(gameid);
|
||||
if (gameData) {
|
||||
return res.status(200).json({ success: true, data: gameData });
|
||||
} else {
|
||||
return res.status(404).json({ success: false, error: { message: "Game not found" } });
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user