Skip to content

Commit

Permalink
refactor: モデルのゲッター/セッターを関数に置き換え (#77)
Browse files Browse the repository at this point in the history
* refactor: `Match`モデルのメソッドでゲッター/セッターを使わないように

* refactor: entryモデルでゲッター/セッターを使わないように

* chore: fix code styles
  • Loading branch information
laminne authored Jul 6, 2024
1 parent a6d5179 commit 0936d9f
Show file tree
Hide file tree
Showing 22 changed files with 211 additions and 157 deletions.
6 changes: 3 additions & 3 deletions packages/kcms/src/entry/adaptor/dummyRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export class DummyRepository implements EntryRepository {
}

async findByTeamName(name: string): Promise<Option.Option<Entry>> {
const entry = this.data.find((e) => e.teamName === name);
const entry = this.data.find((e) => e.getTeamName() === name);
if (entry === undefined) {
return Option.none();
}
return Option.some(entry);
}

async findByID(id: string): Promise<Option.Option<Entry>> {
const entry = this.data.find((e) => e.id === id);
const entry = this.data.find((e) => e.getId() === id);
if (entry === undefined) {
return Option.none();
}
Expand All @@ -34,7 +34,7 @@ export class DummyRepository implements EntryRepository {
}

async delete(id: string): Promise<Option.Option<Error>> {
this.data = this.data.filter((e) => e.id !== id);
this.data = this.data.filter((e) => e.getId() !== id);
return Option.none();
}

Expand Down
22 changes: 10 additions & 12 deletions packages/kcms/src/entry/adaptor/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ export class JSONEntryRepository implements EntryRepository {
}

async findByTeamName(name: string): Promise<Option.Option<Entry>> {
const entry = this.data.find((e) => e.teamName === name);
const entry = this.data.find((e) => e.getTeamName() === name);
if (entry === undefined) {
return Option.none();
}
return Option.some(entry);
}

async findByID(id: string): Promise<Option.Option<Entry>> {
const entry = this.data.find((e) => e.id === id);
const entry = this.data.find((e) => e.getId() === id);
if (entry === undefined) {
return Option.none();
}
Expand All @@ -58,7 +58,7 @@ export class JSONEntryRepository implements EntryRepository {
}

async delete(id: string): Promise<Option.Option<Error>> {
this.data = this.data.filter((e) => e.id !== id);
this.data = this.data.filter((e) => e.getId() !== id);
await this.save();
return Option.none();
}
Expand All @@ -73,14 +73,12 @@ export class JSONEntryRepository implements EntryRepository {

private static async load(): Promise<JSONData> {
const data = await readFile('./data.json', 'utf-8');
const parsed = JSON.parse(data) as JSONData;
parsed.entry = parsed.entry.map((e) => JSONEntryRepository.jsonToEntry(e));
return parsed;
return JSON.parse(data) as JSONData;
}

private isExists(entry: Entry): boolean {
for (const v of this.data) {
if (v.teamName === entry.teamName || v.id === entry.id) {
if (v.getTeamName === entry.getTeamName || v.getId === entry.getId) {
console.error('Entry already exists');
return true;
}
Expand All @@ -90,11 +88,11 @@ export class JSONEntryRepository implements EntryRepository {

private static entryToJSON(entry: Entry): EntryJSON {
return {
id: entry.id,
teamName: entry.teamName,
members: entry.members,
isMultiWalk: entry.isMultiWalk,
category: entry.category,
id: entry.getId(),
teamName: entry.getTeamName(),
members: entry.getMembers(),
isMultiWalk: entry.getIsMultiWalk(),
category: entry.getCategory(),
};
}

Expand Down
9 changes: 8 additions & 1 deletion packages/kcms/src/entry/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ export class Controller {
return Result.err(res[1]);
}

return Result.ok(res[1]);
const unwrapped = Result.unwrap(res);
return Result.ok({
id: unwrapped.getId(),
teamName: unwrapped.getTeamName(),
members: unwrapped.getMembers(),
isMultiWalk: unwrapped.getIsMultiWalk(),
category: unwrapped.getCategory(),
});
}

async get(): Promise<Result.Result<Error, baseEntry[]>> {
Expand Down
10 changes: 5 additions & 5 deletions packages/kcms/src/entry/entry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ describe('正しくインスタンスを生成できる', () => {
category: 'Open',
});

expect(actual.id).toBe('123');
expect(actual.teamName).toBe('チーム1');
expect(actual.members).toEqual(['山田太郎', 'テスト大介']);
expect(actual.isMultiWalk).toBe(false);
expect(actual.category).toBe('Open');
expect(actual.getId()).toBe('123');
expect(actual.getTeamName()).toBe('チーム1');
expect(actual.getMembers()).toEqual(['山田太郎', 'テスト大介']);
expect(actual.getIsMultiWalk()).toBe(false);
expect(actual.getCategory()).toBe('Open');
});
});
40 changes: 20 additions & 20 deletions packages/kcms/src/entry/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export interface EntryCreateArgs {
}

export class Entry {
private readonly _id: EntryID;
private readonly _teamName: string;
private readonly _members: Array<string>;
private readonly _isMultiWalk: boolean;
private readonly _category: EntryCategory;
private readonly id: EntryID;
private readonly teamName: string;
private readonly members: Array<string>;
private readonly isMultiWalk: boolean;
private readonly category: EntryCategory;

private constructor(
id: EntryID,
Expand All @@ -27,31 +27,31 @@ export class Entry {
_isMultiWalk: boolean,
category: EntryCategory
) {
this._id = id;
this._teamName = teamName;
this._members = _members;
this._isMultiWalk = _isMultiWalk;
this._category = category;
this.id = id;
this.teamName = teamName;
this.members = _members;
this.isMultiWalk = _isMultiWalk;
this.category = category;
}

get id(): EntryID {
return this._id;
getId(): EntryID {
return this.id;
}

get teamName(): string {
return this._teamName;
getTeamName(): string {
return this.teamName;
}

get members(): Array<string> {
return this._members;
getMembers(): Array<string> {
return this.members;
}

get isMultiWalk(): boolean {
return this._isMultiWalk;
getIsMultiWalk(): boolean {
return this.isMultiWalk;
}

get category(): EntryCategory {
return this._category;
getCategory(): EntryCategory {
return this.category;
}

public static new(arg: EntryCreateArgs): Entry {
Expand Down
53 changes: 43 additions & 10 deletions packages/kcms/src/entry/service/entry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,40 @@ describe('entryService', () => {
});

it('エントリーできる', async () => {
const actual = await service.create(TestEntryData['ElementaryMultiWalk']);
const data = TestEntryData['ElementaryMultiWalk'];
const actual = await service.create({
teamName: data.getTeamName(),
members: data.getMembers(),
isMultiWalk: data.getIsMultiWalk(),
category: data.getCategory(),
});

expect(Result.isOk(actual)).toBe(true);
if (Result.isErr(actual)) {
return;
}

expect(actual[1].members).toStrictEqual(['TestTaro1']);
expect(actual[1].teamName).toBe('TestTeam1');
expect(actual[1].isMultiWalk).toBe(true);
expect(actual[1].category).toBe('Elementary');
expect(actual[1].getMembers()).toStrictEqual(['TestTaro1']);
expect(actual[1].getTeamName()).toBe('TestTeam1');
expect(actual[1].getIsMultiWalk()).toBe(true);
expect(actual[1].getCategory()).toBe('Elementary');
});

it('チーム名が重複するときはエラー終了する', async () => {
await service.create(TestEntryData['ElementaryMultiWalk']);
const result = await service.create(TestEntryData['ElementaryMultiWalkExists']);
const data = TestEntryData['ElementaryMultiWalk'];
const existsData = TestEntryData['ElementaryMultiWalkExists'];
await service.create({
teamName: data.getTeamName(),
members: data.getMembers(),
isMultiWalk: data.getIsMultiWalk(),
category: data.getCategory(),
});
const result = await service.create({
teamName: existsData.getTeamName(),
members: existsData.getMembers(),
isMultiWalk: existsData.getIsMultiWalk(),
category: existsData.getCategory(),
});

expect(Result.isErr(result)).toBe(true);
expect(result[1]).toStrictEqual(new Error('teamName Exists'));
Expand All @@ -47,7 +65,12 @@ describe('entryService', () => {
isMultiWalk: true,
category: 'Open',
});
const actual = await service.create(entry);
const actual = await service.create({
teamName: entry.getTeamName(),
members: entry.getMembers(),
isMultiWalk: entry.getIsMultiWalk(),
category: entry.getCategory(),
});

expect(Result.isErr(actual)).toBe(true);
expect(actual[1]).toStrictEqual(new Error('too many members'));
Expand All @@ -61,7 +84,12 @@ describe('entryService', () => {
isMultiWalk: true,
category: 'Elementary',
});
const actual = await service.create(entry);
const actual = await service.create({
teamName: entry.getTeamName(),
members: entry.getMembers(),
isMultiWalk: entry.getIsMultiWalk(),
category: entry.getCategory(),
});

expect(Result.isErr(actual)).toBe(true);
expect(actual[1]).toStrictEqual(new Error('too many members'));
Expand All @@ -75,7 +103,12 @@ describe('entryService', () => {
isMultiWalk: true,
category: 'Elementary',
});
const actual = await service.create(entry);
const actual = await service.create({
teamName: entry.getTeamName(),
members: entry.getMembers(),
isMultiWalk: entry.getIsMultiWalk(),
category: entry.getCategory(),
});

expect(Result.isErr(actual)).toBe(true);
expect(actual[1]).toStrictEqual(new Error('no member'));
Expand Down
4 changes: 2 additions & 2 deletions packages/kcms/src/entry/service/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ describe('getEntryService', () => {
});

it('チーム名で取得できる', async () => {
const actual = await service.findByTeamName(testEntryData[0].teamName);
const actual = await service.findByTeamName(testEntryData[0].getTeamName());
expect(Result.isOk(actual)).toBe(true);
expect(actual[1]).toStrictEqual(EntryDTO.fromDomain(testEntryData[0]));
});

it('チームIDで取得できる', async () => {
const actual = await service.findByID(testEntryData[0].id);
const actual = await service.findByID(testEntryData[0].getId());
expect(Result.isOk(actual)).toBe(true);
expect(actual[1]).toStrictEqual(EntryDTO.fromDomain(testEntryData[0]));
});
Expand Down
8 changes: 7 additions & 1 deletion packages/kcms/src/entry/service/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ export class EntryDTO {
}

public static fromDomain(d: Entry): EntryDTO {
return new EntryDTO(d.id, d.teamName, d.members, d.isMultiWalk, d.category);
return new EntryDTO(
d.getId(),
d.getTeamName(),
d.getMembers(),
d.getIsMultiWalk(),
d.getCategory()
);
}

public toToDomain(): Entry {
Expand Down
6 changes: 3 additions & 3 deletions packages/kcms/src/match/adaptor/dummyRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ export class DummyMatchRepository implements MatchRepository {
}

public async findByID(id: string): Promise<Option.Option<Match>> {
const match = this.data.find((m) => m.id === id);
const match = this.data.find((m) => m.getId() === id);
if (!match) {
return Option.none();
}
return Option.some(match);
}

public async findByType(type: string): Promise<Option.Option<Match[]>> {
const match = this.data.filter((m) => m.matchType === type);
const match = this.data.filter((m) => m.getMatchType() === type);
if (!match) {
return Option.none();
}
return Option.some(match);
}

public async update(match: Match): Promise<Result.Result<Error, Match>> {
const i = this.data.findIndex((m) => m.id === match.id);
const i = this.data.findIndex((m) => m.getId() === match.getId());
this.data[i] = match;
return Result.ok(match);
}
Expand Down
28 changes: 14 additions & 14 deletions packages/kcms/src/match/adaptor/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class JSONMatchRepository implements MatchRepository {
}

public async findByID(id: string): Promise<Option.Option<Match>> {
const match = this.data.find((m) => m.id === id);
const match = this.data.find((m) => m.getId() === id);
if (!match) {
return Option.none();
}
Expand All @@ -71,7 +71,7 @@ export class JSONMatchRepository implements MatchRepository {
}

public async findByType(type: string): Promise<Option.Option<Match[]>> {
const match = this.data.filter((m) => m.matchType === type);
const match = this.data.filter((m) => m.getMatchType() === type);
if (!match) {
return Option.none();
}
Expand All @@ -83,7 +83,7 @@ export class JSONMatchRepository implements MatchRepository {
}

public async update(match: Match): Promise<Result.Result<Error, Match>> {
const i = this.data.findIndex((m) => m.id === match.id);
const i = this.data.findIndex((m) => m.getId === match.getId);
this.data[i] = match;

await this.save();
Expand Down Expand Up @@ -112,23 +112,23 @@ export class JSONMatchRepository implements MatchRepository {
}

return {
id: entry.id,
teamName: entry.teamName,
members: entry.members,
isMultiWalk: entry.isMultiWalk,
category: entry.category,
id: entry.getId(),
teamName: entry.getTeamName(),
members: entry.getMembers(),
isMultiWalk: entry.getIsMultiWalk(),
category: entry.getCategory(),
};
};

return {
id: match.id,
id: match.getId(),
teams: {
left: covertToEntryJSON(match.teams.left),
right: covertToEntryJSON(match.teams.right),
left: covertToEntryJSON(match.getTeams().left),
right: covertToEntryJSON(match.getTeams().right),
},
matchType: match.matchType,
courseIndex: match.courseIndex,
results: match.results,
matchType: match.getMatchType(),
courseIndex: match.getCourseIndex(),
results: match.getResults(),
};
}

Expand Down
Loading

0 comments on commit 0936d9f

Please sign in to comment.