Files
goGin/frontend/services/postService.ts
Beyhan Oğur 2a5b661443 first commit
2026-04-26 21:46:42 +03:00

103 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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();
},
};