Skip to content

Commit

Permalink
Merge branch 'main' into feat/522-generate-main-match-button
Browse files Browse the repository at this point in the history
  • Loading branch information
tufusa authored Oct 20, 2024
2 parents b0e9d0d + fc825ff commit 7e69b02
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/kcms/src/match/service/generateMain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Result } from '@mikuroxina/mini-fn';
import { describe, expect, it } from 'vitest';
import { SnowflakeIDGenerator } from '../../id/main';
import { TeamID } from '../../team/models/team';
import { DummyMainMatchRepository } from '../adaptor/dummy/mainMatchRepository';
import { GenerateMainMatchService } from './generateMain';

describe('GenerateMainMatchService', () => {
const idGenerator = new SnowflakeIDGenerator(1, () =>
BigInt(new Date('2024/01/01 00:00:00 UTC').getTime())
);
const mainMatchRepository = new DummyMainMatchRepository([]);
const service = new GenerateMainMatchService(mainMatchRepository, idGenerator);

it('本戦試合を生成できる', async () => {
const res = await service.handle('1' as TeamID, '2' as TeamID);
expect(Result.isOk(res)).toBe(true);

const match = Result.unwrap(res);
expect(match.getTeamId1()).toBe('1');
expect(match.getTeamId2()).toBe('2');
});

it('(安来用) 本戦試合の試合番号は1-1になる', async () => {
const res = await service.handle('1' as TeamID, '2' as TeamID);
expect(Result.isOk(res)).toBe(true);

const match = Result.unwrap(res);
expect(match.getMatchIndex()).toBe(1);
expect(match.getCourseIndex()).toBe(1);
});
});
36 changes: 36 additions & 0 deletions packages/kcms/src/match/service/generateMain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Result } from '@mikuroxina/mini-fn';
import { SnowflakeIDGenerator } from '../../id/main';
import { TeamID } from '../../team/models/team';
import { MainMatch } from '../model/main';
import { MainMatchRepository } from '../model/repository';

export class GenerateMainMatchService {
constructor(
private readonly mainMatchRepository: MainMatchRepository,
private readonly idGenerator: SnowflakeIDGenerator
) {}

async handle(teamID1: TeamID, teamID2: TeamID): Promise<Result.Result<Error, MainMatch>> {
const newIDRes = this.idGenerator.generate<MainMatch>();
if (Result.isErr(newIDRes)) {
return newIDRes;
}
const newID = Result.unwrap(newIDRes);

const match = MainMatch.new({
id: newID,
courseIndex: 1,
matchIndex: 1,
runResults: [],
teamId1: teamID1,
teamId2: teamID2,
});

const matches = await this.mainMatchRepository.create(match);
if (Result.isErr(matches)) {
return matches;
}

return Result.ok(match);
}
}

0 comments on commit 7e69b02

Please sign in to comment.