-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: JSONRepositoryを削除、repositoryに情報更新メソッドを追加
- Loading branch information
Showing
3 changed files
with
74 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,59 @@ | ||
import { TeamRepository } from '../models/repository.js'; | ||
import { Option, Result } from '@mikuroxina/mini-fn'; | ||
import { Team } from '../models/team.js'; | ||
import { Team, TeamID } from '../models/team.js'; | ||
|
||
export class DummyRepository implements TeamRepository { | ||
private data: Array<Team>; | ||
constructor(data?: Array<Team>) { | ||
this.data = data ?? []; | ||
private data: Map<TeamID, Team>; | ||
|
||
constructor(data: Team[] = []) { | ||
this.data = new Map<TeamID, Team>(data.map((v) => [v.getId(), v])); | ||
} | ||
|
||
async create(entry: Team): Promise<Result.Result<Error, Team>> { | ||
this.data.push(entry); | ||
const res = this.data.set(entry.getId(), entry); | ||
if (!res) { | ||
return Result.err(new Error('Team not found')); | ||
} | ||
|
||
return Result.ok(entry); | ||
} | ||
|
||
async findByTeamName(name: string): Promise<Option.Option<Team>> { | ||
const entry = this.data.find((e) => e.getTeamName() === name); | ||
if (entry === undefined) { | ||
const entry = [...this.data].map((v) => v[1]).find((e) => e.getTeamName() === name); | ||
if (!entry) { | ||
return Option.none(); | ||
} | ||
return Option.some(entry); | ||
} | ||
|
||
async findByID(id: string): Promise<Option.Option<Team>> { | ||
const entry = this.data.find((e) => e.getId() === id); | ||
const entry = [...this.data].map((v) => v[1]).find((e) => e.getId() === id); | ||
if (entry === undefined) { | ||
return Option.none(); | ||
} | ||
return Option.some(entry); | ||
} | ||
|
||
async findAll(): Promise<Result.Result<Error, Array<Team>>> { | ||
return Result.ok(this.data); | ||
async findAll(): Promise<Result.Result<Error, Team[]>> { | ||
return Result.ok([...this.data].map((v) => v[1])); | ||
} | ||
|
||
async delete(id: string): Promise<Option.Option<Error>> { | ||
this.data = this.data.filter((e) => e.getId() !== id); | ||
async delete(id: TeamID): Promise<Option.Option<Error>> { | ||
this.data.delete(id); | ||
return Option.none(); | ||
} | ||
|
||
reset() { | ||
this.data = []; | ||
async update(team: Team): Promise<Result.Result<Error, Team>> { | ||
const res = this.data.get(team.getId()); | ||
if (!res) { | ||
return Result.err(new Error('Team not found')); | ||
} | ||
|
||
this.data.set(team.getId(), team); | ||
return Result.ok(team); | ||
} | ||
|
||
reset(data: Team[] = []) { | ||
this.data = new Map<TeamID, Team>(data.map((v) => [v.getId(), v])); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,53 @@ | ||
import { Team } from './team.js'; | ||
import { Option, Result } from '@mikuroxina/mini-fn'; | ||
|
||
/** | ||
* チームを管理するRepository | ||
*/ | ||
export interface TeamRepository { | ||
create(entry: Team): Promise<Result.Result<Error, Team>>; | ||
/** | ||
* @description チームを作成(参加登録)する | ||
* @param team 作成するチームのデータ | ||
* @return 成功時: Team, 失敗時: Error | ||
*/ | ||
create(team: Team): Promise<Result.Result<Error, Team>>; | ||
|
||
/** | ||
* @description チーム名でチームを検索 | ||
* @param name チーム名 | ||
* @return 成功時: Team, 失敗時(見つからない時も含む): Option.none() | ||
* ToDo: Result.Result<Error, Team> で置き換える | ||
*/ | ||
findByTeamName(name: string): Promise<Option.Option<Team>>; | ||
findAll(): Promise<Result.Result<Error, Array<Team>>>; | ||
|
||
/** | ||
* @description 全てのチームを取得 | ||
* @return 成功時: Team[], 失敗時: Error | ||
* ToDo: Result.Result<Error, Team[]> で置き換える | ||
*/ | ||
findAll(): Promise<Result.Result<Error, Team[]>>; | ||
|
||
/** | ||
* @description チームIDでチームを検索 | ||
* @param id チームID | ||
* @return 成功時: Team, 失敗時(見つからない時も含む): Option.none() | ||
* ToDo: Result.Result<Error, Team> で置き換える | ||
*/ | ||
findByID(id: string): Promise<Option.Option<Team>>; | ||
|
||
/** | ||
* @description チームを削除(登録解除)する | ||
* @param id 削除するチームのID | ||
* @return 成功時: Option.none(), 失敗時: Error | ||
*/ | ||
delete(id: string): Promise<Option.Option<Error>>; | ||
|
||
/** | ||
* @description | ||
* チームを更新:\ | ||
* *与えられたデータで上書きします。* | ||
* @param team 更新するチームのデータ | ||
* @return 成功時: Team, 失敗時: Error | ||
*/ | ||
update(team: Team): Promise<Result.Result<Error, Team>>; | ||
} |