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

101 lines
3.1 KiB
TypeScript
Raw Permalink 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 { 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()
},
}