Skip to content

Commit

Permalink
Add push route and setup route
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-m-2983 committed Jul 10, 2024
1 parent c0194f7 commit 5833ca6
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Warnings:
- Added the required column `city` to the `Team` table without a default value. This is not possible if the table is not empty.
- Added the required column `country` to the `Team` table without a default value. This is not possible if the table is not empty.
- Added the required column `state_prov` to the `Team` table without a default value. This is not possible if the table is not empty.
*/
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Team" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"city" TEXT NOT NULL,
"state_prov" TEXT NOT NULL,
"country" TEXT NOT NULL
);
INSERT INTO "new_Team" ("id", "name") SELECT "id", "name" FROM "Team";
DROP TABLE "Team";
ALTER TABLE "new_Team" RENAME TO "Team";
CREATE UNIQUE INDEX "Team_id_key" ON "Team"("id");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;
16 changes: 16 additions & 0 deletions prisma/migrations/20240710211956_make_optional/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Team" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"city" TEXT,
"state_prov" TEXT,
"country" TEXT
);
INSERT INTO "new_Team" ("city", "country", "id", "name", "state_prov") SELECT "city", "country", "id", "name", "state_prov" FROM "Team";
DROP TABLE "Team";
ALTER TABLE "new_Team" RENAME TO "Team";
CREATE UNIQUE INDEX "Team_id_key" ON "Team"("id");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;
4 changes: 4 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ model Team {
id Int @id @unique
name String
city String?
state_prov String?
country String?
entries Entry[]
}

Expand Down
56 changes: 56 additions & 0 deletions src/app/api/push/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { MD5 } from "crypto-js";
import { PrismaClient } from '@prisma/client'
import { NextRequest, NextResponse } from "next/server";
import { existsSync, mkdirSync, writeFileSync } from "fs";

interface Entry {
type: string;
user: string;
team: string;
event: string;
timestamp: string;
data: any;
}

const prismaClient = new PrismaClient();

export async function POST(request: NextRequest) {
const data: Entry[] = await request.json();

const checksum = MD5(JSON.stringify(data)).toString();

const entryIds = (await prismaClient.entry.createManyAndReturn({
data: data.map(entry => ({
type: entry.type,
teamNumber: parseInt(entry.team)
})),
select: {
id: true
}
})).map(entry => entry.id);

entryIds.forEach((id, index) => writeEntry(id, data[index]));

if(entryIds.length == data.length) return NextResponse.json({
hash: checksum,
entryIds: entryIds
});

return NextResponse.error();
}

function writeEntry(id: string, entry: Entry) {
const dataDir = useDataDir();

writeFileSync(`${dataDir}/${id}.turbo.json`, JSON.stringify(entry));
}

function useDataDir() {
const dataDir = "./turbo-data";

if (!existsSync(dataDir)) {
mkdirSync(dataDir);
}

return dataDir;
}
35 changes: 35 additions & 0 deletions src/app/api/setup/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { TBA_BASE, TBA_OPTS } from "@/app/lib/tba_api";
import { PrismaClient } from "@prisma/client";
import { NextRequest, NextResponse } from "next/server";

const prismaClient = new PrismaClient();

interface TeamSimple {
key: string;

team_number: number;
name: string;
nickname: string;

city: string;
state_prov: string;
country: string;
}

export async function GET(req: NextRequest) {
const teams: TeamSimple[] = (await Promise.all([...Array(40).keys()].map(async (pageNumber) => {
return await fetch(TBA_BASE + `/teams/${pageNumber}/simple`, TBA_OPTS).then(resp => resp.json());
}))).flat();

const result = await prismaClient.team.createMany({
data: teams.map(team => ({
id: team.team_number,
name: team.nickname,
city: team.city,
state_prov: team.state_prov,
country: team.country
}))
});

return NextResponse.json({msg: "Added " + result.count + " teams!"});
}
6 changes: 0 additions & 6 deletions src/app/lib/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import MD5 from "crypto-js/md5";
import React from "react";

export async function exportData(sendQueue: any, clearSendQueue: any) {
modals.open({
title: "This is still being worked on!",
})
}

export async function exportDataOLD(sendQueue: any, clearSendQueue: any) {
fetch("/api/push", {
method: 'post',
headers: {
Expand Down

0 comments on commit 5833ca6

Please sign in to comment.