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 { return firstValueFrom( this.http.get('/api/v1/admin/blog/posts', { withCredentials: true }), ); } async createPost(body: CreatePostBody): Promise { return firstValueFrom( this.http.post('/api/v1/admin/blog/posts', body, { withCredentials: true }), ); } async updatePost(id: string, body: UpdatePostBody): Promise { return firstValueFrom( this.http.put(`/api/v1/admin/blog/posts/${encodeURIComponent(id)}`, body, { withCredentials: true, }), ); } async deletePost(id: string): Promise { await firstValueFrom( this.http.delete(`/api/v1/admin/blog/posts/${encodeURIComponent(id)}`, { withCredentials: true, }), ); } async listPublished(): Promise { const res = await firstValueFrom( this.http.get<{ posts: PublicBlogPost[] }>('/api/v1/blog/posts', { withCredentials: true }), ); return res.posts ?? []; } }