From 161fd4afab323ca6bf491def473f84bb7557b481 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 7 Mar 2018 17:48:32 +0900 Subject: wip --- src/api/models/othello-game.ts | 20 ++++++++++++++++---- src/api/models/othello-matching.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) (limited to 'src/api/models') diff --git a/src/api/models/othello-game.ts b/src/api/models/othello-game.ts index a6beaaf9c7..b9fd94ebc0 100644 --- a/src/api/models/othello-game.ts +++ b/src/api/models/othello-game.ts @@ -1,6 +1,7 @@ import * as mongo from 'mongodb'; import deepcopy = require('deepcopy'); import db from '../../db/mongodb'; +import { IUser, pack as packUser } from './user'; const Game = db.get('othello_games'); export default Game; @@ -15,19 +16,30 @@ export interface IGame { /** * Pack an othello game for API response - * - * @param {any} game - * @return {Promise} */ export const pack = ( - game: any + game: any, + me?: string | mongo.ObjectID | IUser ) => new Promise(async (resolve, reject) => { + // Me + const meId: mongo.ObjectID = me + ? mongo.ObjectID.prototype.isPrototypeOf(me) + ? me as mongo.ObjectID + : typeof me === 'string' + ? new mongo.ObjectID(me) + : (me as IUser)._id + : null; + const _game = deepcopy(game); // Rename _id to id _game.id = _game._id; delete _game._id; + // Populate user + _game.black_user = await packUser(_game.black_user_id, meId); + _game.white_user = await packUser(_game.white_user_id, meId); + resolve(_game); }); diff --git a/src/api/models/othello-matching.ts b/src/api/models/othello-matching.ts index bd7aeef3cf..89fcd6df6a 100644 --- a/src/api/models/othello-matching.ts +++ b/src/api/models/othello-matching.ts @@ -1,11 +1,42 @@ import * as mongo from 'mongodb'; +import deepcopy = require('deepcopy'); import db from '../../db/mongodb'; +import { IUser, pack as packUser } from './user'; const Matching = db.get('othello_matchings'); export default Matching; export interface IMatching { _id: mongo.ObjectID; + created_at: Date; parent_id: mongo.ObjectID; child_id: mongo.ObjectID; } + +/** + * Pack an othello matching for API response + */ +export const pack = ( + matching: any, + me?: string | mongo.ObjectID | IUser +) => new Promise(async (resolve, reject) => { + + // Me + const meId: mongo.ObjectID = me + ? mongo.ObjectID.prototype.isPrototypeOf(me) + ? me as mongo.ObjectID + : typeof me === 'string' + ? new mongo.ObjectID(me) + : (me as IUser)._id + : null; + + const _matching = deepcopy(matching); + + delete _matching._id; + + // Populate user + _matching.parent = await packUser(_matching.parent_id, meId); + _matching.child = await packUser(_matching.child_id, meId); + + resolve(_matching); +}); -- cgit v1.2.3-freya