diff --git a/app/api/streams/next/route.ts b/app/api/streams/next/route.ts index f78fcbe..7d3d2ee 100644 --- a/app/api/streams/next/route.ts +++ b/app/api/streams/next/route.ts @@ -1,6 +1,15 @@ +import { RPC_URI } from "@/app/lib/constants"; import { prismaClient } from "@/app/lib/db"; +import { Connection } from "@solana/web3.js"; import { getServerSession } from "next-auth"; -import { NextResponse } from "next/server"; +import { NextRequest, NextResponse } from "next/server"; +import { z } from "zod"; + + +const playNextSchema = z.object({ + streamId: z.string().uuid(), + txnSig: z.string(), +}) export async function GET() { const session = await getServerSession(); @@ -60,4 +69,84 @@ export async function GET() { stream: mostUpvotedStream }) +} + + +export async function POST(req:NextRequest){ + + const session = await getServerSession(); + + const user = await prismaClient.user.findFirst({ + where: { + email: session?.user?.email ?? "" + } + }); + + if (!user) { + return NextResponse.json({ + message: "Unauthenticated" + }, { + status: 403 + }) + } + + const data = await req.json(); + + const {streamId,txnSig} = playNextSchema.parse(data); + + const connection = new Connection(RPC_URI); + + const txResponse = await connection.getTransaction(txnSig,{ maxSupportedTransactionVersion: 0, commitment:"confirmed" }); + + if(!txResponse){ + return NextResponse.json({ + message:"Enter Valid Transaction Signature !" + },{ + status:402, + }) + } + + const nextStream = await prismaClient.stream.findUnique({ + where:{ + id:streamId, + userId:user.id, + } + }) + + if (!nextStream) { + return NextResponse.json({ + message: "Enter Valid Stream Id !" + }, { + status: 404, + }) + } + + + await prismaClient.currentStream.delete({where:{userId:user.id}}); + + await Promise.all([prismaClient.currentStream.upsert({ + where: { + streamId : nextStream.id, + userId:user.id + }, + update: { + streamId : nextStream.id, + }, + create: { + userId: user.id, + streamId : nextStream.id, + }, + }),prismaClient.stream.update({ + where: { + id : nextStream.id, + }, + data: { + played: true, + playedTs: new Date() + } + })]) + + return NextResponse.json({ + stream: nextStream + }) } \ No newline at end of file diff --git a/app/components/Appbar.tsx b/app/components/Appbar.tsx index 957f26b..cba2dab 100644 --- a/app/components/Appbar.tsx +++ b/app/components/Appbar.tsx @@ -5,15 +5,26 @@ import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" //@ts-ignore import { Music } from "lucide-react" +import { + WalletDisconnectButton, + WalletMultiButton +} from '@solana/wallet-adapter-react-ui'; +import { useWallet } from "@solana/wallet-adapter-react"; export function Appbar() { + const session = useSession(); + const {wallet} = useWallet(); return
No video playing
)} {playVideo && ( -