Files
HIPCTF2/frontend/src/app/core/services/blog.service.ts
T

75 lines
1.8 KiB
TypeScript

import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { firstValueFrom } from 'rxjs';
export type BlogStatus = 'draft' | 'published';
export interface AdminBlogPost {
id: string;
title: string;
bodyMd: string;
status: BlogStatus;
createdAt: string;
updatedAt: string;
publishedAt: string | null;
}
export interface CreatePostBody {
title: string;
bodyMd: string;
status?: BlogStatus;
}
export interface UpdatePostBody {
title?: string;
bodyMd?: string;
status?: BlogStatus;
}
export interface PublicBlogPost {
id: string;
title: string;
publishedAt: string;
bodyMd: string;
}
@Injectable({ providedIn: 'root' })
export class BlogApiService {
private readonly http = inject(HttpClient);
async listAllPosts(): Promise<AdminBlogPost[]> {
return firstValueFrom(
this.http.get<AdminBlogPost[]>('/api/v1/admin/blog/posts', { withCredentials: true }),
);
}
async createPost(body: CreatePostBody): Promise<AdminBlogPost> {
return firstValueFrom(
this.http.post<AdminBlogPost>('/api/v1/admin/blog/posts', body, { withCredentials: true }),
);
}
async updatePost(id: string, body: UpdatePostBody): Promise<AdminBlogPost> {
return firstValueFrom(
this.http.put<AdminBlogPost>(`/api/v1/admin/blog/posts/${encodeURIComponent(id)}`, body, {
withCredentials: true,
}),
);
}
async deletePost(id: string): Promise<void> {
await firstValueFrom(
this.http.delete<void>(`/api/v1/admin/blog/posts/${encodeURIComponent(id)}`, {
withCredentials: true,
}),
);
}
async listPublished(): Promise<PublicBlogPost[]> {
const res = await firstValueFrom(
this.http.get<{ posts: PublicBlogPost[] }>('/api/v1/blog/posts', { withCredentials: true }),
);
return res.posts ?? [];
}
}