-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/522-generate-main-match-button
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |