Skip to content

Commit

Permalink
Merge branch 'main' into feat/325-create-runresult-service
Browse files Browse the repository at this point in the history
  • Loading branch information
kiharu3112 authored Oct 8, 2024
2 parents 0bfb458 + a5d064a commit 5a5c7d3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
21 changes: 20 additions & 1 deletion packages/kcms/src/team/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FetchTeamService } from './service/get.js';
import { DeleteTeamService } from './service/delete.js';
import { SnowflakeIDGenerator } from '../id/main.js';
import {
GetTeamResponseSchema,
GetTeamsResponseSchema,
PostTeamsRequestSchema,
PostTeamsResponseSchema,
Expand Down Expand Up @@ -61,7 +62,7 @@ export class Controller {
);
}

async get(): Promise<Result.Result<Error, z.infer<typeof GetTeamsResponseSchema>>> {
async getAll(): Promise<Result.Result<Error, z.infer<typeof GetTeamsResponseSchema>>> {
const res = await this.findTeam.findAll();
if (Result.isErr(res)) {
return Result.err(res[1]);
Expand All @@ -83,6 +84,24 @@ export class Controller {
}),
});
}
async getByID(id: TeamID): Promise<Result.Result<Error, z.infer<typeof GetTeamResponseSchema>>> {
const res = await this.findTeam.findByID(id);
if (Result.isErr(res)) {
return Result.err(res[1]);
}
const team = Result.unwrap(res);

return Result.ok({
id: team.getId(),
name: team.getTeamName(),
members: team.getMembers() as [string, ...string[]],
clubName: team.getClubName() ?? '',
entryCode: '',
robotType: team.getRobotType(),
departmentType: team.getDepartmentType(),
isEntered: team.getIsEntered(),
});
}

async delete(id: TeamID): Promise<Result.Result<Error, void>> {
const res = await this.deleteTeam.handle(id);
Expand Down
21 changes: 18 additions & 3 deletions packages/kcms/src/team/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Controller } from './controller.js';
import { DummyRepository } from './adaptor/repository/dummyRepository';
import { OpenAPIHono } from '@hono/zod-openapi';
import { GetTeamsRoute, PostTeamsRoute } from './routing';
import { GetTeamRoute, GetTeamsRoute, PostTeamsRoute } from './routing';
import { Result } from '@mikuroxina/mini-fn';
import { errorToCode } from './adaptor/errors';
import { PrismaTeamRepository } from './adaptor/repository/prismaRepository';
import { prismaClient } from '../adaptor';
import { TeamID } from './models/team.js';

export const teamHandler = new OpenAPIHono();
const isProduction = process.env.NODE_ENV === 'production';
Expand All @@ -17,14 +18,16 @@ export const controller = new Controller(
* すべてのチームを返す (GET /team)
*/
teamHandler.openapi(GetTeamsRoute, async (c) => {
const res = await controller.get();
const res = await controller.getAll();
if (Result.isErr(res)) {
return c.json({ description: errorToCode(res[1]) }, 400);
}

return c.json(Result.unwrap(res), 200);
});

/**
* すべてのチームを返す (POST /team)
*/
teamHandler.openapi(PostTeamsRoute, async (c) => {
const request = c.req.valid('json');

Expand All @@ -35,3 +38,15 @@ teamHandler.openapi(PostTeamsRoute, async (c) => {
const teams = Result.unwrap(res);
return c.json(teams, 200);
});
/**
* 指定されたIDのチームを返す (GET /team/{teamID})
*/
teamHandler.openapi(GetTeamRoute, async (c) => {
const teamID = c.req.param('teamID');
const res = await controller.getByID(teamID as TeamID);
if (Result.isErr(res)) {
return c.json({ description: errorToCode(res[1]) }, 400);
}
const team = Result.unwrap(res);
return c.json(team, 200);
});
3 changes: 2 additions & 1 deletion packages/kcms/src/team/service/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DummyRepository } from '../adaptor/repository/dummyRepository';
import { FetchTeamService } from './get.js';
import { Result } from '@mikuroxina/mini-fn';
import { TestEntryData } from '../../testData/entry.js';
import { TeamID } from '../models/team';

describe('getEntryService', () => {
const repository = new DummyRepository();
Expand Down Expand Up @@ -41,7 +42,7 @@ describe('getEntryService', () => {
});

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

Expand Down
4 changes: 2 additions & 2 deletions packages/kcms/src/team/service/get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TeamRepository } from '../models/repository.js';
import { Team } from '../models/team.js';
import { Team, TeamID } from '../models/team.js';
import { Option, Result } from '@mikuroxina/mini-fn';

export class FetchTeamService {
Expand All @@ -13,7 +13,7 @@ export class FetchTeamService {
return await this.repository.findAll();
}

async findByID(id: string): Promise<Result.Result<Error, Team>> {
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'));
Expand Down

0 comments on commit 5a5c7d3

Please sign in to comment.