56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
import { orm } from '../orm/orm';
|
|
import { UnwrappedRequest } from '../utilities/guard';
|
|
import { CreatedResponse, ErrorResponse, OkResponse } from '../utilities/responseHelper';
|
|
import { CreateMatchRequest } from '../utilities/requestModels';
|
|
import { GameId, MatchId, PlayerId, UserId } from '../utilities/secureIds';
|
|
import { MatchParticipant } from '../orm/matches';
|
|
|
|
async function create(request: UnwrappedRequest<CreateMatchRequest>): Promise<Response> {
|
|
try {
|
|
return new CreatedResponse(
|
|
await orm.matches.create({
|
|
gameId: GameId.fromHash(request.body.gameId),
|
|
ownerId: request.claims.userId as UserId,
|
|
participants: request.body.participants.map(
|
|
(x) => new MatchParticipant(PlayerId.fromHash(x.playerId), x.standing),
|
|
),
|
|
}),
|
|
);
|
|
} catch (error: any) {
|
|
return new ErrorResponse(error as Error);
|
|
}
|
|
}
|
|
|
|
async function get(request: UnwrappedRequest): Promise<Response> {
|
|
try {
|
|
return new OkResponse(await orm.matches.get(MatchId.fromHash(request.params.id), request.claims));
|
|
} catch (error: any) {
|
|
return new ErrorResponse(error as Error);
|
|
}
|
|
}
|
|
|
|
async function drop(request: UnwrappedRequest): Promise<Response> {
|
|
try {
|
|
return new OkResponse(await orm.matches.drop(MatchId.fromHash(request.params.id), request.claims));
|
|
} catch (error: any) {
|
|
return new ErrorResponse(error as Error);
|
|
}
|
|
}
|
|
|
|
async function leave(request: UnwrappedRequest): Promise<Response> {
|
|
try {
|
|
return new OkResponse(
|
|
await orm.matches.removePlayer(MatchId.fromHash(request.params.id), request.claims.userId as UserId),
|
|
);
|
|
} catch (error: any) {
|
|
return new ErrorResponse(error as Error);
|
|
}
|
|
}
|
|
|
|
export default {
|
|
create,
|
|
get,
|
|
drop,
|
|
leave,
|
|
};
|