103 lines
3.2 KiB
TypeScript
103 lines
3.2 KiB
TypeScript
import { getSession } from "next-auth/react";
|
||
import { PostDetailResponse, PostListResponse } from "@/types/post";
|
||
|
||
const API_URL = (process.env.NEXT_PUBLIC_API_URL || "http://localhost:8080") + "/api/v1";
|
||
|
||
async function getAuthHeaders() {
|
||
const session = await getSession();
|
||
return {
|
||
Authorization: `Bearer ${session?.accessToken}`,
|
||
};
|
||
}
|
||
|
||
export const postService = {
|
||
getPosts: async (
|
||
page: number = 1,
|
||
perPage: number = 20,
|
||
search: string = "",
|
||
soft: string = "with"
|
||
): Promise<PostListResponse> => {
|
||
const headers = await getAuthHeaders();
|
||
const params = new URLSearchParams({
|
||
page: page.toString(),
|
||
per_page: perPage.toString(),
|
||
soft: soft,
|
||
});
|
||
|
||
// Backend AdminListPosts arama parametresi olarak "q" kullanıyor
|
||
if (search) {
|
||
params.append("q", search);
|
||
}
|
||
|
||
const res = await fetch(`${API_URL}/admin/posts?${params}`, {
|
||
headers: headers as HeadersInit,
|
||
});
|
||
|
||
if (!res.ok) {
|
||
const errorText = await res.text();
|
||
console.error("Post list warning:", res.status, errorText);
|
||
throw new Error(`Yazı listesi alınamadı: ${res.status} ${res.statusText}`);
|
||
}
|
||
return res.json();
|
||
},
|
||
|
||
getPost: async (id: number): Promise<PostDetailResponse> => {
|
||
const headers = await getAuthHeaders();
|
||
const res = await fetch(`${API_URL}/admin/posts/${id}`, {
|
||
headers: headers as HeadersInit,
|
||
});
|
||
|
||
if (!res.ok) throw new Error("Yazı detayı alınamadı");
|
||
return res.json();
|
||
},
|
||
|
||
createPost: async (formData: FormData): Promise<PostDetailResponse> => {
|
||
const res = await fetch("/api/admin/posts", {
|
||
method: "POST",
|
||
body: formData,
|
||
});
|
||
|
||
if (!res.ok) {
|
||
const errorData = await res.json();
|
||
throw new Error(errorData.error || "Yazı oluşturulamadı");
|
||
}
|
||
return res.json();
|
||
},
|
||
|
||
updatePost: async (id: number, formData: FormData): Promise<PostDetailResponse> => {
|
||
console.log("updatePost called with id:", id);
|
||
const res = await fetch(`/api/admin/posts/${id}`, {
|
||
method: "PUT",
|
||
body: formData,
|
||
});
|
||
|
||
if (!res.ok) {
|
||
const errorData = await res.json();
|
||
throw new Error(errorData.error || "Yazı güncellenemedi");
|
||
}
|
||
return res.json();
|
||
},
|
||
|
||
deletePost: async (id: number): Promise<void> => {
|
||
const headers = await getAuthHeaders();
|
||
const res = await fetch(`${API_URL}/admin/posts/${id}`, {
|
||
method: "DELETE",
|
||
headers: headers as HeadersInit,
|
||
});
|
||
|
||
if (!res.ok) throw new Error("Yazı silinemedi");
|
||
},
|
||
|
||
restorePost: async (id: number): Promise<PostDetailResponse> => {
|
||
const headers = await getAuthHeaders();
|
||
const res = await fetch(`${API_URL}/admin/posts/${id}/restore`, {
|
||
method: "POST",
|
||
headers: headers as HeadersInit,
|
||
body: JSON.stringify({}),
|
||
});
|
||
|
||
if (!res.ok) throw new Error("Yazı geri yüklenemedi");
|
||
return res.json();
|
||
},
|
||
};
|