first commit
This commit is contained in:
74
frontend/services/categoryService.ts
Normal file
74
frontend/services/categoryService.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { getSession } from "next-auth/react";
|
||||
import { Category, CategoryListResponse } from "../types/category";
|
||||
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL + "/api/v1";
|
||||
|
||||
async function fetchWithAuth(url: string, options: RequestInit = {}) {
|
||||
const session = await getSession();
|
||||
if (!session?.accessToken) {
|
||||
throw new Error("No access token found");
|
||||
}
|
||||
|
||||
const headers = {
|
||||
...options.headers,
|
||||
"Authorization": `Bearer ${session.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
const response = await fetch(url, { ...options, headers });
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(errorData.error || errorData.message || "API request failed");
|
||||
}
|
||||
|
||||
// For 204 No Content
|
||||
if (response.status === 204) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export const categoryService = {
|
||||
async getCategories(page = 1, perPage = 20, search = "", soft = ""): Promise<CategoryListResponse> {
|
||||
const query = new URLSearchParams({
|
||||
page: page.toString(),
|
||||
per_page: perPage.toString(),
|
||||
q: search,
|
||||
soft: soft,
|
||||
});
|
||||
const data = await fetchWithAuth(`${API_URL}/admin/categories?${query}`);
|
||||
return data;
|
||||
},
|
||||
|
||||
async createCategory(data: Partial<Category>): Promise<Category> {
|
||||
const res = await fetchWithAuth(`${API_URL}/admin/categories`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return res.data;
|
||||
},
|
||||
|
||||
async updateCategory(id: number, data: Partial<Category>): Promise<Category> {
|
||||
const res = await fetchWithAuth(`${API_URL}/admin/categories/${id}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return res.data;
|
||||
},
|
||||
|
||||
async deleteCategory(id: number): Promise<void> {
|
||||
await fetchWithAuth(`${API_URL}/admin/categories/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
},
|
||||
|
||||
async restoreCategory(id: number): Promise<Category> {
|
||||
const res = await fetchWithAuth(`${API_URL}/admin/categories/${id}/restore`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({}),
|
||||
});
|
||||
return res.data;
|
||||
}
|
||||
};
|
||||
104
frontend/services/heroService.ts
Normal file
104
frontend/services/heroService.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import { getSession } from "next-auth/react"
|
||||
import { HeroResponse, HeroDetailResponse } from "@/types/hero"
|
||||
|
||||
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 heroService = {
|
||||
getHeroes: async (
|
||||
page: number = 1,
|
||||
perPage: number = 20,
|
||||
search: string = "",
|
||||
soft: string = "with"
|
||||
): Promise<HeroResponse> => {
|
||||
const headers = await getAuthHeaders()
|
||||
const params = new URLSearchParams({
|
||||
page: page.toString(),
|
||||
limit: perPage.toString(),
|
||||
soft: soft,
|
||||
})
|
||||
|
||||
if (search) {
|
||||
params.append("search", search)
|
||||
}
|
||||
|
||||
const res = await fetch(`${API_URL}/admin/heroes?${params}`, {
|
||||
headers: headers as HeadersInit,
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text()
|
||||
console.error("Hero list warning:", res.status, errorText)
|
||||
throw new Error(`Hero listesi alınamadı: ${res.status} ${res.statusText}`)
|
||||
}
|
||||
return res.json()
|
||||
},
|
||||
|
||||
getHeroById: async (id: number): Promise<HeroDetailResponse> => {
|
||||
const headers = await getAuthHeaders()
|
||||
const res = await fetch(`${API_URL}/admin/heroes/${id}`, {
|
||||
headers: headers as HeadersInit,
|
||||
})
|
||||
|
||||
if (!res.ok) throw new Error("Hero detayı alınamadı")
|
||||
return res.json()
|
||||
},
|
||||
|
||||
createHero: async (formData: FormData): Promise<HeroDetailResponse> => {
|
||||
// No auth header needed here because the Next.js API route handles it (it's a same-origin request)
|
||||
// However, if we need to pass the session token from client to server action/route,
|
||||
// standard fetch from client automatically includes cookies for NextAuth.
|
||||
|
||||
const res = await fetch("/api/admin/heroes", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorData = await res.json()
|
||||
throw new Error(errorData.error || "Hero oluşturulamadı")
|
||||
}
|
||||
return res.json()
|
||||
},
|
||||
|
||||
updateHero: async (id: number, formData: FormData): Promise<HeroDetailResponse> => {
|
||||
const res = await fetch(`/api/admin/heroes/${id}`, {
|
||||
method: "PUT",
|
||||
body: formData,
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorData = await res.json()
|
||||
throw new Error(errorData.error || "Hero güncellenemedi")
|
||||
}
|
||||
return res.json()
|
||||
},
|
||||
|
||||
deleteHero: async (id: number): Promise<void> => {
|
||||
const headers = await getAuthHeaders()
|
||||
const res = await fetch(`${API_URL}/admin/heroes/${id}`, {
|
||||
method: "DELETE",
|
||||
headers: headers as HeadersInit,
|
||||
})
|
||||
|
||||
if (!res.ok) throw new Error("Hero silinemedi")
|
||||
},
|
||||
|
||||
restoreHero: async (id: number): Promise<HeroDetailResponse> => {
|
||||
const headers = await getAuthHeaders()
|
||||
const res = await fetch(`${API_URL}/admin/heroes/${id}/restore`, {
|
||||
method: "POST", // veya dökümantasyona göre PUT/PATCH
|
||||
headers: headers as HeadersInit,
|
||||
body: JSON.stringify({}),
|
||||
})
|
||||
|
||||
if (!res.ok) throw new Error("Hero geri yüklenemedi")
|
||||
return res.json()
|
||||
},
|
||||
}
|
||||
102
frontend/services/postService.ts
Normal file
102
frontend/services/postService.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
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();
|
||||
},
|
||||
};
|
||||
100
frontend/services/settingService.ts
Normal file
100
frontend/services/settingService.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import { getSession } from "next-auth/react"
|
||||
import { SettingResponse, SettingDetailResponse } from "@/types/setting"
|
||||
|
||||
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 settingService = {
|
||||
getSettings: async (
|
||||
page: number = 1,
|
||||
perPage: number = 20,
|
||||
search: string = "",
|
||||
soft: string = "with"
|
||||
): Promise<SettingResponse> => {
|
||||
const headers = await getAuthHeaders()
|
||||
const params = new URLSearchParams({
|
||||
page: page.toString(),
|
||||
per_page: perPage.toString(),
|
||||
soft: soft,
|
||||
})
|
||||
|
||||
if (search) {
|
||||
params.append("search", search)
|
||||
}
|
||||
|
||||
const res = await fetch(`${API_URL}/admin/settings?${params}`, {
|
||||
headers: headers as HeadersInit,
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text()
|
||||
console.error("Setting list warning:", res.status, errorText)
|
||||
throw new Error(`Ayarlar listesi alınamadı: ${res.status} ${res.statusText}`)
|
||||
}
|
||||
return res.json()
|
||||
},
|
||||
|
||||
getSettingById: async (id: number): Promise<SettingDetailResponse> => {
|
||||
const headers = await getAuthHeaders()
|
||||
const res = await fetch(`${API_URL}/admin/settings/${id}`, {
|
||||
headers: headers as HeadersInit,
|
||||
})
|
||||
|
||||
if (!res.ok) throw new Error("Ayar detayı alınamadı")
|
||||
return res.json()
|
||||
},
|
||||
|
||||
createSetting: async (formData: FormData): Promise<SettingDetailResponse> => {
|
||||
const res = await fetch("/api/admin/settings", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorData = await res.json()
|
||||
throw new Error(errorData.error || "Ayar oluşturulamadı")
|
||||
}
|
||||
return res.json()
|
||||
},
|
||||
|
||||
updateSetting: async (id: number, formData: FormData): Promise<SettingDetailResponse> => {
|
||||
const res = await fetch(`/api/admin/settings/${id}`, {
|
||||
method: "PUT",
|
||||
body: formData,
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorData = await res.json()
|
||||
throw new Error(errorData.error || "Ayar güncellenemedi")
|
||||
}
|
||||
return res.json()
|
||||
},
|
||||
|
||||
deleteSetting: async (id: number): Promise<void> => {
|
||||
const headers = await getAuthHeaders()
|
||||
const res = await fetch(`${API_URL}/admin/settings/${id}`, {
|
||||
method: "DELETE",
|
||||
headers: headers as HeadersInit,
|
||||
})
|
||||
|
||||
if (!res.ok) throw new Error("Ayar silinemedi")
|
||||
},
|
||||
|
||||
restoreSetting: async (id: number): Promise<SettingDetailResponse> => {
|
||||
const headers = await getAuthHeaders()
|
||||
const res = await fetch(`${API_URL}/admin/settings/${id}/restore`, {
|
||||
method: "POST",
|
||||
headers: headers as HeadersInit,
|
||||
body: JSON.stringify({}),
|
||||
})
|
||||
|
||||
if (!res.ok) throw new Error("Ayar geri yüklenemedi")
|
||||
return res.json()
|
||||
},
|
||||
}
|
||||
74
frontend/services/tagService.ts
Normal file
74
frontend/services/tagService.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { getSession } from "next-auth/react";
|
||||
import { Tag, TagListResponse } from "../types/tag";
|
||||
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL + "/api/v1";
|
||||
|
||||
async function fetchWithAuth(url: string, options: RequestInit = {}) {
|
||||
const session = await getSession();
|
||||
if (!session?.accessToken) {
|
||||
throw new Error("No access token found");
|
||||
}
|
||||
|
||||
const headers = {
|
||||
...options.headers,
|
||||
"Authorization": `Bearer ${session.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
const response = await fetch(url, { ...options, headers });
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(errorData.error || errorData.message || "API request failed");
|
||||
}
|
||||
|
||||
// For 204 No Content
|
||||
if (response.status === 204) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export const tagService = {
|
||||
async getTags(page = 1, perPage = 20, search = "", soft = ""): Promise<TagListResponse> {
|
||||
const query = new URLSearchParams({
|
||||
page: page.toString(),
|
||||
per_page: perPage.toString(),
|
||||
q: search,
|
||||
soft: soft,
|
||||
});
|
||||
const data = await fetchWithAuth(`${API_URL}/admin/tags?${query}`);
|
||||
return data;
|
||||
},
|
||||
|
||||
async createTag(name: string): Promise<Tag> {
|
||||
const data = await fetchWithAuth(`${API_URL}/admin/tags`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
return data.data;
|
||||
},
|
||||
|
||||
async updateTag(id: number, name: string): Promise<Tag> {
|
||||
const data = await fetchWithAuth(`${API_URL}/admin/tags/${id}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
return data.data;
|
||||
},
|
||||
|
||||
async deleteTag(id: number): Promise<void> {
|
||||
await fetchWithAuth(`${API_URL}/admin/tags/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
},
|
||||
|
||||
async restoreTag(id: number): Promise<Tag> {
|
||||
const data = await fetchWithAuth(`${API_URL}/admin/tags/${id}/restore`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({}),
|
||||
});
|
||||
return data.data;
|
||||
}
|
||||
};
|
||||
64
frontend/services/userService.ts
Normal file
64
frontend/services/userService.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { UserListResponse, UserPayload, UserResponse } from "@/types/user";
|
||||
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8080";
|
||||
|
||||
async function fetchWithAuth(url: string, token: string, options: RequestInit = {}) {
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${token}`,
|
||||
...options.headers,
|
||||
};
|
||||
|
||||
const response = await fetch(`${API_URL}${url}`, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(errorData.message || "API request failed");
|
||||
}
|
||||
|
||||
// For 204 No Content
|
||||
if (response.status === 204) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export const userService = {
|
||||
async getUsers(token: string, page: number = 1, per_page: number = 20, soft: string = ""): Promise<UserListResponse> {
|
||||
const queryParams = new URLSearchParams({
|
||||
page: page.toString(),
|
||||
per_page: per_page.toString(),
|
||||
});
|
||||
if (soft) {
|
||||
queryParams.append("soft", soft);
|
||||
}
|
||||
return fetchWithAuth(`/api/v1/admin/users?${queryParams.toString()}`, token);
|
||||
},
|
||||
|
||||
async getUser(token: string, id: number): Promise<UserResponse> {
|
||||
return fetchWithAuth(`/api/v1/admin/users/${id}`, token);
|
||||
},
|
||||
|
||||
async updateUser(token: string, id: number, data: UserPayload): Promise<UserResponse> {
|
||||
return fetchWithAuth(`/api/v1/admin/users/${id}`, token, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
},
|
||||
|
||||
async deleteUser(token: string, id: number): Promise<void> {
|
||||
return fetchWithAuth(`/api/v1/admin/users/${id}`, token, {
|
||||
method: "DELETE",
|
||||
});
|
||||
},
|
||||
|
||||
async restoreUser(token: string, id: number): Promise<UserResponse> {
|
||||
return fetchWithAuth(`/api/v1/admin/users/${id}/restore`, token, {
|
||||
method: "POST",
|
||||
});
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user