Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #22 from Programming-Club-Ahmedabad-University/auc…
Browse files Browse the repository at this point in the history
…tionPart

clan and user db schemas and methods to add and get clans
  • Loading branch information
Codesmith28 authored Oct 5, 2023
2 parents d539ada + 31a2812 commit 8882016
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
40 changes: 40 additions & 0 deletions client/pages/api/admin/auction/clan.ts
Original file line number Diff line number Diff line change
@@ -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<ClanCol>("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<ClanCol>("Clans");

await clans.insertOne({
name:name,
balance:amount,
admin:admin,
})

return res.status(200).json({message:"team saved"});
}
2 changes: 1 addition & 1 deletion client/pages/api/auth/[...nextauth]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ const handler = NextAuth({
},
});

export default handler;
export default handler;
66 changes: 66 additions & 0 deletions client/pages/clan/add-clan.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<form
style={{ display: 'flex', justifyContent: 'center', flexDirection: "column", gap: "10px" }}
>
<input
type="text"
placeholder="Enter clan name..."
value={name}
onChange={(e) => setName(e.target.value)}
/>
<input
type="number"
placeholder="Enter budget"
value={amount}
onChange={(e) => setAmount(e.target.value)}
/>
<input
type="text"
placeholder="clan admin registered e-mail id"
value={admin}
onChange={(e) => setAdmin(e.target.value)}
/>
<div style={{ display: 'flex', justifyContent: 'center', alignItems: "center" }}>
<button onClick={(e) => handleSubmit(e)}>Add</button>
</div>
</form>
</div>
);
};

export default Clan;
16 changes: 15 additions & 1 deletion client/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 8882016

Please sign in to comment.