Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
refactor: ペアをオブジェクトに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
laminne committed Dec 25, 2023
1 parent 6cb4bce commit 342aded
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 190 deletions.
66 changes: 28 additions & 38 deletions src/match/adaptor/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Match, MatchPoints, MatchTeams } from "../match.js";
import { Match, MatchTeams } from "../match.js";
import { MatchRepository } from "../service/repository.js";
import { Option, Result } from "@mikuroxina/mini-fn";
import { readFile, writeFile } from "node:fs/promises";
Expand All @@ -10,19 +10,31 @@ interface JSONData {
entry: Array<object>;
}

interface matchResultJSON {
teamID: string;
points: number;
time: number;
}

interface matchResultPairJSON {
Left: matchResultJSON;
Right: matchResultJSON;
}

interface matchResultFinalPairJSON {
results: [matchResultPairJSON, matchResultPairJSON];
winnerID: string;
}

interface MatchJSON {
id: string;
matchType: string;
courseIndex: number;
teams: [EntryJSON | undefined, EntryJSON | undefined];
points?: [MatchPointsJSON, MatchPointsJSON];
time?: [number, number];
winnerID?: string;
}

interface MatchPointsJSON {
teamID: string;
points: number;
teams: {
Left: EntryJSON | undefined;
Right: EntryJSON | undefined;
};
results?: matchResultPairJSON | matchResultFinalPairJSON;
}

export class JSONMatchRepository implements MatchRepository {
Expand Down Expand Up @@ -91,36 +103,16 @@ export class JSONMatchRepository implements MatchRepository {
category: entry.category,
};
};
const convertToMatchPointJSON = (points: MatchPoints) => {
return {
teamID: points.teamID,
points: points.points,
};
};
const convertToMatchPointsJSON = (
points: [MatchPoints, MatchPoints] | undefined,
): [MatchPointsJSON, MatchPointsJSON] | undefined => {
if (!points) {
return points;
}
return [
convertToMatchPointJSON(points[0]),
convertToMatchPointJSON(points[1]),
];
};

return {
id: match.id,
teams: [
covertToEntryJSON(match.teams[0]),
covertToEntryJSON(match.teams[1]),
],
teams: {
Left: covertToEntryJSON(match.teams.Left),
Right: covertToEntryJSON(match.teams.Right),
},
matchType: match.matchType,
courseIndex: match.courseIndex,
// ToDo: MatchPointsのパースをする
points: convertToMatchPointsJSON(match.points),
time: match.time,
winnerID: match.winnerID,
results: match.results,
};
}

Expand All @@ -131,9 +123,7 @@ export class JSONMatchRepository implements MatchRepository {
matchType: json.matchType as "primary" | "final",
// ToDo: MatchPointsのパース
courseIndex: json.courseIndex,
points: json.points,
time: json.time,
winnerID: json.winnerID,
results: json.results,
});
}
}
55 changes: 32 additions & 23 deletions src/match/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { GenerateMatchService } from "./service/generate.js";
import { Result } from "@mikuroxina/mini-fn";
import { Match } from "./match.js";
import { EditMatchService } from "./service/edit.js";
import { Entry } from "../entry/entry.js";

export class MatchController {
private readonly matchService: GenerateMatchService;
Expand Down Expand Up @@ -42,37 +43,45 @@ export class MatchController {
}

private toJSON(i: Match) {
const toTeamsJSON = (i: Match) =>
i.teams.map((j) => {
if (!j) {
return undefined;
}
const toTeamJSON = (i?: Entry) => {
if (!i) {
return i;
}

return {
id: i.id,
teamName: i.teamName,
isMultiWalk: i.isMultiWalk,
category: i.category,
};
};

return {
id: j.id,
teamName: j.teamName,
isMultiWalk: j.isMultiWalk,
category: j.category,
};
});
// winnerIDなどがundefinedになる
return {
id: i.id,
teams: toTeamsJSON(i),
teams: {
Left: toTeamJSON(i.teams.Left),
Right: toTeamJSON(i.teams.Right),
},
matchType: i.matchType,
courseIndex: i.courseIndex,
points: i.points,
time: i.time,
winnerID: i.winnerID,
results: i.results,
};
}
}

interface matchResultJSON {
teamID: string;
points: number;
time: number;
}
interface matchResultPairJSON {
Left: matchResultJSON;
Right: matchResultJSON;
}
interface matchResultFinalPairJSON {
results: [matchResultPairJSON, matchResultPairJSON];
winnerID: string;
}
interface matchUpdateJSON {
points?: [
{ teamID: string; points: number },
{ teamID: string; points: number },
];
time?: [number, number];
winnerID?: string;
results: matchResultPairJSON | matchResultFinalPairJSON;
}
9 changes: 7 additions & 2 deletions src/match/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { MatchController } from "./controller.js";
import { Result } from "@mikuroxina/mini-fn";
import { EditMatchService } from "./service/edit.js";
import { ReconstructMatchArgs } from "./match.js";

export const matchHandler = new Hono();
const repository = await JSONMatchRepository.new();
const entryRepository = await JSONEntryRepository.new();
Expand All @@ -25,9 +26,13 @@ matchHandler.post("/:match", async (c) => {

matchHandler.put("/:match", async (c) => {
const { match } = c.req.param();
const req = (await c.req.json()) as Partial<
Pick<ReconstructMatchArgs, "points" | "time" | "winnerID">
const req = (await c.req.json()) as Required<
Pick<ReconstructMatchArgs, "results">
>;
if (!req.results) {
return c.json([{ error: "results is required" }], 400);
}

const res = await controller.editMatch(match, req);
if (Result.isErr(res)) {
return c.json([{ error: res[1].message }]);
Expand Down
16 changes: 7 additions & 9 deletions src/match/match.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from "vitest";
import { describe, expect, it } from "vitest";
import { Match } from "./match.js";
import { Entry } from "../entry/entry.js";

Expand All @@ -22,15 +22,14 @@ describe("正しくインスタンスを生成できる", () => {
it("試合相手が居るとき", () => {
const actual = Match.new({
id: "999",
teams: [entry1, entry2],
teams: { Left: entry1, Right: entry2 },
matchType: "primary",
courseIndex: 0,
});

expect(actual.id).toBe("999");
expect(actual.teams).toEqual([entry1, entry2]);
expect(actual.points).toBeUndefined();
expect(actual.winnerID).toBeUndefined();
expect(actual.teams).toEqual({ Left: entry1, Right: entry2 });
expect(actual.results).toBeUndefined();
expect(actual.matchType).toBe("primary");
expect(actual.courseIndex).toBe(0);
expect(actual.time).toBeUndefined();
Expand All @@ -39,15 +38,14 @@ describe("正しくインスタンスを生成できる", () => {
it("試合相手が居ないとき", () => {
const actual = Match.new({
id: "999",
teams: [entry1, undefined],
teams: { Left: entry1, Right: undefined },
matchType: "primary",
courseIndex: 0,
});

expect(actual.id).toBe("999");
expect(actual.teams).toEqual([entry1, undefined]);
expect(actual.points).toBeUndefined();
expect(actual.winnerID).toBeUndefined();
expect(actual.teams).toEqual({ Left: entry1, Right: undefined });
expect(actual.results).toBeUndefined();
expect(actual.matchType).toBe("primary");
expect(actual.courseIndex).toBe(0);
expect(actual.time).toBeUndefined();
Expand Down
Loading

0 comments on commit 342aded

Please sign in to comment.