mirror of
https://github.com/System-End/site.git
synced 2026-04-19 18:35:12 +00:00
api update
This commit is contained in:
parent
40c6990239
commit
c88f5f763b
2 changed files with 88 additions and 51 deletions
|
|
@ -24,40 +24,72 @@ export default async function handler(req, res) {
|
|||
}
|
||||
|
||||
try {
|
||||
const { authorization, points, 'project-id': projectId } = req.headers;
|
||||
|
||||
const { authorization} = req.headers;
|
||||
const { rank1, rank2, rank3 } = req.body;
|
||||
return res.status(500).json({ error: req.body });
|
||||
|
||||
if (!authorization || !points || !projectId) {
|
||||
return res.status(400).json({ error: 'Missing required headers' });
|
||||
const pointsDistribution = [5, 4, 3, 2, 1];
|
||||
|
||||
for (let i = 0; i < rank1.length; i++) {
|
||||
const project = rank1[i];
|
||||
const points = pointsDistribution[i];
|
||||
|
||||
addVote(project, points, authorization);
|
||||
}
|
||||
|
||||
const users = await usersTable.read({
|
||||
filterByFormula: `{auth_token} = '${authorization}'`
|
||||
});
|
||||
|
||||
if (users.length === 0) {
|
||||
return res.status(404).json({ error: 'User not found' });
|
||||
for (let i = 0; i < rank2.length; i++) {
|
||||
const project = rank2[i];
|
||||
const points = pointsDistribution[i];
|
||||
|
||||
addVote(project, points, authorization);
|
||||
}
|
||||
|
||||
const user = users[0];
|
||||
for (let i = 0; i < rank3.length; i++) {
|
||||
const project = rank3[i];
|
||||
const points = pointsDistribution[i];
|
||||
|
||||
addVote(project, points, authorization);
|
||||
}
|
||||
|
||||
const vote = await votesTable.create({
|
||||
points: parseInt(points, 10),
|
||||
voter: [user.id]
|
||||
});
|
||||
|
||||
const project = await showcaseTable.find(projectId);
|
||||
|
||||
const updatedVotes = project.fields.Votes
|
||||
? [...project.fields.Votes, vote.id]
|
||||
: [vote.id];
|
||||
|
||||
await showcaseTable.update(projectId, {
|
||||
Votes: updatedVotes
|
||||
});
|
||||
|
||||
res.status(200).json({ success: true, vote });
|
||||
{ success: true, vote }
|
||||
|
||||
res.status(200).json();
|
||||
} catch (error) {
|
||||
console.error('Error creating vote:', error);
|
||||
res.status(500).json({ error: 'Internal Server Error' });
|
||||
}
|
||||
}
|
||||
|
||||
const addVote = async (projectId, points, authorization) => {
|
||||
if (!authorization || !points || !projectId) {
|
||||
return res.status(400).json({ error: 'Missing required headers' });
|
||||
}
|
||||
|
||||
const users = await usersTable.read({
|
||||
filterByFormula: `{Auth Token} = '${authorization}'`
|
||||
});
|
||||
|
||||
if (users.length === 0) {
|
||||
return res.status(404).json({ error: 'User not found' });
|
||||
}
|
||||
|
||||
const user = users[0];
|
||||
console.log(user);
|
||||
|
||||
const vote = await votesTable.create({
|
||||
Points: parseInt(points, 10),
|
||||
Voter: [user.id]
|
||||
});
|
||||
|
||||
const project = await showcaseTable.find(projectId);
|
||||
|
||||
const updatedVotes = project.fields.Votes
|
||||
? [...project.fields.Votes, vote.id]
|
||||
: [vote.id];
|
||||
|
||||
await showcaseTable.update(projectId, {
|
||||
Votes: updatedVotes
|
||||
});
|
||||
|
||||
}
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
import React from 'react'
|
||||
import { useEffect } from 'react';
|
||||
import React, { useEffect } from 'react';
|
||||
|
||||
const test = () => {
|
||||
const Test = () => {
|
||||
|
||||
|
||||
async function submitVote(points, projectId) {
|
||||
const authToken = window.localStorage.getItem('arcade.authToken')
|
||||
const submitVote = async (rank1, rank2, rank3) => {
|
||||
const authToken = window.localStorage.getItem('arcade.authToken');
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/arcade/showcase/vote', {
|
||||
|
|
@ -12,30 +12,35 @@ const test = () => {
|
|||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': authToken,
|
||||
'Points': points.toString(),
|
||||
'Project-ID': projectId
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
|
||||
},
|
||||
body: JSON.stringify({
|
||||
rank1,
|
||||
rank2,
|
||||
rank3,
|
||||
}),
|
||||
})
|
||||
|
||||
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('Error submitting vote:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
submitVote(5, "recnSpjM91mCKJjlo");
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div>test</div>
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
export default test
|
||||
useEffect(() => {
|
||||
|
||||
const rank1 = ["recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo"];
|
||||
const rank2 = ["recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo"];
|
||||
const rank3 = ["recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo"];
|
||||
|
||||
submitVote(rank1, rank2, rank3);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div>test</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Test;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue