feat(services): manage services through backend

This commit is contained in:
zetaloop
2026-04-25 14:31:04 +08:00
parent 074ad8f35b
commit e559204347
4 changed files with 236 additions and 71 deletions
+8 -1
View File
@@ -13,7 +13,14 @@ export { getOrderById, listOrders, listOrdersByConsumer } from "./orders"
export { getPlayerById, listPlayers, listPlayersByShop } from "./players"
export { getPostById, listPosts, listPostsByAuthor, togglePostLike } from "./posts"
export { listReviews, listReviewsByOrder, listReviewsByTargetUser } from "./reviews"
export { getServiceById, listServices, listServicesByPlayer } from "./services"
export {
createPlayerService,
deletePlayerService,
getServiceById,
listServices,
listServicesByPlayer,
updatePlayerService,
} from "./services"
export { getShopById, getShopByOwnerId, listShops } from "./shops"
export {
getWalletBalance,
+51 -4
View File
@@ -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",
})
}