diff --git a/packages/kcms/src/match/service/fetchRunResult.test.ts b/packages/kcms/src/match/service/fetchRunResult.test.ts new file mode 100644 index 00000000..fa8a36ae --- /dev/null +++ b/packages/kcms/src/match/service/fetchRunResult.test.ts @@ -0,0 +1,37 @@ +import { describe, expect, it } from 'vitest'; +import { FetchRunResultService } from './fetchRunResult'; +import { DummyMainMatchRepository } from '../adaptor/dummy/mainMatchRepository'; +import { DummyPreMatchRepository } from '../adaptor/dummy/preMatchRepository'; +import { testRankingMainMatchData, testRankingPreMatchData } from '../../testData/match'; +import { MainMatchID } from '../model/main'; +import { PreMatchID } from '../model/pre'; +import { Result } from '@mikuroxina/mini-fn'; + +describe('FetchRunResultService', () => { + const dummyMainMatchRepository = new DummyMainMatchRepository(testRankingMainMatchData); + const dummyPreMatchRepository = new DummyPreMatchRepository(testRankingPreMatchData); + const fetchRunResultService = new FetchRunResultService( + dummyMainMatchRepository, + dummyPreMatchRepository + ); + it('MainMatchの走行結果が取得できる', async () => { + const res = await fetchRunResultService.handle('main', '900' as MainMatchID); + expect(Result.isErr(res)).toBe(false); + const mainMatch = Result.unwrap(res); + expect(mainMatch).toStrictEqual(testRankingMainMatchData[0].getRunResults()); + }); + it('PreMatchの走行結果が取得できる', async () => { + const res = await fetchRunResultService.handle('pre', '100' as PreMatchID); + expect(Result.isErr(res)).toBe(false); + const preMatch = Result.unwrap(res); + expect(preMatch).toStrictEqual(testRankingPreMatchData[0].getRunResults()); + }); + it('MainMatchの走行結果が無ければエラーを返す', async () => { + const res = await fetchRunResultService.handle('main', '0' as MainMatchID); + expect(Result.isErr(res)).toBe(true); + }); + it('PreMatchの走行結果が無ければエラーを返す', async () => { + const res = await fetchRunResultService.handle('pre', '0' as PreMatchID); + expect(Result.isErr(res)).toBe(true); + }); +}); diff --git a/packages/kcms/src/match/service/fetchRunResult.ts b/packages/kcms/src/match/service/fetchRunResult.ts new file mode 100644 index 00000000..f2c7984d --- /dev/null +++ b/packages/kcms/src/match/service/fetchRunResult.ts @@ -0,0 +1,33 @@ +import { MatchType } from 'config'; +import { RunResult } from '../model/runResult'; +import { MainMatchID } from '../model/main'; +import { PreMatchID } from '../model/pre'; +import { Option, Result } from '@mikuroxina/mini-fn'; +import { MainMatchRepository, PreMatchRepository } from '../model/repository'; + +export class FetchRunResultService { + constructor( + private readonly mainMatchRepository: MainMatchRepository, + private readonly preMatchRepository: PreMatchRepository + ) {} + async handle( + matchType: MatchType, + matchID: MainMatchID | PreMatchID + ): Promise> { + if (matchType === 'main') { + const mainMatchRes = await this.mainMatchRepository.findByID(matchID as MainMatchID); + if (Option.isNone(mainMatchRes)) { + return Result.err(new Error('MainMatch not found')); + } + const mainMatch = Option.unwrap(mainMatchRes); + return Result.ok(mainMatch.getRunResults()); + } else { + const preMatchRes = await this.preMatchRepository.findByID(matchID as PreMatchID); + if (Option.isNone(preMatchRes)) { + return Result.err(new Error('PreMatch not found')); + } + const preMatch = Option.unwrap(preMatchRes); + return Result.ok(preMatch.getRunResults()); + } + } +} diff --git a/packages/kcms/src/testData/match.ts b/packages/kcms/src/testData/match.ts index b1b041ef..442cfd4a 100644 --- a/packages/kcms/src/testData/match.ts +++ b/packages/kcms/src/testData/match.ts @@ -1,6 +1,7 @@ import { TeamID } from '../team/models/team.js'; import { RunResult, RunResultID } from '../match/model/runResult.js'; import { PreMatch, PreMatchID } from '../match/model/pre.js'; // ToDo: もっとデータ数を増やす +import { MainMatch, MainMatchID } from '../match/model/main.js'; // ランキング生成用の試合データ // Openは予選がないので、予選の試合データはない @@ -234,4 +235,42 @@ export const testRankingPreMatchData = [ export const testRankingMainMatchData = [ // ToDo: L/Rを入れるとL/Rを入れ替えたデータも作れるようにしたい + MainMatch.new({ + id: '900' as MainMatchID, + courseIndex: 1, + matchIndex: 1, + teamId1: '91' as TeamID, + teamId2: '92' as TeamID, + winnerId: '91' as TeamID, + runResults: [ + RunResult.new({ + id: '80' as RunResultID, + teamId: '91' as TeamID, + points: 12, + goalTimeSeconds: 60, + finishState: 'GOAL', + }), + RunResult.new({ + id: '81' as RunResultID, + teamId: '92' as TeamID, + points: 10, + goalTimeSeconds: Infinity, + finishState: 'FINISHED', + }), + RunResult.new({ + id: '82' as RunResultID, + teamId: '91' as TeamID, + points: 12, + goalTimeSeconds: Infinity, + finishState: 'FINISHED', + }), + RunResult.new({ + id: '83' as RunResultID, + teamId: '92' as TeamID, + points: 1, + goalTimeSeconds: 80, + finishState: 'GOAL', + }), + ], + }), ];