feat: update post ID type from number to string across the application

This commit is contained in:
SquidSpirit 2025-10-12 18:03:29 +08:00
parent 9862850d28
commit 6232105458
10 changed files with 13 additions and 18 deletions

1
frontend/.env.example Normal file
View File

@ -0,0 +1 @@
PUBLIC_API_BASE_URL=http://127.0.0.1:5173/api/

View File

@ -3,5 +3,5 @@ import type { PostResponseDto } from '$lib/post/adapter/gateway/postResponseDto'
export interface PostApiService { export interface PostApiService {
getAllPosts(): Promise<PostInfoResponseDto[]>; getAllPosts(): Promise<PostInfoResponseDto[]>;
getPost(id: number): Promise<PostResponseDto | null>; getPost(id: string): Promise<PostResponseDto | null>;
} }

View File

@ -11,7 +11,7 @@ export class PostRepositoryImpl implements PostRepository {
return dtos.map((dto) => dto.toEntity()); return dtos.map((dto) => dto.toEntity());
} }
async getPost(id: number): Promise<Post | null> { async getPost(id: string): Promise<Post | null> {
const dto = await this.postApiService.getPost(id); const dto = await this.postApiService.getPost(id);
return dto?.toEntity() ?? null; return dto?.toEntity() ?? null;
} }

View File

@ -32,7 +32,7 @@ export class PostBloc {
} }
} }
private async loadPost(id: number): Promise<PostState> { private async loadPost(id: string): Promise<PostState> {
this.state.set({ status: StatusType.Loading, data: get(this.state).data }); this.state.set({ status: StatusType.Loading, data: get(this.state).data });
const post = await this.getPostUseCase.execute(id); const post = await this.getPostUseCase.execute(id);
@ -56,7 +56,7 @@ export enum PostEventType {
PostLoadedEvent PostLoadedEvent
} }
export interface PostLoadedEvent { interface PostLoadedEvent {
event: PostEventType.PostLoadedEvent; event: PostEventType.PostLoadedEvent;
id: number; id: string;
} }

View File

@ -3,5 +3,5 @@ import type { PostInfo } from '$lib/post/domain/entity/postInfo';
export interface PostRepository { export interface PostRepository {
getAllPosts(): Promise<PostInfo[]>; getAllPosts(): Promise<PostInfo[]>;
getPost(id: number): Promise<Post | null>; getPost(id: string): Promise<Post | null>;
} }

View File

@ -4,7 +4,7 @@ import type { Post } from '$lib/post/domain/entity/post';
export class GetPostUseCase { export class GetPostUseCase {
constructor(private readonly postRepository: PostRepository) {} constructor(private readonly postRepository: PostRepository) {}
execute(id: number): Promise<Post | null> { execute(id: string): Promise<Post | null> {
return this.postRepository.getPost(id); return this.postRepository.getPost(id);
} }
} }

View File

@ -19,7 +19,7 @@ export class PostApiServiceImpl implements PostApiService {
return json.map(PostInfoResponseDto.fromJson); return json.map(PostInfoResponseDto.fromJson);
} }
async getPost(id: number): Promise<PostResponseDto | null> { async getPost(id: string): Promise<PostResponseDto | null> {
const url = new URL(`post/${id}`, Environment.API_BASE_URL); const url = new URL(`post/${id}`, Environment.API_BASE_URL);
const response = await this.fetchFn(url.href); const response = await this.fetchFn(url.href);

View File

@ -7,7 +7,7 @@
import generateTitle from '$lib/common/framework/ui/generateTitle'; import generateTitle from '$lib/common/framework/ui/generateTitle';
import StructuredData from '$lib/post/framework/ui/StructuredData.svelte'; import StructuredData from '$lib/post/framework/ui/StructuredData.svelte';
const { id }: { id: number } = $props(); const { id }: { id: string } = $props();
const postBloc = getContext<PostBloc>(PostBloc.name); const postBloc = getContext<PostBloc>(PostBloc.name);
const state = $derived($postBloc); const state = $derived($postBloc);
@ -32,7 +32,7 @@
{/if} {/if}
{/if} {/if}
</svelte:head> </svelte:head>
<article class="container prose pb-10 prose-gray"> <article class="prose prose-gray container pb-10">
{#if state.data} {#if state.data}
<PostContentHeader postInfo={state.data.info} /> <PostContentHeader postInfo={state.data.info} />
<div class="max-w-3xl"> <div class="max-w-3xl">

View File

@ -5,14 +5,9 @@ import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ locals, params }) => { export const load: PageServerLoad = async ({ locals, params }) => {
const { postBloc } = locals; 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({ const state = await postBloc.dispatch({
event: PostEventType.PostLoadedEvent, event: PostEventType.PostLoadedEvent,
id: id id: params.id
}); });
if (!state.data) { if (!state.data) {
error(404, { message: 'Post not found' }); error(404, { message: 'Post not found' });

View File

@ -9,8 +9,7 @@
import PostContentPage from '$lib/post/framework/ui/PostContentPage.svelte'; import PostContentPage from '$lib/post/framework/ui/PostContentPage.svelte';
const { data, params }: PageProps = $props(); const { data, params }: PageProps = $props();
const { id } = params;
const id = parseInt(params.id, 10);
const initialData = PostViewModel.rehydrate(data.dehydratedData!); const initialData = PostViewModel.rehydrate(data.dehydratedData!);