Skip to content

Commit

Permalink
Merge branch 'asmm-signup-patch'
Browse files Browse the repository at this point in the history
  • Loading branch information
EwanLyon committed Jul 27, 2023
2 parents 6956b7b + 600d99f commit f370274
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
54 changes: 46 additions & 8 deletions apps/nextjs/pages/api/asmm/sign-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,58 @@ import sql from 'mssql';
import { connect } from './db';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== "POST") {
return res.status(405).setHeader("Allow", "POST");
}

try {
const body = JSON.parse(req.body);
await connect(sql);

const data = await sql.query`
INSERT INTO [dbo].[Participants]
VALUES (${body.username},${body.ticketID ?? "NULL"})`;
//Try find barcode for setSignedUp
if (req.method === "GET") {

const data = await sql.query`
SELECT Barcode
FROM [dbo].[Participants]
WHERE Username = ${req.query.username}
`;

console.log(data)

if (!data.recordset[0]) return res.status(200).json({ barcode: "NULL" });

return res.status(200).json({ barcode: data.recordset[0].Barcode });

}

if (req.method === "POST") {

const body = JSON.parse(req.body);

const userExists = await sql.query`
SELECT [ParticipantId] FROM [dbo].[Participants]
WHERE Username = ${body.username}`;

if (userExists.recordset.length === 0) {

const data = await sql.query`
INSERT INTO [dbo].[Participants]
VALUES (${body.username},${body.ticketID ?? "NULL"})`;

console.log(JSON.stringify(data))
} else {

const editData = await sql.query`
UPDATE [dbo].[Participants]
SET Barcode = ${body.ticketId ?? "NULL"}
WHERE Username = ${body.username}`;

console.log(JSON.stringify(editData))
}

return res.status(200).json({ success: true });
}

return res.status(200).json({ success: true });
} catch (error) {
return res.status(500).json({ error: error });
}

return res.status(405).setHeader("Allow", ["POST", "GET"]);
}
14 changes: 10 additions & 4 deletions apps/nextjs/pages/asmm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,17 @@ export default function ASMM() {
res.text().then(console.log);
});

fetch(`/api/asmm/sign-up?username=${username}`, { method: "GET" }).then((res) => {
res.json().then((data) => {
console.log(data);
setSignedUp(data.Barcode != "NULL");
});
});

fetch(`/api/asmm/pledge?username=${username}`, { method: "GET" }).then((res) => {
res.json().then((data) => {
console.log(data);
setPledgeAmount(data.pledge);
setSignedUp(data.pledge > 0);
});
});
}
Expand Down Expand Up @@ -123,9 +129,9 @@ export default function ASMM() {
<div className={`${styles.content} ${styles.form}`} style={{ padding: "5rem" }}>
<h1 style={{ marginBottom: "3rem" }}>Australian Speedruns Marathon Marathon Sign Up</h1>
<p>
The Australian Speedruns Marathon Marathon (ASMM) is a walkathon we will be conducting during the Australian
Speedrun Marathon 2023. At the end of each day we will tally the steps taken by each participant.
Participants are asked to pledge an amount per 10km walked by the community.
The Australian Speedruns Marathon Marathon (ASMM) is a walkathon we will be conducting during the
Australian Speedrun Marathon 2023. At the end of each day we will tally the steps taken by each
participant. Participants are asked to pledge an amount per 10km walked by the community.
</p>
<p>Those not attending ASM2023 can also make a pledge.</p>
<p>
Expand Down

0 comments on commit f370274

Please sign in to comment.