Skip to content

Commit

Permalink
ユーザーとevent, taskを関連づけ
Browse files Browse the repository at this point in the history
  • Loading branch information
yuto-trd committed Oct 26, 2024
1 parent 6a717d5 commit 7a7e615
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
21 changes: 11 additions & 10 deletions task_yell/src/lib/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,36 @@ const COLLECTION_NAME = "events";
* @param event 作成されるイベント
* @returns 競合がない場合はnull, そうでない場合はマージエディターで必要なので該当日時のイベントのリスト
*/
export async function createEvent(event: Event): Promise<Event[] | null> {
export async function createEvent(userId: string, event: Event): Promise<Event[] | null> {
// event.start ~ event.end とコンフリクトする予定があるかどうかを確認する
const events = await readEvents();
const events = await readEvents(userId);
const isConflict = events.some(
(e) => event.start <= e.end && event.end >= e.start,
);
if (isConflict) {
return events;
} else {
await createData<Event>(COLLECTION_NAME, event);
await createData<Event>(`users/${userId}/${COLLECTION_NAME}`, event);
return null;
}
}

export async function readEvents(): Promise<Event[]> {
return readData<Event>(COLLECTION_NAME);
export async function readEvents(userId: string): Promise<Event[]> {
return readData<Event>(`users/${userId}/${COLLECTION_NAME}`);
}

export async function readSingleEvent(id: string): Promise<Event | null> {
return readSingleData<Event>(COLLECTION_NAME, id);
export async function readSingleEvent(userId: string, id: string): Promise<Event | null> {
return readSingleData<Event>(`users/${userId}/${COLLECTION_NAME}`, id);
}

export async function updateEvent(
userId: string,
id: string,
eventData: Partial<Event>,
): Promise<void> {
return updateData<Event>(COLLECTION_NAME, id, eventData);
return updateData<Event>(`users/${userId}/${COLLECTION_NAME}`, id, eventData);
}

export async function deleteEvent(id: string): Promise<void> {
return deleteData(COLLECTION_NAME, id);
export async function deleteEvent(userId: string, id: string): Promise<void> {
return deleteData(`users/${userId}/${COLLECTION_NAME}`, id);
}
19 changes: 10 additions & 9 deletions task_yell/src/lib/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ const COLLECTION_NAME = "tasks";
*
* @param task 作成されるタスク
*/
export async function createTask(task: Task): Promise<void> {
await createData(COLLECTION_NAME, task);
export async function createTask(userId: string, task: Task): Promise<void> {
await createData(`users/${userId}/${COLLECTION_NAME}`, task);
}

/**
* 全タスクを取得する。
*
* @returns 全タスク
*/
export async function readTasks(): Promise<Task[]> {
return readData<Task>(COLLECTION_NAME);
export async function readTasks(userId: string): Promise<Task[]> {
return readData<Task>(`users/${userId}/${COLLECTION_NAME}`);
}

/**
Expand All @@ -33,8 +33,8 @@ export async function readTasks(): Promise<Task[]> {
* @param id - 取得するタスクのID。
* @returns 指定されたIDのタスク。
*/
export async function readSingleTask(id: string): Promise<Task | null> {
return readSingleData<Task>(COLLECTION_NAME, id);
export async function readSingleTask(userId: string, id: string): Promise<Task | null> {
return readSingleData<Task>(`users/${userId}/${COLLECTION_NAME}`, id);
}

/**
Expand All @@ -44,17 +44,18 @@ export async function readSingleTask(id: string): Promise<Task | null> {
* @param taskData 新規タスクデータ
*/
export async function updateTask(
userId: string,
id: string,
taskData: Partial<Task>,
): Promise<void> {
await updateData<Task>(COLLECTION_NAME, id, taskData);
await updateData<Task>(`users/${userId}/${COLLECTION_NAME}`, id, taskData);
}

/**
* 指定されたIDのタスクを削除します。
*
* @param id 削除するタスクのID。
*/
export async function deleteTask(id: string): Promise<void> {
await deleteData(COLLECTION_NAME, id);
export async function deleteTask(userId: string, id: string): Promise<void> {
await deleteData(`users/${userId}/${COLLECTION_NAME}`, id);
}
19 changes: 10 additions & 9 deletions task_yell/src/lib/want-todo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ const COLLECTION_NAME = "want-todos";
*
* @param wantTodo - 作成する `WantTodo` オブジェクト。
*/
export async function createWantTodo(wantTodo: WantTodo): Promise<void> {
await createData<WantTodo>(COLLECTION_NAME, wantTodo);
export async function createWantTodo(userId: string, wantTodo: WantTodo): Promise<void> {
await createData<WantTodo>(`users/${userId}/${COLLECTION_NAME}`, wantTodo);
}

/**
* 全WantTodoを取得
*
* @returns 全WantTodo
*/
export async function readWantTodos(): Promise<WantTodo[]> {
return readData<WantTodo>(COLLECTION_NAME);
export async function readWantTodos(userId: string): Promise<WantTodo[]> {
return readData<WantTodo>(`users/${userId}/${COLLECTION_NAME}`);
}

/**
Expand All @@ -32,8 +32,8 @@ export async function readWantTodos(): Promise<WantTodo[]> {
* @param id - 取得するイベントのID。
* @returns 指定されたIDのWantTodo。
*/
export async function readSingleWantTodo(id: string): Promise<WantTodo | null> {
return readSingleData<WantTodo>(COLLECTION_NAME, id);
export async function readSingleWantTodo(userId: string, id: string): Promise<WantTodo | null> {
return readSingleData<WantTodo>(`users/${userId}/${COLLECTION_NAME}`, id);
}

/**
Expand All @@ -43,17 +43,18 @@ export async function readSingleWantTodo(id: string): Promise<WantTodo | null> {
* @param wantTodoData 新規WantTodoデータ。
*/
export async function updateWantTodo(
userId: string,
id: string,
wantTodoData: Partial<WantTodo>,
): Promise<void> {
return updateData<WantTodo>(COLLECTION_NAME, id, wantTodoData);
return updateData<WantTodo>(`users/${userId}/${COLLECTION_NAME}`, id, wantTodoData);
}

/**
* 指定されたIDのWantTodoを削除します。
*
* @param id - 削除するWantTodoのID。
*/
export async function deleteWantTodo(id: string): Promise<void> {
return deleteData(COLLECTION_NAME, id);
export async function deleteWantTodo(userId: string, id: string): Promise<void> {
return deleteData(`users/${userId}/${COLLECTION_NAME}`, id);
}

0 comments on commit 7a7e615

Please sign in to comment.