Skip to content

Commit

Permalink
update tating
Browse files Browse the repository at this point in the history
  • Loading branch information
satanakorn committed Nov 27, 2024
2 parents a5ec4f0 + 43ac989 commit b9ece08
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 130 deletions.
127 changes: 112 additions & 15 deletions frontend/src/eventdetail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,74 @@ const EventDetail = () => {
useEffect(() => {
console.log('member:', member);
}, [member]);

const checkteam = async (TeamID) => {
try {
const responsed = await Axios.get(`http://localhost:3000/team/finduserteam/${TeamID}`);
const team = responsed.data;
const currentUser = team.find(member => member.UserID === UserID);
if (currentUser) {
Swal.fire({
title: "Sorry",
text: "You are in the team !",
icon: "error"
});
setTimeout(() => {
Swal.close();
}, 3000);

await new Promise(resolve => setTimeout(resolve, 2000));

window.location.href = `/EventDetail/${HackathonID}`;}
} catch (error) {
console.error("Error deleting from team:", error);
}
}


const deleteteam = async (TeamID) => {
try {
const responsed = await Axios.get(`http://localhost:3000/team/finduserteam/${TeamID}`);
const team = responsed.data;
const currentUser = team.find(member => member.UserID === UserID);
if (currentUser) {
setCurrentUserRole(currentUser.role)}

if (currentUserRole === 'head'){
const response = await Axios.delete(`http://localhost:3000/team/delete/${TeamID}`,TeamID);
console.log('Delete response:', response.data);
Swal.fire({
title: "Congratulation",
text: "Delete team success !",
icon: "success"
});
setTimeout(() => {
Swal.close();
}, 3000);

await new Promise(resolve => setTimeout(resolve, 2000));

window.location.href = `/EventDetail/${HackathonID}`;
}
else{
Swal.fire({
title: "Sorry",
text: "You don't have permission to delete team !",
icon: "error"
});
setTimeout(() => {
Swal.close();
}, 3000);

await new Promise(resolve => setTimeout(resolve, 2000));

window.location.href = `/EventDetail/${HackathonID}`;
}} catch (error) {
console.error("Error deleting from team:", error);
}
}

//delete members from team
const deletefromteam = async (TeamID, UserID) => {
try {
const response = await Axios.delete(`http://localhost:3000/team/removeMember`, {
Expand All @@ -43,9 +110,9 @@ const EventDetail = () => {
});
setTimeout(() => {
Swal.close();
}, 3000);
}, 1000);

await new Promise(resolve => setTimeout(resolve, 2000));
await new Promise(resolve => setTimeout(resolve, 1000));

window.location.href = `/EventDetail/${HackathonID}`;
} catch (error) {
Expand Down Expand Up @@ -99,6 +166,19 @@ const EventDetail = () => {
window.location.href = `/EventDetail/${HackathonID}`;
}
else{

const payloadt = {
teamID: TeamID,
userID: UserID,
role: role
};
console.log('Payload:', payloadt);
await Axios.post('http://localhost:3000/team/addMember', payloadt);
setuserTeamlist((prev) => [
...prev,
{ teamID: TeamID, userID: UserID, role: role },
]);

Swal.fire({
title: "Good job!",
text: "Join team Sucessful!",
Expand All @@ -112,18 +192,6 @@ const EventDetail = () => {

// localStorage.setItem('UserID', JSON.stringify(response.data.UserID));
window.location.href = `/EventDetail/${HackathonID}`;

const payloadt = {
teamID: TeamID,
userID: UserID,
role: role
};
console.log('Payload:', payloadt);
await Axios.post('http://localhost:3000/team/addMember', payloadt);
setuserTeamlist((prev) => [
...prev,
{ teamID: TeamID, userID: UserID, role: role },
]);
}
} catch (error) {
console.error("Error joining team:", error);
Expand Down Expand Up @@ -166,6 +234,18 @@ const addteam = async () => {
]);

console.log('Team created successfully');
Swal.fire({
title: "Congratulation",
text: "Create team success !",
icon: "success"
});
setTimeout(() => {
Swal.close();
}, 3000);

await new Promise(resolve => setTimeout(resolve, 2000));

window.location.href = `/EventDetail/${HackathonID}`;
} catch (error) {
console.error('Error creating team:', error.response?.data || error.message);
}
Expand Down Expand Up @@ -264,6 +344,19 @@ const addteam = async () => {
<span>👥 {val.CurrentMember}/{val.MaxMember}</span>
</div>
<h3 className="text-xl font-semibold text-gray-700 mt-8">Team {val.TeamName}</h3>


<div className="absolute top-4 right-4">
<button
onClick={() => deleteteam(val.TeamID)}
className="ml-2 bg-red-500/80 text-white rounded-full p-1.5
hover:bg-red-600 transition-all duration-300 transform hover:rotate-180"
>
</button>
</div>


<div className="absolute bottom-4 right-4 flex space-x-2">
<button
onClick={async () => {
Expand All @@ -274,8 +367,12 @@ const addteam = async () => {
>
Delete Member
</button>

<button
onClick={() => setIsjoinTeamModalOpen([true, val.TeamID, val.CurrentMember, val.MaxMember])}
onClick={async () => {
await setIsjoinTeamModalOpen([true, val.TeamID, val.CurrentMember, val.MaxMember])
checkteam(val.TeamID)
}}
className="px-3 py-1 bg-gradient-to-r from-blue-400 to-blue-500 text-white font-semibold rounded-full shadow-md hover:from-blue-500 hover:to-blue-600 transition-transform transform hover:-translate-y-1 hover:scale-105"
>
Join Team
Expand Down
190 changes: 75 additions & 115 deletions src/controllers/teamController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,92 @@ export const teamController = new Elysia({ prefix: "/team" })
return teams;
})

// Get users in a team
.get("/users/:teamID", async ({ params: { teamID }, error }) => {
const users = await prisma.userTeam.findMany({
where: {
TeamID: parseInt(teamID, 10),
},
include: {
User: true,
},
});

if (!users || users.length === 0) {
return error(404, "Users not found");
}

return users.map(userTeam => userTeam.User);
}, {
params: t.Object({
teamID: t.String(),
}),
})

// Get Team by TeamID
.get("/getTeam", async ({ query: { TeamID }, error }) => {
const team = await prisma.team.findFirst({
where: {
TeamID: TeamID,
},
});




























if (!team) {
return error(404, "Team not found");
}

return team;
}, {
query: t.Object({
TeamID: t.Number(),
}),
})

// Check if a user is part of a team
.get("/checkteam", async ({ query: { TeamID, UserID }, error }) => {
if (!TeamID || !UserID) {
return error(400, "TeamID and UserID are required");
}

const teamMember = await prisma.userTeam.findFirst({
where: {
TeamID: TeamID,
UserID: UserID,
},
});

if (!teamMember) {
return error(404, "User is not part of the team");
}

return { valid: true };
}, {
query: t.Object({
TeamID: t.Number(),
UserID: t.String(),
}),
})

// Other endpoints...
// Get TeamID from TeamName
.get("/getTeamID", async ({ query: { TeamName }, error }) => {
const team = await prisma.team.findFirst({
where: {
TeamName: TeamName,
},
});

if (!team) {
return error(404, "Team not found");
}

return {TeamID: team.TeamID};
}, {
query: t.Object({
TeamName: t.String(),
}),
})

.get("/finduserteam/:teamID", async ({ params, error }) => {
const teamID = parseInt(params.teamID, 10); // Ensure teamID is a number
if (isNaN(teamID)) {
Expand Down Expand Up @@ -260,91 +307,4 @@ export const teamController = new Elysia({ prefix: "/team" })
teamID: t.Number(),
userID: t.String(),
}),
})


// Get users in a team
.get("/users/:teamID", async ({ params: { teamID }, error }) => {
const users = await prisma.userTeam.findMany({
where: {
TeamID: parseInt(teamID, 10),
},
include: {
User: true,
},
});

if (!users || users.length === 0) {
return error(404, "Users not found");
}

return users.map(userTeam => userTeam.User);
}, {
params: t.Object({
teamID: t.String(),
}),
})

// Get Team by TeamID
.get("/getTeam", async ({ query: { TeamID }, error }) => {
const team = await prisma.team.findFirst({
where: {
TeamID: TeamID,
},
});

if (!team) {
return error(404, "Team not found");
}

return team;
}, {
query: t.Object({
TeamID: t.Number(),
}),
})

// Check if a user is part of a team
.get("/checkteam", async ({ query: { TeamID, UserID }, error }) => {
if (!TeamID || !UserID) {
return error(400, "TeamID and UserID are required");
}

const teamMember = await prisma.userTeam.findFirst({
where: {
TeamID: TeamID,
UserID: UserID,
},
});

if (!teamMember) {
return error(404, "User is not part of the team");
}

return { valid: true };
}, {
query: t.Object({
TeamID: t.Number(),
UserID: t.String(),
}),
})

// Other endpoints...
// Get TeamID from TeamName
.get("/getTeamID", async ({ query: { TeamName }, error }) => {
const team = await prisma.team.findFirst({
where: {
TeamName: TeamName,
},
});

if (!team) {
return error(404, "Team not found");
}

return {TeamID: team.TeamID};
}, {
query: t.Object({
TeamName: t.String(),
}),
})
})

0 comments on commit b9ece08

Please sign in to comment.