From 31a281277e35b781ecdc355007e239945bad6782 Mon Sep 17 00:00:00 2001 From: Saumya Date: Thu, 5 Oct 2023 21:59:01 +0530 Subject: [PATCH] clan and user db schemas and clan addition --- client/pages/api/admin/auction/clan.ts | 40 ++++++++++++ client/pages/api/auth/[...nextauth]/index.ts | 2 +- client/pages/clan/add-clan.tsx | 66 ++++++++++++++++++++ client/util/types.ts | 16 ++++- 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 client/pages/api/admin/auction/clan.ts create mode 100644 client/pages/clan/add-clan.tsx diff --git a/client/pages/api/admin/auction/clan.ts b/client/pages/api/admin/auction/clan.ts new file mode 100644 index 0000000..95a8cd4 --- /dev/null +++ b/client/pages/api/admin/auction/clan.ts @@ -0,0 +1,40 @@ +import type { NextApiRequest, NextApiResponse } from "next"; +import { ClanCol } from "@/util/types"; +import { clientPromise } from "@/util/DB"; +import { getServerSession } from "next-auth/next" + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + + if (req.method === "GET") { + return GET(req, res); + } else if (req.method === "POST") { + return POST(req, res); + } else { + return res.status(400).send(`Method ${req.method} is not supported!`); + } +} + +async function GET(req: NextApiRequest, res: NextApiResponse) { + const db = (await clientPromise).db("coc-portal"); + const clans = db.collection("Clans"); + + const fields = await clans.find(); + return res.json(fields); +} + +async function POST(req: NextApiRequest, res: NextApiResponse ) { + const {name,amount,admin} = req.body; + const db = (await clientPromise).db("coc-portal"); + const clans = db.collection("Clans"); + + await clans.insertOne({ + name:name, + balance:amount, + admin:admin, + }) + + return res.status(200).json({message:"team saved"}); +} \ No newline at end of file diff --git a/client/pages/api/auth/[...nextauth]/index.ts b/client/pages/api/auth/[...nextauth]/index.ts index e7acfde..afdf02c 100644 --- a/client/pages/api/auth/[...nextauth]/index.ts +++ b/client/pages/api/auth/[...nextauth]/index.ts @@ -46,4 +46,4 @@ const handler = NextAuth({ }, }); -export default handler; +export default handler; \ No newline at end of file diff --git a/client/pages/clan/add-clan.tsx b/client/pages/clan/add-clan.tsx new file mode 100644 index 0000000..67ed32a --- /dev/null +++ b/client/pages/clan/add-clan.tsx @@ -0,0 +1,66 @@ +'use client' +import React, { useState } from "react"; + +const Clan = () => { + const [name, setName] = useState(""); + const [amount, setAmount] = useState(""); + const [admin, setAdmin] = useState(""); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + + try { + const response = await fetch('/api/admin/auction/clan', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + name, + amount, + admin, + }), + }); + + if (response.status === 200) { + setName(""); + setAmount(""); + setAdmin(""); + } + } catch (error) { + console.error("Error:", error); + } + }; + + return ( +
+
+ setName(e.target.value)} + /> + setAmount(e.target.value)} + /> + setAdmin(e.target.value)} + /> +
+ +
+
+
+ ); +}; + +export default Clan; \ No newline at end of file diff --git a/client/util/types.ts b/client/util/types.ts index aaeae2a..c128c09 100644 --- a/client/util/types.ts +++ b/client/util/types.ts @@ -3,7 +3,21 @@ import { ObjectId } from "mongodb"; export type UserCol = { name: string; email: string; - + rank?:number; + clan?:string; + soldPrice?:number + qualifOne?:number; + qualifTwo?:number; role: "Admin" | "Leader" | "Co-leader" | "Elder" | "Member" | null; _id?: ObjectId; }; + +export type ClanCol = { + name:string; + balance:string; + members?:ObjectId[]; + matchScores?:{[matchName:string]: number}; + admin:string; + adminId?:string | ObjectId; + _id?:ObjectId; +} \ No newline at end of file