From 623210545802c0faa3cbaf9adee0a97830af8cd3 Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Sun, 12 Oct 2025 18:03:29 +0800 Subject: [PATCH] feat: update post ID type from number to string across the application --- frontend/.env.example | 1 + frontend/src/lib/post/adapter/gateway/postApiService.ts | 2 +- .../src/lib/post/adapter/gateway/postRepositoryImpl.ts | 2 +- frontend/src/lib/post/adapter/presenter/postBloc.ts | 6 +++--- .../src/lib/post/application/repository/postRepository.ts | 2 +- .../src/lib/post/application/useCase/getPostUseCase.ts | 2 +- frontend/src/lib/post/framework/api/postApiServiceImpl.ts | 2 +- frontend/src/lib/post/framework/ui/PostContentPage.svelte | 4 ++-- frontend/src/routes/post/[id]/+page.server.ts | 7 +------ frontend/src/routes/post/[id]/+page.svelte | 3 +-- 10 files changed, 13 insertions(+), 18 deletions(-) create mode 100644 frontend/.env.example diff --git a/frontend/.env.example b/frontend/.env.example new file mode 100644 index 0000000..76e9364 --- /dev/null +++ b/frontend/.env.example @@ -0,0 +1 @@ +PUBLIC_API_BASE_URL=http://127.0.0.1:5173/api/ diff --git a/frontend/src/lib/post/adapter/gateway/postApiService.ts b/frontend/src/lib/post/adapter/gateway/postApiService.ts index ccabb11..a8503d7 100644 --- a/frontend/src/lib/post/adapter/gateway/postApiService.ts +++ b/frontend/src/lib/post/adapter/gateway/postApiService.ts @@ -3,5 +3,5 @@ import type { PostResponseDto } from '$lib/post/adapter/gateway/postResponseDto' export interface PostApiService { getAllPosts(): Promise; - getPost(id: number): Promise; + getPost(id: string): Promise; } diff --git a/frontend/src/lib/post/adapter/gateway/postRepositoryImpl.ts b/frontend/src/lib/post/adapter/gateway/postRepositoryImpl.ts index bfd0f3e..33723f3 100644 --- a/frontend/src/lib/post/adapter/gateway/postRepositoryImpl.ts +++ b/frontend/src/lib/post/adapter/gateway/postRepositoryImpl.ts @@ -11,7 +11,7 @@ export class PostRepositoryImpl implements PostRepository { return dtos.map((dto) => dto.toEntity()); } - async getPost(id: number): Promise { + async getPost(id: string): Promise { const dto = await this.postApiService.getPost(id); return dto?.toEntity() ?? null; } diff --git a/frontend/src/lib/post/adapter/presenter/postBloc.ts b/frontend/src/lib/post/adapter/presenter/postBloc.ts index 0e65a29..584a571 100644 --- a/frontend/src/lib/post/adapter/presenter/postBloc.ts +++ b/frontend/src/lib/post/adapter/presenter/postBloc.ts @@ -32,7 +32,7 @@ export class PostBloc { } } - private async loadPost(id: number): Promise { + private async loadPost(id: string): Promise { this.state.set({ status: StatusType.Loading, data: get(this.state).data }); const post = await this.getPostUseCase.execute(id); @@ -56,7 +56,7 @@ export enum PostEventType { PostLoadedEvent } -export interface PostLoadedEvent { +interface PostLoadedEvent { event: PostEventType.PostLoadedEvent; - id: number; + id: string; } diff --git a/frontend/src/lib/post/application/repository/postRepository.ts b/frontend/src/lib/post/application/repository/postRepository.ts index 021170f..664a263 100644 --- a/frontend/src/lib/post/application/repository/postRepository.ts +++ b/frontend/src/lib/post/application/repository/postRepository.ts @@ -3,5 +3,5 @@ import type { PostInfo } from '$lib/post/domain/entity/postInfo'; export interface PostRepository { getAllPosts(): Promise; - getPost(id: number): Promise; + getPost(id: string): Promise; } diff --git a/frontend/src/lib/post/application/useCase/getPostUseCase.ts b/frontend/src/lib/post/application/useCase/getPostUseCase.ts index 2e2d920..7e2a60d 100644 --- a/frontend/src/lib/post/application/useCase/getPostUseCase.ts +++ b/frontend/src/lib/post/application/useCase/getPostUseCase.ts @@ -4,7 +4,7 @@ import type { Post } from '$lib/post/domain/entity/post'; export class GetPostUseCase { constructor(private readonly postRepository: PostRepository) {} - execute(id: number): Promise { + execute(id: string): Promise { return this.postRepository.getPost(id); } } diff --git a/frontend/src/lib/post/framework/api/postApiServiceImpl.ts b/frontend/src/lib/post/framework/api/postApiServiceImpl.ts index 6ede11e..3abeea5 100644 --- a/frontend/src/lib/post/framework/api/postApiServiceImpl.ts +++ b/frontend/src/lib/post/framework/api/postApiServiceImpl.ts @@ -19,7 +19,7 @@ export class PostApiServiceImpl implements PostApiService { return json.map(PostInfoResponseDto.fromJson); } - async getPost(id: number): Promise { + async getPost(id: string): Promise { const url = new URL(`post/${id}`, Environment.API_BASE_URL); const response = await this.fetchFn(url.href); diff --git a/frontend/src/lib/post/framework/ui/PostContentPage.svelte b/frontend/src/lib/post/framework/ui/PostContentPage.svelte index 207faed..df68c42 100644 --- a/frontend/src/lib/post/framework/ui/PostContentPage.svelte +++ b/frontend/src/lib/post/framework/ui/PostContentPage.svelte @@ -7,7 +7,7 @@ import generateTitle from '$lib/common/framework/ui/generateTitle'; import StructuredData from '$lib/post/framework/ui/StructuredData.svelte'; - const { id }: { id: number } = $props(); + const { id }: { id: string } = $props(); const postBloc = getContext(PostBloc.name); const state = $derived($postBloc); @@ -32,7 +32,7 @@ {/if} {/if} -
+
{#if state.data}
diff --git a/frontend/src/routes/post/[id]/+page.server.ts b/frontend/src/routes/post/[id]/+page.server.ts index dc958b4..e57ee52 100644 --- a/frontend/src/routes/post/[id]/+page.server.ts +++ b/frontend/src/routes/post/[id]/+page.server.ts @@ -5,14 +5,9 @@ import type { PageServerLoad } from './$types'; export const load: PageServerLoad = async ({ locals, params }) => { const { postBloc } = locals; - const id = parseInt(params.id, 10); - if (isNaN(id) || id <= 0) { - error(400, { message: 'Invalid post ID' }); - } - const state = await postBloc.dispatch({ event: PostEventType.PostLoadedEvent, - id: id + id: params.id }); if (!state.data) { error(404, { message: 'Post not found' }); diff --git a/frontend/src/routes/post/[id]/+page.svelte b/frontend/src/routes/post/[id]/+page.svelte index a8ee450..da4ea70 100644 --- a/frontend/src/routes/post/[id]/+page.svelte +++ b/frontend/src/routes/post/[id]/+page.svelte @@ -9,8 +9,7 @@ import PostContentPage from '$lib/post/framework/ui/PostContentPage.svelte'; const { data, params }: PageProps = $props(); - - const id = parseInt(params.id, 10); + const { id } = params; const initialData = PostViewModel.rehydrate(data.dehydratedData!);