From ef59e67451a6d67b6a76c2f9126b99526215df22 Mon Sep 17 00:00:00 2001 From: Cypro Freelance <110410268+Proxyy587@users.noreply.github.com> Date: Thu, 1 Aug 2024 00:21:28 +0530 Subject: [PATCH] fixed ig? --- .../admin/vulnerabilities/mods/page.tsx | 276 ------------------ app/api/delete/vulnerability/[id]/route.ts | 28 +- app/api/delete/vulnerability/mods/route.ts | 32 -- 3 files changed, 18 insertions(+), 318 deletions(-) delete mode 100644 app/(auth)/admin/vulnerabilities/mods/page.tsx delete mode 100644 app/api/delete/vulnerability/mods/route.ts diff --git a/app/(auth)/admin/vulnerabilities/mods/page.tsx b/app/(auth)/admin/vulnerabilities/mods/page.tsx deleted file mode 100644 index 0763745..0000000 --- a/app/(auth)/admin/vulnerabilities/mods/page.tsx +++ /dev/null @@ -1,276 +0,0 @@ -"use client"; -import React, { useEffect, useState } from "react"; -import { useForm, SubmitHandler, useFieldArray } from "react-hook-form"; -import { zodResolver } from "@hookform/resolvers/zod"; -import { Button } from "@/components/ui/button"; -import { - Form, - FormControl, - FormField, - FormItem, - FormLabel, - FormMessage, -} from "@/components/ui/form"; -import { - Table, - TableBody, - TableCell, - TableHead, - TableHeader, - TableRow, -} from "@/components/ui/table"; -import { Input } from "@/components/ui/input"; -import { - Select, - SelectTrigger, - SelectContent, - SelectItem, - SelectValue, -} from "@/components/ui/select"; -import { z } from "zod"; -import { useToast } from "@/components/ui/use-toast"; -import { - ModsVulnerability, - vulnerabilitiesSchema, -} from "@/lib/validations/validation"; - -interface VulnerabilityEntry { - _id: string; - title: string; - category: string; - bullets: { point: string }[]; -} - -type VulnerabilitiesForm = z.infer; - -const AdminLogPage = () => { - const [logs, setLogs] = useState([]); - const [categories, setCategories] = useState< - { title: string; _id: string }[] - >([]); - const [selectedCategory, setSelectedCategory] = useState(""); - const [error, setError] = useState(""); - const { toast } = useToast(); - const [loading, setLoading] = useState(false); - - const form = useForm({ - resolver: zodResolver(vulnerabilitiesSchema), - defaultValues: { - title: "", - category: "", - bullets: [{ point: "" }], - }, - }); - - const { fields, append, remove } = useFieldArray({ - control: form.control, - name: "bullets", - }); - - const fetchLogs = async () => { - try { - const response = await fetch("/api/vulnerabilities", { method: "GET" }); - if (response.ok) { - const data: VulnerabilityEntry[] = await response.json(); - setLogs(data); - } else { - throw new Error(`HTTP error! status: ${response.status}`); - } - } catch (error: any) { - setError(error.message || "Failed to fetch logs"); - } - }; - - const fetchCategories = async () => { - try { - const response = await fetch("/api/mdx/pages", { method: "GET" }); - if (response.ok) { - const data = await response.json(); - setCategories(data); - } else { - throw new Error(`HTTP error! status: ${response.status}`); - } - } catch (error: any) { - setError(error.message || "Failed to fetch categories"); - } - }; - - const onSubmit: SubmitHandler = async (data) => { - setLoading(true); - const response = await fetch("/api/vulnerability/mods", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - ...data, - category: selectedCategory, - }), - }); - - if (response.ok) { - form.reset(); - fetchLogs(); - setLoading(false); - toast({ description: "Logs successfully added" }); - } else { - setLoading(false); - toast({ description: "Upload Failed", variant: "destructive" }); - } - }; - - const deleteLog = async (id: string) => { - try { - const response = await fetch(`/api/delete/vulnerability/${id}`, { - method: "DELETE", - }); - if (response.ok) { - fetchLogs(); - } else { - throw new Error(`HTTP error! status: ${response.status}`); - } - } catch (error: any) { - setError(error.message || "Failed to delete log"); - } - }; - - useEffect(() => { - fetchLogs(); - fetchCategories(); - const interval = setInterval(() => { - fetchLogs(); - }, 10000); - - return () => clearInterval(interval); - }, []); - - return ( -
-

Server Vulnerabilities Form

-
- - ( - - Title - - - - - - )} - /> - - ( - - Category - - - )} - /> - - {fields.map((field, index) => ( - ( - - Bullet Point {index + 1} - - - - - - - )} - /> - ))} - - - - - - {/* Section to list and delete logs */} -
-

- Existing Vulnerabilities -

- {error &&

{error}

} - - - - Title - Category - Actions - - - - {logs - .slice() - .reverse() - .map((log) => ( - - - {log.title} - - - {log.category} - - - - - - ))} - -
-
-
- ); -}; - -export default AdminLogPage; diff --git a/app/api/delete/vulnerability/[id]/route.ts b/app/api/delete/vulnerability/[id]/route.ts index a4018ac..3e1d4cc 100644 --- a/app/api/delete/vulnerability/[id]/route.ts +++ b/app/api/delete/vulnerability/[id]/route.ts @@ -1,4 +1,3 @@ -// app/api/delete/[id]/route.ts import clientPromise from "@/lib/db"; import { ObjectId } from "mongodb"; import { NextResponse } from "next/server"; @@ -7,23 +6,32 @@ export async function DELETE( request: Request, { params }: { params: { id: string } } ) { + const client = await clientPromise; + const db = client.db(); const { id } = params; + if (!id) { + return NextResponse.json({ message: "ID is required" }, { status: 400 }); + } + try { - const client = await clientPromise; - const db = client.db("downloadsDatabase"); - const collection = db.collection("vulnerabilities"); - - const result = await collection.deleteOne({ _id: new ObjectId(id) }); - + const result = await db + .collection("vulnerabilities") + .deleteOne({ _id: new ObjectId(id) }); if (result.deletedCount === 1) { - return NextResponse.json({ message: "Log deleted successfully" }); + return NextResponse.json( + { message: "Vulnerability deleted successfully" }, + { status: 200 } + ); } else { - return NextResponse.json({ message: "Log not found" }, { status: 404 }); + return NextResponse.json( + { message: "Vulnerability not found" }, + { status: 404 } + ); } } catch (error) { return NextResponse.json( - { message: "Failed to delete log", error: error }, + { message: "Failed to delete vulnerability" }, { status: 500 } ); } diff --git a/app/api/delete/vulnerability/mods/route.ts b/app/api/delete/vulnerability/mods/route.ts deleted file mode 100644 index bc0a681..0000000 --- a/app/api/delete/vulnerability/mods/route.ts +++ /dev/null @@ -1,32 +0,0 @@ -import clientPromise from "@/lib/db"; -import type { NextApiRequest, NextApiResponse } from "next"; - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse -) { - if (req.method === "POST") { - try { - const client = await clientPromise; - const db = client.db("modsVuln"); - const { title, category, bullets } = req.body; - - if (!title || !category || !bullets || !Array.isArray(bullets)) { - return res.status(400).json({ error: "Invalid data" }); - } - - await db.collection("vulnerabilities").insertOne({ - title, - category, - bullets, - }); - - res.status(200).json({ message: "Vulnerability added successfully" }); - } catch (error) { - res.status(500).json({ error: error || "Failed to add vulnerability" }); - } - } else { - res.setHeader("Allow", ["POST"]); - res.status(405).end(`Method ${req.method} Not Allowed`); - } -}