feat: 支持删除

This commit is contained in:
sealday 2024-04-06 23:32:31 +08:00
parent 64392b307b
commit a3814b8adf
3 changed files with 69 additions and 31 deletions

View File

@ -4,6 +4,7 @@ import {
InternetProviderLyricSearchResponse, InternetProviderLyricSearchResponse,
getSearchResults, getSearchResults,
} from "@/lib/actions/lyrics"; } from "@/lib/actions/lyrics";
import { revalidateTag } from "next/cache";
import { LyricSource } from "@/lib/types"; import { LyricSource } from "@/lib/types";
import { cookies } from "next/headers"; import { cookies } from "next/headers";
@ -23,7 +24,7 @@ export async function searchSongs(
export async function addSong( export async function addSong(
_: unknown, _: unknown,
item: InternetProviderLyricSearchResponse item: InternetProviderLyricSearchResponse
): Promise<unknown> { ) {
const token = cookies().get("token"); const token = cookies().get("token");
await fetch("https://tachy.daoyoucloud.com/api/songs:create", { await fetch("https://tachy.daoyoucloud.com/api/songs:create", {
method: "POST", method: "POST",
@ -40,37 +41,51 @@ export async function addSong(
source_id: item.id, source_id: item.id,
}), }),
}); });
const params = new URLSearchParams(); revalidateTag("songs");
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",
},
});
const result = await songs.json();
console.log(result.data);
return result?.data;
} }
export async function listSongs(): Promise<any[]> { export async function listSongs(): Promise<any[]> {
const token = cookies().get("token"); const token = cookies().get("token");
const params = new URLSearchParams(); const params = new URLSearchParams();
params.append('pageSize', '99999'); params.append("pageSize", "99999");
params.append('filter', JSON.stringify({"$and":[{"createdBy":{"id":{"$eq":"{{$user.id}}"}}}]})) params.append(
const songs = await fetch("https://tachy.daoyoucloud.com/api/songs:list?" + params.toString(), { "filter",
method: "GET", JSON.stringify({ $and: [{ createdBy: { id: { $eq: "{{$user.id}}" } } }] })
headers: { );
"X-App": "danmu-sim", const songs = await fetch(
"X-Authenticator": "basic", "https://tachy.daoyoucloud.com/api/songs:list?" + params.toString(),
authorization: "Bearer " + token?.value, {
"content-type": "application/json", 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(); const result = await songs.json();
return result?.data ?? []; return result?.data ?? [];
} }
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");
}

View File

@ -0,0 +1,21 @@
"use client";
import { deleteSong } from "./actions";
export default function DeleteSongButton({
className,
id,
}: {
className?: string | undefined;
id: number;
}) {
return (
<button
className={className}
onClick={() => {
deleteSong(id);
}}
>
</button>
);
}

View File

@ -1,4 +1,5 @@
import { listSongs } from "./actions"; import { deleteSong, listSongs } from "./actions";
import DeleteSongButton from "./delete-song-button";
import SearchForm from "./search-form"; import SearchForm from "./search-form";
export default async function Page() { export default async function Page() {
@ -18,9 +19,10 @@ export default async function Page() {
<span> <span>
{song.name} - {song.artist} {song.name} - {song.artist}
</span> </span>
<button className="text-red-500 hover:text-red-700"> <DeleteSongButton
className="text-red-500 hover:text-red-700"
</button> id={song.id}
/>
</li> </li>
))} ))}
</ul> </ul>