From 160c93b83d230a8a68ede800fe9c81e1908b34dd Mon Sep 17 00:00:00 2001 From: louiscklaw Date: Thu, 15 May 2025 09:26:36 +0800 Subject: [PATCH] ``` refactor GetUserById function and add Create/UpdateUser APIs with type definitions ``` --- .../cms/src/db/Users/ChangeUserState.tsx | 0 002_source/cms/src/db/Users/GetById.tsx | 18 ++++++------------ 002_source/cms/src/db/Users/UpdateById.tsx | 10 ++++++++++ 002_source/cms/src/db/Users/_GUIDELINES.md | 5 ++++- 002_source/cms/src/db/Users/type.d.ts | 15 +++++++++++++++ 5 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 002_source/cms/src/db/Users/ChangeUserState.tsx create mode 100644 002_source/cms/src/db/Users/UpdateById.tsx create mode 100644 002_source/cms/src/db/Users/type.d.ts diff --git a/002_source/cms/src/db/Users/ChangeUserState.tsx b/002_source/cms/src/db/Users/ChangeUserState.tsx new file mode 100644 index 0000000..e69de29 diff --git a/002_source/cms/src/db/Users/GetById.tsx b/002_source/cms/src/db/Users/GetById.tsx index f313892..aa8c2cf 100644 --- a/002_source/cms/src/db/Users/GetById.tsx +++ b/002_source/cms/src/db/Users/GetById.tsx @@ -1,15 +1,9 @@ -import { pb } from '@/lib/pb'; import { COL_USERS } from '@/constants'; -import type { User } from '@/types/user'; -export async function getUserById(id: string): Promise { - try { - const user = await pb.collection(COL_USERS).getOne(id); - return user; - } catch (err) { - if (err instanceof Error && err.message.includes('404')) { - throw new Error(`User with ID ${id} not found`); - } - throw err; - } +import { pb } from '@/lib/pb'; + +import type { User } from './type.d'; + +export function getUserById(id: string): Promise { + return pb.collection(COL_USERS).getOne(id); } diff --git a/002_source/cms/src/db/Users/UpdateById.tsx b/002_source/cms/src/db/Users/UpdateById.tsx new file mode 100644 index 0000000..53cc3d4 --- /dev/null +++ b/002_source/cms/src/db/Users/UpdateById.tsx @@ -0,0 +1,10 @@ +import { COL_USERS } from '@/constants'; +import type { RecordModel } from 'pocketbase'; + +import { pb } from '@/lib/pb'; + +import type { UpdateUser } from './type.d'; + +export async function UpdateUserById(id: string, data: Partial): Promise { + return pb.collection(COL_USERS).update(id, data); +} diff --git a/002_source/cms/src/db/Users/_GUIDELINES.md b/002_source/cms/src/db/Users/_GUIDELINES.md index 0c42524..b652215 100644 --- a/002_source/cms/src/db/Users/_GUIDELINES.md +++ b/002_source/cms/src/db/Users/_GUIDELINES.md @@ -21,9 +21,12 @@ the `@` sign refer to `/home/logic/_wsl_workspace/001_github_ws/lettersoup-onlin simple template: ```typescript -import { pb } from '@/lib/pb'; import { COL_USERS } from '@/constants'; +import { pb } from '@/lib/pb'; + +import type { User } from './type.d'; + export async function createUser(data: CreateFormProps) { // ...content // use direct return of pb.collection (e.g. return pb.collection(xxx)) diff --git a/002_source/cms/src/db/Users/type.d.ts b/002_source/cms/src/db/Users/type.d.ts new file mode 100644 index 0000000..08de926 --- /dev/null +++ b/002_source/cms/src/db/Users/type.d.ts @@ -0,0 +1,15 @@ +// +// RULES +// pocketbase Users collection schema +// +// User type definitions +export interface User { + verified: boolean; + // + id: string; + createdAt: Date; +} + +export interface UpdateUser { + verified?: boolean; +}