first commit
This commit is contained in:
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()
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user