blog/frontend/src/lib/post/adapter/gateway/postInfoResponseDto.ts
SquidSpirit 8b1d643531
All checks were successful
Frontend CI / build (push) Successful in 1m21s
PR Title Check / pr-title-check (pull_request) Successful in 17s
chore: pnpm format
2025-10-12 18:26:56 +08:00

73 lines
2.0 KiB
TypeScript

import { LabelResponseDto, LabelResponseSchema } from '$lib/post/adapter/gateway/labelResponseDto';
import { PostInfo } from '$lib/post/domain/entity/postInfo';
import z from 'zod';
export const PostInfoResponseSchema = z.object({
id: z.int32(),
semantic_id: z.string(),
title: z.string(),
description: z.string(),
preview_image_url: z.url(),
labels: z.array(LabelResponseSchema),
published_time: z.iso.datetime({ offset: true }).nullable(),
});
export class PostInfoResponseDto {
readonly id: number;
readonly semanticId: string;
readonly title: string;
readonly description: string;
readonly previewImageUrl: URL;
readonly labels: readonly LabelResponseDto[];
readonly publishedTime: Date | null;
private constructor(props: {
id: number;
semanticId: string;
title: string;
description: string;
previewImageUrl: URL;
labels: LabelResponseDto[];
publishedTime: Date | null;
}) {
this.id = props.id;
this.semanticId = props.semanticId;
this.title = props.title;
this.description = props.description;
this.previewImageUrl = props.previewImageUrl;
this.labels = props.labels;
this.publishedTime = props.publishedTime;
}
static fromJson(json: unknown): PostInfoResponseDto {
const parsedJson = PostInfoResponseSchema.parse(json);
let published_time: Date | null = null;
if (parsedJson.published_time !== null) {
published_time = new Date(parsedJson.published_time);
}
return new PostInfoResponseDto({
id: parsedJson.id,
semanticId: parsedJson.semantic_id,
title: parsedJson.title,
description: parsedJson.description,
previewImageUrl: new URL(parsedJson.preview_image_url),
labels: parsedJson.labels.map((label) => LabelResponseDto.fromJson(label)),
publishedTime: published_time,
});
}
toEntity(): PostInfo {
return new PostInfo({
id: this.id,
semanticId: this.semanticId,
title: this.title,
description: this.description,
previewImageUrl: this.previewImageUrl,
labels: this.labels.map((label) => label.toEntity()),
publishedTime: this.publishedTime,
});
}
}