feat(services): manage services through backend
This commit is contained in:
+51
-4
@@ -4,7 +4,7 @@ import type { PlayerService } from "@/lib/types"
|
||||
import { httpJson } from "./http"
|
||||
|
||||
type Paginated<T> = {
|
||||
items: T[]
|
||||
items: T[] | null
|
||||
meta: {
|
||||
total: number
|
||||
offset: number
|
||||
@@ -12,10 +12,37 @@ type Paginated<T> = {
|
||||
}
|
||||
}
|
||||
|
||||
function itemsFrom<T>(value: Paginated<T> | T[]): T[] {
|
||||
if (Array.isArray(value)) return value
|
||||
return value.items ?? []
|
||||
}
|
||||
|
||||
export type ServiceInput = {
|
||||
gameId: string
|
||||
title: string
|
||||
description: string
|
||||
price: number
|
||||
unit: string
|
||||
rankRange?: string
|
||||
availability: string[]
|
||||
}
|
||||
|
||||
function serviceJson(input: ServiceInput) {
|
||||
return {
|
||||
gameId: Number(input.gameId),
|
||||
title: input.title,
|
||||
description: input.description,
|
||||
price: input.price,
|
||||
unit: input.unit,
|
||||
rankRange: input.rankRange,
|
||||
availability: input.availability,
|
||||
}
|
||||
}
|
||||
|
||||
export async function listServices(): Promise<PlayerService[]> {
|
||||
const res = await httpJson<unknown>("/api/v1/services", { cache: "no-store" })
|
||||
if (typeof res === "object" && res !== null && "items" in res) {
|
||||
return (res as Paginated<PlayerService>).items
|
||||
return itemsFrom(res as Paginated<PlayerService>)
|
||||
}
|
||||
return res as PlayerService[]
|
||||
}
|
||||
@@ -44,7 +71,27 @@ export async function listServicesByPlayer(playerId: string): Promise<PlayerServ
|
||||
},
|
||||
)
|
||||
if (typeof res === "object" && res !== null && "items" in res) {
|
||||
return (res as Paginated<PlayerService>).items
|
||||
return itemsFrom(res as Paginated<PlayerService>)
|
||||
}
|
||||
return res as PlayerService[]
|
||||
return itemsFrom(res)
|
||||
}
|
||||
|
||||
export async function createPlayerService(input: ServiceInput): Promise<PlayerService> {
|
||||
return httpJson<PlayerService>("/api/v1/services", {
|
||||
method: "POST",
|
||||
json: serviceJson(input),
|
||||
})
|
||||
}
|
||||
|
||||
export async function updatePlayerService(serviceId: string, input: ServiceInput): Promise<void> {
|
||||
await httpJson<unknown>(`/api/v1/services/${encodeURIComponent(serviceId)}`, {
|
||||
method: "PUT",
|
||||
json: serviceJson(input),
|
||||
})
|
||||
}
|
||||
|
||||
export async function deletePlayerService(serviceId: string): Promise<void> {
|
||||
await httpJson<unknown>(`/api/v1/services/${encodeURIComponent(serviceId)}`, {
|
||||
method: "DELETE",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user