261 lines
6 KiB
TypeScript
261 lines
6 KiB
TypeScript
|
|
import apiService from "@/services/api.service";
|
||
|
|
|
||
|
|
export interface BlogPost {
|
||
|
|
id: string;
|
||
|
|
title: string;
|
||
|
|
slug: string;
|
||
|
|
content?: string;
|
||
|
|
summary: string;
|
||
|
|
coverImage?: string;
|
||
|
|
author: {
|
||
|
|
id: string;
|
||
|
|
name: string;
|
||
|
|
avatar?: string;
|
||
|
|
};
|
||
|
|
category: {
|
||
|
|
id: string;
|
||
|
|
name: string;
|
||
|
|
slug: string;
|
||
|
|
};
|
||
|
|
tags: string[];
|
||
|
|
viewCount: number;
|
||
|
|
likeCount: number;
|
||
|
|
commentCount: number;
|
||
|
|
isPublished: boolean;
|
||
|
|
publishedAt?: string;
|
||
|
|
createdAt: string;
|
||
|
|
updatedAt: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface BlogCategory {
|
||
|
|
id: string;
|
||
|
|
name: string;
|
||
|
|
slug: string;
|
||
|
|
description?: string;
|
||
|
|
postCount: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface BlogComment {
|
||
|
|
id: string;
|
||
|
|
postId: string;
|
||
|
|
content: string;
|
||
|
|
author: {
|
||
|
|
id: string;
|
||
|
|
name: string;
|
||
|
|
avatar?: string;
|
||
|
|
};
|
||
|
|
parentId?: string;
|
||
|
|
replies?: BlogComment[];
|
||
|
|
likeCount: number;
|
||
|
|
isLiked?: boolean;
|
||
|
|
createdAt: string;
|
||
|
|
updatedAt: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface CreateUpdateBlogPostDto {
|
||
|
|
title: string
|
||
|
|
content: string
|
||
|
|
summary: string
|
||
|
|
categoryId: string
|
||
|
|
tags: string[]
|
||
|
|
coverImage?: string
|
||
|
|
isPublished: boolean
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface CreateUpdateBlogCategoryDto {
|
||
|
|
name: string
|
||
|
|
slug: string
|
||
|
|
description: string
|
||
|
|
icon?: string
|
||
|
|
displayOrder: number
|
||
|
|
isActive: boolean
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface CreateCommentDto {
|
||
|
|
postId: string;
|
||
|
|
content: string;
|
||
|
|
parentId?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface BlogListParams {
|
||
|
|
page?: number;
|
||
|
|
pageSize?: number;
|
||
|
|
categoryId?: string;
|
||
|
|
tag?: string;
|
||
|
|
search?: string;
|
||
|
|
authorId?: string;
|
||
|
|
sortBy?: 'latest' | 'popular' | 'trending';
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface PaginatedResponse<T> {
|
||
|
|
items: T[];
|
||
|
|
totalCount: number;
|
||
|
|
pageNumber: number;
|
||
|
|
pageSize: number;
|
||
|
|
totalPages: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
class BlogService {
|
||
|
|
async getPosts(params: BlogListParams = {}): Promise<PaginatedResponse<BlogPost>> {
|
||
|
|
const response = await apiService.fetchData<PaginatedResponse<BlogPost>>({
|
||
|
|
url: '/api/app/blog/posts',
|
||
|
|
method: 'GET',
|
||
|
|
params
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async getPost(idOrSlug: string): Promise<BlogPost> {
|
||
|
|
const response = await apiService.fetchData<BlogPost>({
|
||
|
|
url: `/api/app/blog/posts/${idOrSlug}`,
|
||
|
|
method: 'GET'
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async createPost(data: CreateUpdateBlogPostDto): Promise<BlogPost> {
|
||
|
|
const response = await apiService.fetchData<BlogPost>({
|
||
|
|
url: '/api/app/blog/posts',
|
||
|
|
method: 'POST',
|
||
|
|
data: data as any
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async updatePost(id: string, data: CreateUpdateBlogPostDto): Promise<BlogPost> {
|
||
|
|
const response = await apiService.fetchData<BlogPost>({
|
||
|
|
url: `/api/app/blog/posts/${id}`,
|
||
|
|
method: 'PUT',
|
||
|
|
data: data as any
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async deletePost(id: string): Promise<void> {
|
||
|
|
await apiService.fetchData({
|
||
|
|
url: `/api/app/blog/posts/${id}`,
|
||
|
|
method: 'DELETE'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
async publishPost(id: string): Promise<BlogPost> {
|
||
|
|
const response = await apiService.fetchData<BlogPost>({
|
||
|
|
url: `/api/app/blog/posts/${id}/publish`,
|
||
|
|
method: 'POST'
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async unpublishPost(id: string): Promise<BlogPost> {
|
||
|
|
const response = await apiService.fetchData<BlogPost>({
|
||
|
|
url: `/api/app/blog/posts/${id}/unpublish`,
|
||
|
|
method: 'POST'
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async getCategories(): Promise<BlogCategory[]> {
|
||
|
|
const response = await apiService.fetchData<BlogCategory[]>({
|
||
|
|
url: '/api/app/blog/categories',
|
||
|
|
method: 'GET'
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async getComments(postId: string): Promise<BlogComment[]> {
|
||
|
|
const response = await apiService.fetchData<BlogComment[]>({
|
||
|
|
url: `/api/app/blog/posts/${postId}/comments`,
|
||
|
|
method: 'GET'
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async createComment(data: CreateCommentDto): Promise<BlogComment> {
|
||
|
|
const response = await apiService.fetchData<BlogComment>({
|
||
|
|
url: '/api/app/blog/comments',
|
||
|
|
method: 'POST',
|
||
|
|
data: data as any
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async deleteComment(id: string): Promise<void> {
|
||
|
|
await apiService.fetchData({
|
||
|
|
url: `/api/app/blog/comments/${id}`,
|
||
|
|
method: 'DELETE'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
async likePost(postId: string): Promise<void> {
|
||
|
|
await apiService.fetchData({
|
||
|
|
url: `/api/app/blog/posts/${postId}/like`,
|
||
|
|
method: 'POST'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
async unlikePost(postId: string): Promise<void> {
|
||
|
|
await apiService.fetchData({
|
||
|
|
url: `/api/app/blog/posts/${postId}/like`,
|
||
|
|
method: 'DELETE'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
async likeComment(commentId: string): Promise<void> {
|
||
|
|
await apiService.fetchData({
|
||
|
|
url: `/api/app/blog/comments/${commentId}/like`,
|
||
|
|
method: 'POST'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
async unlikeComment(commentId: string): Promise<void> {
|
||
|
|
await apiService.fetchData({
|
||
|
|
url: `/api/app/blog/comments/${commentId}/like`,
|
||
|
|
method: 'DELETE'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
async getTags(): Promise<string[]> {
|
||
|
|
const response = await apiService.fetchData<string[]>({
|
||
|
|
url: '/api/app/blog/tags',
|
||
|
|
method: 'GET'
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Category methods
|
||
|
|
async getCategory(id: string): Promise<BlogCategory> {
|
||
|
|
const response = await apiService.fetchData<BlogCategory>({
|
||
|
|
url: `/api/app/blog/categories/${id}`,
|
||
|
|
method: 'GET'
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async createCategory(data: CreateUpdateBlogCategoryDto): Promise<BlogCategory> {
|
||
|
|
const response = await apiService.fetchData<BlogCategory>({
|
||
|
|
url: '/api/app/blog/categories',
|
||
|
|
method: 'POST',
|
||
|
|
data: data as any
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async updateCategory(id: string, data: CreateUpdateBlogCategoryDto): Promise<BlogCategory> {
|
||
|
|
const response = await apiService.fetchData<BlogCategory>({
|
||
|
|
url: `/api/app/blog/categories/${id}`,
|
||
|
|
method: 'PUT',
|
||
|
|
data: data as any
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async deleteCategory(id: string): Promise<void> {
|
||
|
|
await apiService.fetchData({
|
||
|
|
url: `/api/app/blog/categories/${id}`,
|
||
|
|
method: 'DELETE'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export const blogService = new BlogService();
|