danmu-sim/app/songs/actions.tsx

92 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-04-06 18:08:51 +08:00
"use server";
import {
InternetProviderLyricSearchResponse,
getSearchResults,
} from "@/lib/actions/lyrics";
2024-04-06 23:32:31 +08:00
import { revalidateTag } from "next/cache";
2024-04-06 18:08:51 +08:00
import { LyricSource } from "@/lib/types";
import { cookies } from "next/headers";
export async function searchSongs(
_currentState: InternetProviderLyricSearchResponse[],
formData: FormData
): Promise<InternetProviderLyricSearchResponse[]> {
const list = await getSearchResults({
name: formData.get("name") as string,
});
if (list) {
return list;
}
return [];
}
2024-04-06 18:08:51 +08:00
export async function addSong(
_: unknown,
item: InternetProviderLyricSearchResponse
2024-04-06 23:32:31 +08:00
) {
2024-04-06 18:08:51 +08:00
const token = cookies().get("token");
await fetch("https://tachy.daoyoucloud.com/api/songs:create", {
method: "POST",
headers: {
"X-App": "danmu-sim",
"X-Authenticator": "basic",
authorization: "Bearer " + token?.value,
"content-type": "application/json",
},
body: JSON.stringify({
artist: item.artist,
name: item.name,
source: LyricSource.NETEASE,
source_id: item.id,
}),
});
2024-04-06 23:32:31 +08:00
revalidateTag("songs");
2024-04-06 18:08:51 +08:00
}
export async function listSongs(): Promise<any[]> {
const token = cookies().get("token");
const params = new URLSearchParams();
2024-04-06 23:32:31 +08:00
params.append("pageSize", "99999");
params.append(
"filter",
JSON.stringify({ $and: [{ createdBy: { id: { $eq: "{{$user.id}}" } } }] })
);
const songs = await fetch(
"https://tachy.daoyoucloud.com/api/songs:list?" + params.toString(),
{
method: "GET",
headers: {
"X-App": "danmu-sim",
"X-Authenticator": "basic",
authorization: "Bearer " + token?.value,
"content-type": "application/json",
},
next: {
tags: ["songs"],
},
}
);
const result = await songs.json();
return result?.data ?? [];
}
2024-04-06 23:32:31 +08:00
export async function deleteSong(id: number): Promise<void> {
const token = cookies().get("token");
const params = new URLSearchParams();
params.append('filterByTk', id.toString());
await fetch(
"https://tachy.daoyoucloud.com/api/songs:destroy?" + params.toString(),
{
method: "GET",
headers: {
"X-App": "danmu-sim",
"X-Authenticator": "basic",
authorization: "Bearer " + token?.value,
"content-type": "application/json",
},
}
);
revalidateTag("songs");
}