-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0194f7
commit 5833ca6
Showing
6 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
prisma/migrations/20240710211511_add_team_geo_location/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
prisma/migrations/20240710211956_make_optional/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters