Skip to content

Commit

Permalink
update rating
Browse files Browse the repository at this point in the history
  • Loading branch information
satanakorn committed Nov 27, 2024
1 parent d12b0d2 commit 0c457d5
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 84 deletions.
87 changes: 50 additions & 37 deletions frontend/src/Rating.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const Rating = () => {
console.error('Error fetching users:', error);
}
}
console.log(selectedTeam)
};

fetchUsers();
Expand All @@ -58,9 +59,9 @@ const Rating = () => {
return;
}

// ดึง TeamID จาก TeamName
const teamResponse = await Axios.get(`http://localhost:3000/team/getTeamID`, {
params: { TeamName: selectedTeam },
// Fetch TeamID from TeamName
const teamResponse = await Axios.get(`http://localhost:3000/team/getTeam`, {
params: { TeamID: selectedTeam },
});

const TeamID = teamResponse.data.TeamID;
Expand All @@ -70,54 +71,66 @@ const Rating = () => {
return;
}

// ดึง UserID จาก Username
const userResponse = await Axios.get(`http://localhost:3000/user/getUserID`, {
params: { Username: selectedUser },
});
// // Store TeamID in local storage
// localStorage.setItem('TeamID', TeamID);

// Fetch UserID from Username
const userResponse = await Axios.get(`http://localhost:3000/user/getUserID/${selectedUser}`);

const ratedUserID = userResponse.data.userID;
console.log(userResponse.data);


const ratedUserID = userResponse.data.UserID;

if (!ratedUserID) {
setErrorMessage('ไม่พบ UserID ของ Username นี้');
return;
}

// ตรวจสอบว่าคนให้คะแนนอยู่ในทีมเดียวกันคนถูกให้คะแนนมั้ย
const checkTeam = await Axios.post(`http://localhost:3000/team/checkteam`, {
Teamname: selectedTeam,
userID: ratedUserID, // ตรวจสอบ UserID ในทีม
// Check if the user is part of the team
const checkTeam = await Axios.get(`http://localhost:3000/team/checkteam`, {
params: {
TeamID: TeamID,
UserID: ratedUserID,
},
});

if (!checkTeam.data.valid) {
setErrorMessage('คุณไม่ได้อยู่ทีมเดียวกัน กรุณาลองใหม่อีกครั้ง');
return;
}

// เพิ่มข้อมูลลงในตาราง userteam
await Axios.post(`http://localhost:3000/rating/rateuser`, {
ratedByID: UserID,
ratedUserID,
RatingValue: RatingValue,
Comment,
},
{
headers: {
Authorization: `Bearer ${localStorage.getItem('token')}`,
console.log(UserID)
console.log(ratedUserID)
console.log(RatingValue)
console.log(Comment)

// Add rating
// Add rating
await Axios.post(`http://localhost:3000/rating/rateuser`, {
ratedByID: UserID,
ratedUserID: ratedUserID,
ratingValue: RatingValue,
comment: Comment,
},
}
);

// รีเซ็ตฟอร์ม
setSelectedTeam('');
setSelectedUser('');
setRatingValue('');
setComment('');
setErrorMessage('');
alert('บันทึกคะแนนสำเร็จ');
} catch (error) {
console.error('Error:', error);
setErrorMessage('เกิดข้อผิดพลาดในการบันทึกข้อมูล');
}
{
headers: {
Authorization: `Bearer ${localStorage.getItem('token')}`,
},
}
);

// Reset form
setSelectedTeam('');
setSelectedUser('');
setRatingValue('');
setComment('');
setErrorMessage('');
alert('บันทึกคะแนนสำเร็จ');
} catch (error) {
console.error('Error:', error);
setErrorMessage('เกิดข้อผิดพลาดในการบันทึกข้อมูล');
}
};

return (
Expand Down Expand Up @@ -148,7 +161,7 @@ const Rating = () => {
>
<option value="" disabled>Select User</option>
{users.map((user) => (
<option key={user.UserID} value={user.UserName}>
<option key={user.UserID} value={user.UserID}>
{user.UserName}
</option>
))}
Expand Down
7 changes: 5 additions & 2 deletions src/controllers/ratingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export const ratingController = new Elysia({ prefix: "/rating" })
}),
})

// Rating to someone

// Rate someone
.post("/rateuser", async ({ body, error }) => {
const { ratedByID, ratedUserID, ratingValue, comment } = body;

Expand Down Expand Up @@ -71,6 +72,8 @@ export const ratingController = new Elysia({ prefix: "/rating" })
}),
})

// Other endpoints...

// Update rating
.put("/:userRatingID", async ({ params, body, error }) => {
const { userRatingID } = params;
Expand Down Expand Up @@ -103,4 +106,4 @@ export const ratingController = new Elysia({ prefix: "/rating" })
ratingValue: t.Optional(t.Number()),
comment: t.Optional(t.String()),
}),
});
})
53 changes: 24 additions & 29 deletions src/controllers/teamController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const teamController = new Elysia({ prefix: "/team" })
},
});

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

Expand All @@ -30,14 +30,11 @@ export const teamController = new Elysia({ prefix: "/team" })
}),
})



// Get TeamID from name and hackathon
.get("/find", async ({ query: { teamName, hackathonID }, error }) => {
// Get Team by TeamID
.get("/getTeam", async ({ query: { TeamID }, error }) => {
const team = await prisma.team.findFirst({
where: {
TeamName: teamName,
HackathonID: hackathonID,
TeamID: TeamID,
},
});

Expand All @@ -48,43 +45,41 @@ export const teamController = new Elysia({ prefix: "/team" })
return team;
}, {
query: t.Object({
teamName: t.String(),
hackathonID: t.Number(),
TeamID: t.Number(),
}),
})

// check TeamName and UserID in rating
.get("/checkteam", async ({ query: { TeamID, userID }, error }) => {
// ตรวจสอบ input ว่ามีค่าหรือไม่
if (!TeamID || !userID) {
return error(400, "teamID and userID are required");
}
// 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({
const teamMember = await prisma.userTeam.findFirst({
where: {
TeamID: TeamID,
UserID: userID,
TeamID: TeamID,
UserID: UserID,
},
});
});

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

return "User is part of the team";
},
{
query: t.Object({
return { valid: true };
}, {
query: t.Object({
TeamID: t.Number(),
userID: t.String(),
}),
})
UserID: t.String(),
}),
})

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

Expand Down
40 changes: 24 additions & 16 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,24 +255,26 @@ export const userController = new Elysia({ prefix: "/user" })
return users; // Return all users with related skills and notifications
})

// Get UserID from Username
.get("/getUserID", async ({ query: { UserName }, error }) => {
const user = await prisma.user.findFirst({
where: {
UserName: UserName,
},
});

if (!user) {
return error(404, "User not found");
}

return {userID: user.UserID};

// Get user details from Username
.get("/getUserID/:UserID", async ({params, error }) => {
const user = await prisma.user.findFirst({
where: {
UserID: params.UserID,
},
});

if (!user) {
return error(404, "User not found");
}

return { UserID : user.UserID , UserName : user.UserName};
}, {
query: t.Object({
UserName: t.String(),
params: t.Object({
UserID: t.String(),
}),
})


// Delete user profile
.delete(
Expand All @@ -299,4 +301,10 @@ export const userController = new Elysia({ prefix: "/user" })
userID: t.String(),
}),
}
);
);



// .get (
// "/GetId",
// )

0 comments on commit 0c457d5

Please sign in to comment.