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: エントリーができるように(Controller/Handlerの実装) #457

Merged
merged 2 commits into from
Oct 10, 2024
Merged
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
30 changes: 16 additions & 14 deletions packages/kcms/src/team/adaptor/controller/controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { z } from '@hono/zod-openapi';
import { Result } from '@mikuroxina/mini-fn';
import { SnowflakeIDGenerator } from '../../../id/main';
import { TeamRepository } from '../../models/repository';
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 {
GetTeamResponseSchema,
Expand All @@ -14,18 +13,12 @@ import {
} from '../validator/team';

export class TeamController {
private readonly createTeam: CreateTeamService;
private readonly findTeam: FetchTeamService;
private readonly deleteTeam: DeleteTeamService;

constructor(repository: TeamRepository) {
this.createTeam = new CreateTeamService(
repository,
new SnowflakeIDGenerator(1, () => BigInt(new Date().getTime()))
);
this.findTeam = new FetchTeamService(repository);
this.deleteTeam = new DeleteTeamService(repository);
}
constructor(
private readonly createTeam: CreateTeamService,
private readonly findTeam: FetchTeamService,
private readonly deleteTeam: DeleteTeamService,
private readonly entry: EntryService
) {}

async create(
args: z.infer<typeof PostTeamsRequestSchema>
Expand Down Expand Up @@ -84,6 +77,7 @@ export class TeamController {
}),
});
}

async getByID(id: TeamID): Promise<Result.Result<Error, z.infer<typeof GetTeamResponseSchema>>> {
const res = await this.findTeam.findByID(id);
if (Result.isErr(res)) {
Expand Down Expand Up @@ -111,4 +105,12 @@ export class TeamController {

return Result.ok(undefined);
}

async enter(id: TeamID): Promise<Result.Result<Error, void>> {
const res = await this.entry.enter(id);
if (Result.isErr(res)) {
return Result.err(res[1]);
}
return Result.ok(undefined);
}
}
47 changes: 45 additions & 2 deletions packages/kcms/src/team/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import { OpenAPIHono } from '@hono/zod-openapi';

import {
DeleteTeamRoute,
GetTeamRoute,
GetTeamsRoute,
PostEntryTeamRoute,
PostTeamsRoute,
} from './routing';

import { Result } from '@mikuroxina/mini-fn';
import { prismaClient } from '../adaptor';
import { TeamController } from './adaptor/controller/controller';
import { errorToCode } from './adaptor/errors';
import { DummyRepository } from './adaptor/repository/dummyRepository';
import { PrismaTeamRepository } from './adaptor/repository/prismaRepository';
import { TeamID } from './models/team.js';
import { DeleteTeamRoute, GetTeamRoute, GetTeamsRoute, PostTeamsRoute } from './routing';

import { SnowflakeIDGenerator } from '../id/main';
import { CreateTeamService } from './service/createTeam';
import { DeleteTeamService } from './service/delete';
import { EntryService } from './service/entry';
import { FetchTeamService } from './service/get';

export const teamHandler = new OpenAPIHono();
const isProduction = process.env.NODE_ENV === 'production';
const teamRepository = isProduction
? new PrismaTeamRepository(prismaClient)
: new DummyRepository();
const idGenerator = new SnowflakeIDGenerator(1, () => BigInt(new Date().getTime()));

const fetchTeamService = new FetchTeamService(teamRepository);
const createTeamService = new CreateTeamService(teamRepository, idGenerator);
const deleteTeamService = new DeleteTeamService(teamRepository);
const entryService = new EntryService(teamRepository);

export const controller = new TeamController(
isProduction ? new PrismaTeamRepository(prismaClient) : new DummyRepository()
createTeamService,
fetchTeamService,
deleteTeamService,
entryService
);

/**
Expand All @@ -25,6 +52,7 @@ teamHandler.openapi(GetTeamsRoute, async (c) => {

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

/**
* すべてのチームを返す (POST /team)
*/
Expand All @@ -38,6 +66,7 @@ teamHandler.openapi(PostTeamsRoute, async (c) => {
const teams = Result.unwrap(res);
return c.json(teams, 200);
});

/**
* 指定されたIDのチームを返す (GET /team/{teamID})
*/
Expand All @@ -50,6 +79,7 @@ teamHandler.openapi(GetTeamRoute, async (c) => {
const team = Result.unwrap(res);
return c.json(team, 200);
});

/**
* 指定されたIDのチームを削除する (DELETE /team/{teamID})
*/
Expand All @@ -61,3 +91,16 @@ teamHandler.openapi(DeleteTeamRoute, async (c) => {
}
return new Response(null, { status: 204 });
});

/**
* エントリーする (POST /team/{teamID}/entry)
*/
teamHandler.openapi(PostEntryTeamRoute, async (c) => {
const { teamID } = c.req.valid('param');
const res = await controller.enter(teamID as TeamID);
if (Result.isErr(res)) {
return c.json({ description: errorToCode(Result.unwrapErr(res)) }, 400);
}

return new Response(null, { status: 200 });
});