Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add participant numbers #92

Merged
merged 3 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/api/teams/get-request/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import dbConnect from "../../../../lib/dbConnect";
import { TeamModel } from "../../../../model/Team";

export async function GET(req) {
await dbConnect();
const { teamId } = req.query;

try {
const team = await TeamModel.findById(teamId).populate('requests', 'username');

if (!team) {
return new Response(JSON.stringify({ success: false, message: 'Team not found' }), { status: 404 });
}

return new Response(JSON.stringify({ success: true, requests: team.requests }), { status: 200 });
} catch (error) {
console.error('Error fetching requests:', error);
return new Response(JSON.stringify({ success: false, message: 'Error fetching requests' }), { status: 500 });
}
}
29 changes: 29 additions & 0 deletions app/api/teams/send-request/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import dbConnect from "../../../../lib/dbConnect";
import { TeamModel } from "../../../../model/Team";
import { UserModel } from "../../../../model/User";

export async function POST(req) {
await dbConnect();
const { teamId, userId } = await req.json();

try {
const team = await TeamModel.findById(teamId);
const user = await UserModel.findById(userId);

if (!team || !user) {
return new Response(JSON.stringify({ success: false, message: 'Team or User not found' }), { status: 404 });
}

if (team.requests.includes(userId)) {
return new Response(JSON.stringify({ success: false, message: 'Request already sent' }), { status: 400 });
}

team.requests.push(userId);
await team.save();

return new Response(JSON.stringify({ success: true, message: 'Request sent successfully' }), { status: 200 });
} catch (error) {
console.error('Error sending request:', error);
return new Response(JSON.stringify({ success: false, message: 'Error sending request' }), { status: 500 });
}
}
Loading
Loading