Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 本戦試合生成Serviceの実装とテスト #510

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 team = Result.unwrap(res);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const team = Result.unwrap(res);
const match = Result.unwrap(res);

expect(team.getTeamId1()).toBe('1');
expect(team.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 team = Result.unwrap(res);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const team = Result.unwrap(res);
const match = Result.unwrap(res);

expect(team.getMatchIndex()).toBe(1);
expect(team.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);
}
}