Skip to content

Commit

Permalink
feat: JSONRepositoryを削除、repositoryに情報更新メソッドを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
laminne committed Sep 7, 2024
1 parent f273ec4 commit 4baf8a6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 124 deletions.
43 changes: 29 additions & 14 deletions packages/kcms/src/team/adaptor/dummyRepository.ts
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]));
}
}
108 changes: 0 additions & 108 deletions packages/kcms/src/team/adaptor/json.ts

This file was deleted.

47 changes: 45 additions & 2 deletions packages/kcms/src/team/models/repository.ts
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>>;
}

0 comments on commit 4baf8a6

Please sign in to comment.