UNPKG

@andreabiagini5/applicazioni-e-servizi-web-project

Version:
98 lines 3.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DBMatch = exports.MatchRepository = void 0; const mongoose_1 = __importDefault(require("mongoose")); const Match_1 = require("../models/Match"); class MatchRepository { constructor() { } async createMatch(match, matchId) { const dbmatch = new exports.DBMatch({ _id: matchId, player1: match.player1, player2: match.player2, creationDate: match.creationDate, initialState: match.initialState, moves: match.moves, ratingDelta: match.ratingDelta, }); return (await dbmatch.save())._id.toString(); } async findMatch(matchId) { const match = await exports.DBMatch.findById(matchId); if (match) { return Match_1.MatchFactory.createFromObject(match); } else { return null; } } async findMatchesByPlayer(player) { const matches = await exports.DBMatch.find({ $or: [{ player1: player }, { player2: player }], }); return matches .sort((a, b) => { return b.creationDate.getTime() - a.creationDate.getTime(); }) .map(match => match._id.toString()); } async updateMatch(matchId, newMatch) { if (!mongoose_1.default.Types.ObjectId.isValid(matchId)) { throw new Error(`Invalid matchId: ${matchId}`); } const result = await exports.DBMatch.findOneAndUpdate({ _id: matchId }, { $set: newMatch }, { new: true, upsert: false, runValidators: true, }); if (!result) { throw new Error(`Match ${matchId} not found or update failed`); } } async deleteMatch(matchId) { return ((await exports.DBMatch.deleteOne({ _id: new mongoose_1.default.Types.ObjectId(matchId) })).deletedCount > 0); } } exports.MatchRepository = MatchRepository; // Mongoose schemas const pileSchema = new mongoose_1.default.Schema({ owner: { type: String, required: true, }, numberOfGrains: { type: Number, required: true, min: 1, }, }); const cellSchema = new mongoose_1.default.Schema({ pile: { type: pileSchema, required: false, default: null, }, }); const boardSchema = new mongoose_1.default.Schema({ height: { type: Number, required: true }, width: { type: Number, required: true }, state: [[cellSchema]], }); const matchSchema = new mongoose_1.default.Schema({ player1: { type: String, required: true }, player2: { type: String, required: true }, creationDate: { type: Date, required: true, default: Date.now }, initialState: { type: boardSchema, required: true }, moves: [ { x: { type: Number, required: true }, y: { type: Number, required: true }, }, ], ratingDelta: { type: Number, default: null }, }); exports.DBMatch = mongoose_1.default.model('Match', matchSchema); //# sourceMappingURL=match.js.map