From 4b4936afec1a613ebecc3aaeb3efdfd877f7a6b5 Mon Sep 17 00:00:00 2001 From: Azalea Colburn <62953415+azaleacolburn@users.noreply.github.com> Date: Wed, 11 Dec 2024 17:11:56 -0800 Subject: [PATCH] Database Hotifx (#26) * feat: created DB file * feat: type model for scouting system * chore: update svelte * feat: tried * feat: nothing * feat: add none * feat: added DB.sql and migrations * refactor: type state model * feat: state transforms * add successfail screen * theme fixes * fix: package.json * fix: formating * fix: dependencies * format thingy * feat: implemented submit api * fix: removed unnecessary line * refactor: update types to match new schema * fix: id after clarifying with Brandon * update dep * style: format files * feat: pulled from main * style: format files * fix: dependencies * style: format files * fix: dependencies * fix: skill property name * fix: removed npm config * fix: bun dependencies * feat: finished submit function * feat: read route * feat: superscouting and pit scouting in db * fix: DB code * fix: updated queries to allow ids * fix: tailwind to CommonJS module * style: format files * fix: queries * style: format files * fix: types * style: format files * feat: hooked up frontend to backend * fix: queries * feat: functional queries * style: format files * fix: queries returning too much * more merge * feat: insert to users and matches * style: format files * fix: database enums * fix: missing comma --------- Co-authored-by: Brandon Harad Co-authored-by: awwpotato Co-authored-by: awwpotato Co-authored-by: Brandon-Harad Co-authored-by: azaleacolburn --- migrations/DB.sql | 41 ++++++++++++++++-------------- src/lib/server-assets/database.ts | 31 ++++++++++++++++++++++ src/routes/admin/+page.svelte | 11 +++++++- src/routes/api/newmatch/+server.ts | 8 ++++++ src/routes/api/submit/+server.ts | 4 +-- 5 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 src/routes/api/newmatch/+server.ts diff --git a/migrations/DB.sql b/migrations/DB.sql index ebf367c..dc83d0a 100644 --- a/migrations/DB.sql +++ b/migrations/DB.sql @@ -1,26 +1,30 @@ CREATE TYPE tele_action_type AS ENUM ( - 'IntakeTote', - 'IntakeBalloon', - 'TakeBalloonFromCoral', - - 'ScoreYourHeldTote', - 'ScoreOtherHeldTote', - 'ScoreExternalTote', - 'ScoreLowBalloon' + 'IntakeTote', + 'IntakeBalloon', + 'ScoreBalloonInternalTote', + 'ScoreBalloonExternalTote', + 'ScoreBalloonUncontrolledTote', + 'ScoreBalloonLow', + 'EjectBalloon', + 'EjectBunny', + 'EjectTote' ); CREATE TYPE auto_action_type as ENUM ( - 'IntakeTote', - 'IntakeBalloon', - 'TakeBalloonFromCoral', - - 'ScoreYourHeldTote', - 'ScoreOtherHeldTote', - 'ScoreExternalTote', - 'ScoreLowBalloon', + 'IntakeTote', + 'IntakeBalloon', + 'ScoreBalloonInternalTote', + 'ScoreBalloonExternalTote', + 'ScoreBalloonUncontrolledTote', + 'ScoreBalloonLow', + 'EjectBalloon', + 'EjectBunny', + 'EjectTote', 'IntakeBunny', - 'ScoreBunnyTote', - 'ScoreBunnyLowZone' + 'ScoreBunnyInternalTote', + 'ScoreBunnyExternalTote', + 'ScoreBunnyUncontrolledTote', + 'ScoreBunnyLow' ); CREATE TYPE tele_action_data AS ( act tele_action_type, @@ -39,7 +43,6 @@ CREATE TYPE drivetrain_enum as ENUM ( CREATE TABLE "Users"( "id" SERIAL PRIMARY KEY, "name" VARCHAR(255) NOT NULL, - "is_admin" BOOLEAN NOT NULL ); CREATE TABLE "Teams"( "team_key" VARCHAR(255) NOT NULL, diff --git a/src/lib/server-assets/database.ts b/src/lib/server-assets/database.ts index e3cb8c0..6f5e587 100644 --- a/src/lib/server-assets/database.ts +++ b/src/lib/server-assets/database.ts @@ -18,6 +18,9 @@ import type { TeleActionsTM } from '$lib/types'; +// Const b/c of obvious reasons +const event_key = 'orbb2024'; + // Whether or not the database is currently being used const use_db: boolean = USE_DB === 'true'; @@ -218,10 +221,38 @@ export async function insertTeamMatch(match: TeamMatch): Promise { } } +export async function insertUser(name: string): Promise { + if (!use_db) return true; + + try { + await db.query('INSERT INTO "Users" VALUES (DEFAULT, $1)', [name]); + + return true; + } catch (error) { + console.error(error); + return false; + } +} + export async function select(matchkey: string, teamkey: string) { + if (!use_db) return null; + let response = await db.query( 'SELECT * FROM "TeamMatches" WHERE "match_key" = $1 AND "team_key" = $2', [matchkey, teamkey] ); return response.rows; } + +export async function insertMatch(match_key: string): Promise { + if (!use_db) return true; + + try { + await db.query('INSERT INTO "Matches" VALUES ($1, $2)', [match_key, event_key]); + + return true; + } catch (error) { + console.error(error); + return false; + } +} diff --git a/src/routes/admin/+page.svelte b/src/routes/admin/+page.svelte index 0e02949..988466d 100644 --- a/src/routes/admin/+page.svelte +++ b/src/routes/admin/+page.svelte @@ -60,9 +60,18 @@ teams.map((team, i) => [team, colors[i]] as [string, 'red' | 'blue']) ); - const queue_match = () => { + const queue_match = async () => { teams = teams.filter((team) => team != ''); socket.emit('send_match', [match_key, team_color]); + + await fetch('/api/newmatch', { + method: 'POST', + body: JSON.stringify(match_key), + headers: { + 'Content-Type': 'application/json' + } + }); + match_key = ''; teams = ['', '', '', '', '', '']; }; diff --git a/src/routes/api/newmatch/+server.ts b/src/routes/api/newmatch/+server.ts new file mode 100644 index 0000000..ae0000c --- /dev/null +++ b/src/routes/api/newmatch/+server.ts @@ -0,0 +1,8 @@ +import { json } from '@sveltejs/kit'; +import type { RequestHandler } from './$types'; +import { insertMatch } from '$lib/server-assets/database'; + +export const POST: RequestHandler = async ({ request }) => { + const match_key: string = await request.json(); + return json(await insertMatch(match_key)); +}; diff --git a/src/routes/api/submit/+server.ts b/src/routes/api/submit/+server.ts index c59ab37..0cc0cf3 100644 --- a/src/routes/api/submit/+server.ts +++ b/src/routes/api/submit/+server.ts @@ -1,9 +1,9 @@ import type { TeamMatch } from '$lib/types'; import { json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; -import { insertTeamMatch } from '$lib/server-assets/database'; +import { insertTeamMatch, insertUser } from '$lib/server-assets/database'; export const POST: RequestHandler = async ({ request }) => { const match: TeamMatch = await request.json(); - return json(await insertTeamMatch(match)); + return json((await insertTeamMatch(match)) && (await insertUser(match.scout_id))); };