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

chore:チーム取得サービスを統合する #646

Merged
merged 7 commits into from
Oct 31, 2024
2 changes: 1 addition & 1 deletion packages/kcms/src/match/adaptor/controller/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from '@hono/zod-openapi';
import { Result } from '@mikuroxina/mini-fn';
import { DepartmentType, MatchType } from 'config';
import { Team, TeamID } from '../../../team/models/team';
import { FetchTeamService } from '../../../team/service/get';
import { FetchTeamService } from '../../../team/service/fetchTeam';
import { MainMatch, MainMatchID } from '../../model/main';
import { PreMatch, PreMatchID } from '../../model/pre';
import { FetchRunResultService } from '../../service/fetchRunResult';
Expand Down
2 changes: 1 addition & 1 deletion packages/kcms/src/match/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { errorToCode } from '../team/adaptor/errors';
import { DummyRepository } from '../team/adaptor/repository/dummyRepository';
import { PrismaTeamRepository } from '../team/adaptor/repository/prismaRepository';
import { TeamID } from '../team/models/team';
import { FetchTeamService } from '../team/service/get';
import { FetchTeamService } from '../team/service/fetchTeam';
import { MatchController } from './adaptor/controller/match';
import { DummyMainMatchRepository } from './adaptor/dummy/mainMatchRepository';
import { DummyPreMatchRepository } from './adaptor/dummy/preMatchRepository';
Expand Down
2 changes: 1 addition & 1 deletion packages/kcms/src/match/service/generatePre.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, expect, it } from 'vitest';
import { SnowflakeIDGenerator } from '../../id/main';
import { DummyRepository } from '../../team/adaptor/repository/dummyRepository';
import { TeamID } from '../../team/models/team';
import { FetchTeamService } from '../../team/service/get';
import { FetchTeamService } from '../../team/service/fetchTeam';
import { testTeamData } from '../../testData/entry';
import { DummyPreMatchRepository } from '../adaptor/dummy/preMatchRepository';
import { GeneratePreMatchService } from './generatePre';
Expand Down
2 changes: 1 addition & 1 deletion packages/kcms/src/match/service/generatePre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Result } from '@mikuroxina/mini-fn';
import { config, DepartmentType } from 'config';
import { SnowflakeIDGenerator } from '../../id/main';
import { Team } from '../../team/models/team';
import { FetchTeamService } from '../../team/service/get';
import { FetchTeamService } from '../../team/service/fetchTeam';
import { PreMatch } from '../model/pre';
import { PreMatchRepository } from '../model/repository';

Expand Down
2 changes: 1 addition & 1 deletion packages/kcms/src/team/adaptor/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TeamID } from '../../models/team';
import { CreateTeamService } from '../../service/createTeam';
import { DeleteTeamService } from '../../service/delete';
import { EntryService } from '../../service/entry';
import { FetchTeamService } from '../../service/get';
import { FetchTeamService } from '../../service/fetchTeam';
import {
GetTeamResponseSchema,
GetTeamsResponseSchema,
Expand Down
2 changes: 1 addition & 1 deletion packages/kcms/src/team/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { GetMatchService } from '../match/service/get';
import { CreateTeamService } from './service/createTeam';
import { DeleteTeamService } from './service/delete';
import { EntryService } from './service/entry';
import { FetchTeamService } from './service/get';
import { FetchTeamService } from './service/fetchTeam';

export const teamHandler = new OpenAPIHono();
const isProduction = process.env.NODE_ENV === 'production';
Expand Down
60 changes: 40 additions & 20 deletions packages/kcms/src/team/service/fetchTeam.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
import { Result } from '@mikuroxina/mini-fn';
import { describe, expect, it } from 'vitest';
import { DummyRepository } from '../adaptor/repository/dummyRepository';
import { Team, TeamID } from '../models/team';
import { FetchTeamService } from './fetchTeam';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { TestEntryData } from '../../testData/entry.js';
import { DummyRepository } from '../adaptor/repository/dummyRepository.js';
import { TeamID } from '../models/team.js';
import { FetchTeamService } from './fetchTeam.js';

describe('FetchTeamService', () => {
const teamData = Team.new({
id: '7549586' as TeamID,
teamName: 'かに2',
members: ['メンバー3'],
robotType: 'wheel',
departmentType: 'elementary',
});
const repository = new DummyRepository([teamData]);
const repository = new DummyRepository();
const service = new FetchTeamService(repository);

it('チーム取得ができる', async () => {
const teamRes = await service.handle('7549586' as TeamID);
expect(Result.isErr(teamRes)).toBe(false);
const team = Result.unwrap(teamRes);
expect(team).toStrictEqual(teamData);
const testEntryData = [TestEntryData['ElementaryMultiWalk'], TestEntryData['ElementaryWheel']];

beforeEach(() => {
testEntryData.map((d) => repository.create(d));
});
afterEach(() => {
repository.reset();
});

it('すべて取得できる', async () => {
const actual = await service.findAll();

expect(Result.isOk(actual)).toBe(true);
expect(actual[1]).toStrictEqual(testEntryData);
});

it('チームがない場合はエラーを返す', async () => {
const teamRes = await service.handle('0' as TeamID);
expect(Result.isErr(teamRes)).toBe(true);
it('チーム名で取得できる', async () => {
const actual = await service.findByTeamName(testEntryData[0].getTeamName());
expect(Result.isOk(actual)).toBe(true);
expect(actual[1]).toStrictEqual(testEntryData[0]);
});

it('チームIDで取得できる', async () => {
const actual = await service.findByID(testEntryData[0].getId());
expect(Result.isOk(actual)).toBe(true);
expect(actual[1]).toStrictEqual(testEntryData[0]);
});

it('存在しないときはエラーを返す', async () => {
const actual = await service.findByID('0' as TeamID);
expect(Result.isErr(actual)).toBe(true);
expect(actual[1]).toStrictEqual(new Error('Not found'));

const actual2 = await service.findByTeamName('team0');
expect(Result.isErr(actual2)).toBe(true);
expect(actual2[1]).toStrictEqual(new Error('Not found'));
});
});
28 changes: 21 additions & 7 deletions packages/kcms/src/team/service/fetchTeam.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Option, Result } from '@mikuroxina/mini-fn';
import { TeamRepository } from '../models/repository';
import { Team, TeamID } from '../models/team';
import { TeamRepository } from '../models/repository.js';
import { Team, TeamID } from '../models/team.js';

export class FetchTeamService {
private readonly repository: TeamRepository;
Expand All @@ -9,11 +9,25 @@ export class FetchTeamService {
this.repository = repository;
}

async handle(teamID: TeamID): Promise<Result.Result<Error, Team>> {
const teamRes = await this.repository.findByID(teamID);
if (Option.isNone(teamRes)) {
return Result.err(new Error('Team not found'));
async findAll(): Promise<Result.Result<Error, Team[]>> {
return await this.repository.findAll();
}

async findByID(id: TeamID): Promise<Result.Result<Error, Team>> {
const res = await this.repository.findByID(id);
if (Option.isNone(res)) {
return Result.err(new Error('Not found'));
}
return Result.ok(Option.unwrap(teamRes));

return Result.ok(Option.unwrap(res));
}

async findByTeamName(name: string): Promise<Result.Result<Error, Team>> {
const res = await this.repository.findByTeamName(name);
if (Option.isNone(res)) {
return Result.err(new Error('Not found'));
}

return Result.ok(Option.unwrap(res));
}
}
49 changes: 0 additions & 49 deletions packages/kcms/src/team/service/get.test.ts

This file was deleted.

33 changes: 0 additions & 33 deletions packages/kcms/src/team/service/get.ts

This file was deleted.