diff --git a/.env b/.env index 97160d5..dfdf19f 100644 --- a/.env +++ b/.env @@ -1,2 +1,2 @@ -DATABASE_URL="" +DATABASE_URL="sqlserver://UK-DIET-SQL-T1:1433;database=Group8_DB;user=UserGroup8;password=aFgbsH1f2evK6xyP;trustServerCertificate=true" JWT_SECRET_KEY=mysupersecretkey \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 42a3f01..5cd2b5b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "axios": "^1.9.0", "bcryptjs": "^3.0.2", "body-parser": "^2.2.0", + "csv-parse": "^5.6.0", "csv-parser": "^3.2.0", "dotenv": "^16.5.0", "easy-peasy": "^6.1.0", @@ -2657,7 +2658,7 @@ "integrity": "sha512-k38b3XOZKv60C4E2hVsXTolJWfkGRMbILBIe2IBITXciy5bOsTKot5kDrf3ZfufQtQOUN5mXceUEpU1rTl9Uog==", "license": "BSD-3-Clause", "bin": { - "bcryptjs": "bin/bcryptjs" + "bcrypt": "bin/bcrypt" } }, "node_modules/binary-extensions": { @@ -3122,6 +3123,12 @@ "devOptional": true, "license": "MIT" }, + "node_modules/csv-parse": { + "version": "5.6.0", + "resolved": "https://registry.npmjs.org/csv-parse/-/csv-parse-5.6.0.tgz", + "integrity": "sha512-l3nz3euub2QMg5ouu5U09Ew9Wf6/wQ8I++ch1loQ0ljmzhmfZYrH9fflS22i/PQEvsPvxCwxgz5q7UB8K1JO4Q==", + "license": "MIT" + }, "node_modules/csv-parser": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/csv-parser/-/csv-parser-3.2.0.tgz", @@ -8621,4 +8628,4 @@ } } } -} \ No newline at end of file +} diff --git a/package.json b/package.json index 0dd2db1..72a4377 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "axios": "^1.9.0", "bcryptjs": "^3.0.2", "body-parser": "^2.2.0", + "csv-parse": "^5.6.0", "csv-parser": "^3.2.0", "dotenv": "^16.5.0", "easy-peasy": "^6.1.0", @@ -52,4 +53,4 @@ "tailwindcss": "^3.4.1", "typescript": "^5" } -} \ No newline at end of file +} diff --git a/src/app/api/earthquakes/route.ts b/src/app/api/earthquakes/route.ts index 114ba70..addbd60 100644 --- a/src/app/api/earthquakes/route.ts +++ b/src/app/api/earthquakes/route.ts @@ -6,54 +6,28 @@ const usingPrisma = false; let prisma: PrismaClient; if (usingPrisma) prisma = new PrismaClient(); -export async function GET(request: Request) { +export async function POST(req: Request) { try { - const events = [ - { - id: "1234", - title: "Earthquake in Germany", - text1: "Magnitude 8.5", - text2: "30 minutes ago", - magnitude: 8.5, - longitude: 10.4515, // Near Berlin, Germany - latitude: 52.52, - }, - { - id: "2134", - title: "Earthquake in California", - text1: "Magnitude 5.3", - text2: "2 hours ago", - magnitude: 5.3, - longitude: -122.4194, // Near San Francisco, California, USA - latitude: 37.7749, - }, - { - id: "2314", - title: "Tremor in Japan", - text1: "Magnitude 4.7", - text2: "5 hours ago", - magnitude: 4.7, - longitude: 139.6917, // Near Tokyo, Japan - latitude: 35.6762, - }, - { - id: "2341", - title: "Tremor in Spain", - text1: "Magnitude 2.1", - text2: "10 hours ago", - magnitude: 2.1, - longitude: -3.7038, // Near Madrid, Spain - latitude: 40.4168, - }, - ]; + const json = await req.json(); // Parse incoming JSON data + const {rangeDaysPrev} = json.body - let earthquakes; - if (usingPrisma) earthquakes = await prisma.earthquakes.findMany(); + const now = new Date() + const rangeBeginning = new Date(); + rangeBeginning.setDate(rangeBeginning.getDate() - rangeDaysPrev) + + const earthquakes = await prisma.earthquake.findMany( + {where: { + date: { + gte: rangeBeginning, + lte: now + } + }} + ); if (earthquakes) { return NextResponse.json({ message: "Got earthquakes successfully", earthquakes }, { status: 200 }); } else { - return NextResponse.json({ message: "Got earthquakes successfully", earthquakes: events }, { status: 200 }); + return NextResponse.json({ message: "Got earthquakes successfully", earthquakes }, { status: 200 }); // return NextResponse.json({ message: "Failed to get earthquakes" }, { status: 401 }); } } catch (error) { diff --git a/src/app/api/import-earthquakes/route.ts b/src/app/api/import-earthquakes/route.ts new file mode 100644 index 0000000..bfe324e --- /dev/null +++ b/src/app/api/import-earthquakes/route.ts @@ -0,0 +1,63 @@ +import { NextResponse } from "next/server"; +import { PrismaClient } from "@prisma/client"; +import fs from "fs/promises"; +import path from "path"; +import { parse } from "csv-parse/sync"; + +// Define the path to your CSV file. +// Place your earthquakes.csv in your project root or `public` directory +const csvFilePath = path.resolve(process.cwd(), "public/earthquakes.csv"); + +const prisma = new PrismaClient(); + +type CsvRow = { + Date: string; + Magnitude: string; + Latitude: string; + Longitude: string; + Location: string; + Depth: string; +}; + +export async function POST() { + try { + // 1. Read the CSV file + const fileContent = await fs.readFile(csvFilePath, "utf8"); + + // 2. Parse the CSV + const records: CsvRow[] = parse(fileContent, { + columns: true, + skip_empty_lines: true + }); + + // 3. Transform each CSV row to Earthquake model + // Since your prisma model expects: name, date (DateTime), location, magnitude (float), depth (float). We'll fill casualties/creatorId as zero/null for now. + const earthquakes = records.map(row => { + // You may want to add better parsing & validation depending on your actual data + return { + date: new Date(row.Date), + location: row.Location, + magnitude: parseFloat(row.Magnitude), + latitude: row.Latitude, + longitude: row.Longitude, + depth: parseFloat(row.Depth.replace(" km", "")), + // todo add creatorId + creatorId: null + }; + }); + + // 4. Bulk create earthquakes in database: + // Consider chunking if your CSV is large! + await prisma.earthquake.createMany({ + data: earthquakes, + skipDuplicates: true, // in case the route is called twice + }); + + return NextResponse.json({ success: true, count: earthquakes.length }); + } catch (error: any) { + console.error(error); + return NextResponse.json({ success: false, error: error.message }, { status: 500 }); + } finally { + await prisma.$disconnect(); + } +} \ No newline at end of file diff --git a/src/app/api/login/route.ts b/src/app/api/login/route.ts index bd518b2..0f66f55 100644 --- a/src/app/api/login/route.ts +++ b/src/app/api/login/route.ts @@ -33,7 +33,7 @@ export async function POST(req: Request) { user = findUserByEmail(userData, email); } - if (user && bcrypt.compareSync(password, usingPrisma ? user.hashedPassword : user.password)) { + if (user && bcryptjs.compareSync(password, usingPrisma ? user.passwordHash : user.password)) { // todo remove password from returned user // get user and relations diff --git a/src/app/earthquakes/page.tsx b/src/app/earthquakes/page.tsx index cd07d6c..3b53d75 100644 --- a/src/app/earthquakes/page.tsx +++ b/src/app/earthquakes/page.tsx @@ -3,7 +3,7 @@ import { useMemo, useState } from "react"; import useSWR from "swr"; -import Map from "@components/map"; +import Map from "@components/Map"; import Sidebar from "@components/Sidebar"; import { fetcher } from "@utils/fetcher"; diff --git a/src/app/layout.tsx b/src/app/layout.tsx index c953752..de01756 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -6,7 +6,7 @@ import { action, createStore, StoreProvider } from 'easy-peasy'; import { Inter } from 'next/font/google'; import { StoreModel } from '@appTypes/StoreModel'; -import Navbar from '@components/navbar'; +import Navbar from '@components/Navbar'; const inter = Inter({ subsets: ["latin"], diff --git a/src/app/observatories/page.tsx b/src/app/observatories/page.tsx index 6b3d030..f608af9 100644 --- a/src/app/observatories/page.tsx +++ b/src/app/observatories/page.tsx @@ -4,7 +4,7 @@ import { useMemo, useState } from "react"; import useSWR from "swr"; import Sidebar from "@/components/Sidebar"; -import Map from "@components/map"; +import Map from "@components/Map"; import { fetcher } from "@utils/fetcher"; export default function Observatories() {