From 78124b029d0e2af0830be9c2e5c8fdae026cf74b Mon Sep 17 00:00:00 2001 From: derGraph Date: Sat, 24 Aug 2024 20:47:50 +0200 Subject: [PATCH] changed db, removed voyages, visibility of Trips added --- .../migration.sql | 18 +-- .../migration.sql | 2 + .../20240728152121_new/migration.sql | 126 ------------------ .../20240801232822_add_ping/migration.sql | 2 - .../20240801232905_add_ping2/migration.sql | 25 ---- prisma/migrations_old/migration_lock.toml | 3 - prisma/schema.prisma | 20 +-- src/routes/api/Datapoints/+server.ts | 29 +++- src/routes/sign_up/+page.server.ts | 14 +- 9 files changed, 43 insertions(+), 196 deletions(-) rename prisma/migrations/{20240817134152_ => 20240824175652_init}/migration.sql (90%) create mode 100644 prisma/migrations/20240824180328_changed_visi_for_trip/migration.sql delete mode 100644 prisma/migrations_old/20240728152121_new/migration.sql delete mode 100644 prisma/migrations_old/20240801232822_add_ping/migration.sql delete mode 100644 prisma/migrations_old/20240801232905_add_ping2/migration.sql delete mode 100644 prisma/migrations_old/migration_lock.toml diff --git a/prisma/migrations/20240817134152_/migration.sql b/prisma/migrations/20240824175652_init/migration.sql similarity index 90% rename from prisma/migrations/20240817134152_/migration.sql rename to prisma/migrations/20240824175652_init/migration.sql index d2aa0e7..8443fdb 100644 --- a/prisma/migrations/20240817134152_/migration.sql +++ b/prisma/migrations/20240824175652_init/migration.sql @@ -46,22 +46,9 @@ CREATE TABLE `Key` ( PRIMARY KEY (`id`) ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; --- CreateTable -CREATE TABLE `Voyage` ( - `id` VARCHAR(191) NOT NULL, - `name` VARCHAR(191) NOT NULL, - `description` VARCHAR(191) NULL, - `start` DATETIME(3) NULL, - `end` DATETIME(3) NULL, - `public` BOOLEAN NOT NULL, - - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - -- CreateTable CREATE TABLE `Trip` ( `id` VARCHAR(191) NOT NULL, - `voyageId` VARCHAR(191) NOT NULL, `name` VARCHAR(191) NOT NULL, `description` VARCHAR(191) NULL, `startPointId` VARCHAR(191) NULL, @@ -71,6 +58,7 @@ CREATE TABLE `Trip` ( `last_update` DATETIME(3) NOT NULL, `length` DECIMAL(65, 30) NULL, `skipperName` VARCHAR(191) NULL, + `visibility` BOOLEAN NOT NULL DEFAULT false, UNIQUE INDEX `Trip_startPointId_key`(`startPointId`), UNIQUE INDEX `Trip_endPointId_key`(`endPointId`), @@ -90,6 +78,7 @@ CREATE TABLE `Datapoint` ( `h_accuracy` DECIMAL(65, 30) NULL, `v_accuracy` DECIMAL(65, 30) NULL, `propulsion` INTEGER NULL, + `optimized` INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (`id`) ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; @@ -127,9 +116,6 @@ ALTER TABLE `Session` ADD CONSTRAINT `Session_userId_fkey` FOREIGN KEY (`userId` -- AddForeignKey ALTER TABLE `Key` ADD CONSTRAINT `Key_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`username`) ON DELETE CASCADE ON UPDATE CASCADE; --- AddForeignKey -ALTER TABLE `Trip` ADD CONSTRAINT `Trip_voyageId_fkey` FOREIGN KEY (`voyageId`) REFERENCES `Voyage`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; - -- AddForeignKey ALTER TABLE `Trip` ADD CONSTRAINT `Trip_startPointId_fkey` FOREIGN KEY (`startPointId`) REFERENCES `Datapoint`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/migrations/20240824180328_changed_visi_for_trip/migration.sql b/prisma/migrations/20240824180328_changed_visi_for_trip/migration.sql new file mode 100644 index 0000000..a6d563c --- /dev/null +++ b/prisma/migrations/20240824180328_changed_visi_for_trip/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE `trip` MODIFY `visibility` INTEGER NOT NULL DEFAULT 1; diff --git a/prisma/migrations_old/20240728152121_new/migration.sql b/prisma/migrations_old/20240728152121_new/migration.sql deleted file mode 100644 index 6a0fcaa..0000000 --- a/prisma/migrations_old/20240728152121_new/migration.sql +++ /dev/null @@ -1,126 +0,0 @@ --- CreateTable -CREATE TABLE "User" ( - "username" TEXT NOT NULL PRIMARY KEY, - "email" TEXT NOT NULL, - "firstName" TEXT, - "lastName" TEXT, - "description" TEXT, - "profilePictureId" TEXT, - "dateOfBirth" DATETIME, - "roleId" TEXT NOT NULL DEFAULT 'user', - "activeTripId" TEXT NOT NULL, - CONSTRAINT "User_profilePictureId_fkey" FOREIGN KEY ("profilePictureId") REFERENCES "Media" ("id") ON DELETE SET NULL ON UPDATE CASCADE, - CONSTRAINT "User_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "Role" ("id") ON DELETE RESTRICT ON UPDATE CASCADE, - CONSTRAINT "User_activeTripId_fkey" FOREIGN KEY ("activeTripId") REFERENCES "Trip" ("id") ON DELETE RESTRICT ON UPDATE CASCADE -); - --- CreateTable -CREATE TABLE "Session" ( - "id" TEXT NOT NULL PRIMARY KEY, - "userId" TEXT NOT NULL, - "expiresAt" DATETIME NOT NULL, - CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("username") ON DELETE CASCADE ON UPDATE CASCADE -); - --- CreateTable -CREATE TABLE "Role" ( - "id" TEXT NOT NULL PRIMARY KEY -); - --- CreateTable -CREATE TABLE "Key" ( - "id" TEXT NOT NULL PRIMARY KEY, - "type" TEXT NOT NULL, - "passwordHash" TEXT, - "userId" TEXT NOT NULL, - "primary" BOOLEAN NOT NULL, - CONSTRAINT "Key_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("username") ON DELETE CASCADE ON UPDATE CASCADE -); - --- CreateTable -CREATE TABLE "Voyage" ( - "id" TEXT NOT NULL PRIMARY KEY, - "name" TEXT NOT NULL, - "description" TEXT, - "start" DATETIME, - "end" DATETIME, - "public" BOOLEAN NOT NULL -); - --- CreateTable -CREATE TABLE "Trip" ( - "id" TEXT NOT NULL PRIMARY KEY, - "voyageId" TEXT NOT NULL, - "name" TEXT NOT NULL, - "description" TEXT, - "startPointId" TEXT, - "endPointId" TEXT, - "start" DATETIME, - "end" DATETIME, - "last_update" DATETIME NOT NULL, - "length" DECIMAL, - "skipperName" TEXT, - CONSTRAINT "Trip_voyageId_fkey" FOREIGN KEY ("voyageId") REFERENCES "Voyage" ("id") ON DELETE RESTRICT ON UPDATE CASCADE, - CONSTRAINT "Trip_startPointId_fkey" FOREIGN KEY ("startPointId") REFERENCES "Datapoint" ("id") ON DELETE SET NULL ON UPDATE CASCADE, - CONSTRAINT "Trip_endPointId_fkey" FOREIGN KEY ("endPointId") REFERENCES "Datapoint" ("id") ON DELETE SET NULL ON UPDATE CASCADE, - CONSTRAINT "Trip_skipperName_fkey" FOREIGN KEY ("skipperName") REFERENCES "User" ("username") ON DELETE SET NULL ON UPDATE CASCADE -); - --- CreateTable -CREATE TABLE "Datapoint" ( - "id" TEXT NOT NULL PRIMARY KEY, - "tripId" TEXT NOT NULL, - "time" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - "lat" DECIMAL NOT NULL, - "long" DECIMAL NOT NULL, - "speed" DECIMAL, - "heading" DECIMAL, - "depth" DECIMAL, - "h_accuracy" DECIMAL, - "v_accuracy" DECIMAL, - "propulsion" INTEGER, - CONSTRAINT "Datapoint_tripId_fkey" FOREIGN KEY ("tripId") REFERENCES "Trip" ("id") ON DELETE RESTRICT ON UPDATE CASCADE -); - --- CreateTable -CREATE TABLE "Media" ( - "id" TEXT NOT NULL PRIMARY KEY, - "visibility" INTEGER NOT NULL DEFAULT 1, - "username" TEXT NOT NULL, - CONSTRAINT "Media_username_fkey" FOREIGN KEY ("username") REFERENCES "User" ("username") ON DELETE RESTRICT ON UPDATE CASCADE -); - --- CreateTable -CREATE TABLE "_crew" ( - "A" TEXT NOT NULL, - "B" TEXT NOT NULL, - CONSTRAINT "_crew_A_fkey" FOREIGN KEY ("A") REFERENCES "Trip" ("id") ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT "_crew_B_fkey" FOREIGN KEY ("B") REFERENCES "User" ("username") ON DELETE CASCADE ON UPDATE CASCADE -); - --- CreateIndex -CREATE UNIQUE INDEX "User_username_key" ON "User"("username"); - --- CreateIndex -CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); - --- CreateIndex -CREATE UNIQUE INDEX "User_profilePictureId_key" ON "User"("profilePictureId"); - --- CreateIndex -CREATE UNIQUE INDEX "Key_id_key" ON "Key"("id"); - --- CreateIndex -CREATE INDEX "Key_userId_idx" ON "Key"("userId"); - --- CreateIndex -CREATE UNIQUE INDEX "Trip_startPointId_key" ON "Trip"("startPointId"); - --- CreateIndex -CREATE UNIQUE INDEX "Trip_endPointId_key" ON "Trip"("endPointId"); - --- CreateIndex -CREATE UNIQUE INDEX "_crew_AB_unique" ON "_crew"("A", "B"); - --- CreateIndex -CREATE INDEX "_crew_B_index" ON "_crew"("B"); diff --git a/prisma/migrations_old/20240801232822_add_ping/migration.sql b/prisma/migrations_old/20240801232822_add_ping/migration.sql deleted file mode 100644 index e621ce8..0000000 --- a/prisma/migrations_old/20240801232822_add_ping/migration.sql +++ /dev/null @@ -1,2 +0,0 @@ --- AlterTable -ALTER TABLE "User" ADD COLUMN "lastPing" DATETIME; diff --git a/prisma/migrations_old/20240801232905_add_ping2/migration.sql b/prisma/migrations_old/20240801232905_add_ping2/migration.sql deleted file mode 100644 index d5ec5f0..0000000 --- a/prisma/migrations_old/20240801232905_add_ping2/migration.sql +++ /dev/null @@ -1,25 +0,0 @@ --- RedefineTables -PRAGMA foreign_keys=OFF; -CREATE TABLE "new_User" ( - "username" TEXT NOT NULL PRIMARY KEY, - "email" TEXT NOT NULL, - "firstName" TEXT, - "lastName" TEXT, - "description" TEXT, - "profilePictureId" TEXT, - "dateOfBirth" DATETIME, - "roleId" TEXT NOT NULL DEFAULT 'user', - "activeTripId" TEXT NOT NULL, - "lastPing" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - CONSTRAINT "User_profilePictureId_fkey" FOREIGN KEY ("profilePictureId") REFERENCES "Media" ("id") ON DELETE SET NULL ON UPDATE CASCADE, - CONSTRAINT "User_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "Role" ("id") ON DELETE RESTRICT ON UPDATE CASCADE, - CONSTRAINT "User_activeTripId_fkey" FOREIGN KEY ("activeTripId") REFERENCES "Trip" ("id") ON DELETE RESTRICT ON UPDATE CASCADE -); -INSERT INTO "new_User" ("activeTripId", "dateOfBirth", "description", "email", "firstName", "lastName", "lastPing", "profilePictureId", "roleId", "username") SELECT "activeTripId", "dateOfBirth", "description", "email", "firstName", "lastName", coalesce("lastPing", CURRENT_TIMESTAMP) AS "lastPing", "profilePictureId", "roleId", "username" FROM "User"; -DROP TABLE "User"; -ALTER TABLE "new_User" RENAME TO "User"; -CREATE UNIQUE INDEX "User_username_key" ON "User"("username"); -CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); -CREATE UNIQUE INDEX "User_profilePictureId_key" ON "User"("profilePictureId"); -PRAGMA foreign_key_check("User"); -PRAGMA foreign_keys=ON; diff --git a/prisma/migrations_old/migration_lock.toml b/prisma/migrations_old/migration_lock.toml deleted file mode 100644 index e5e5c47..0000000 --- a/prisma/migrations_old/migration_lock.toml +++ /dev/null @@ -1,3 +0,0 @@ -# Please do not edit this file manually -# It should be added in your version-control system (i.e. Git) -provider = "sqlite" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 84bbe45..b897712 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -54,20 +54,8 @@ model Key { @@index([userId]) } -model Voyage { - id String @id @default(cuid()) - name String - description String? - trips Trip[] - start DateTime? - end DateTime? - public Boolean -} - model Trip { id String @id @default(cuid()) - voyage Voyage @relation(fields: [voyageId], references: [id]) - voyageId String name String description String? startPoint Datapoint?@relation("startPoint", fields: [startPointId], references: [id]) @@ -83,7 +71,10 @@ model Trip { skipper User? @relation("skipper", fields: [skipperName], references: [username]) skipperName String? crew User[] @relation("crew") -} + visibility Int @default(1) // 0: private + // 1: logged in + // 2: public + } model Datapoint { id String @id @default(cuid()) @@ -102,6 +93,9 @@ model Datapoint { propulsion Int? // 0: anchor // 1: motor // 2: sailing + optimized Int @default(0) // 0: to optimize + // 1: deleted + // 2: needed } model Media { diff --git a/src/routes/api/Datapoints/+server.ts b/src/routes/api/Datapoints/+server.ts index b411889..442dbf7 100644 --- a/src/routes/api/Datapoints/+server.ts +++ b/src/routes/api/Datapoints/+server.ts @@ -312,15 +312,34 @@ export async function GET(event) { } try { - if(!event.locals.user?.username){ + if(event.locals.user?.username){ let tripData = await prisma.trip.findFirstOrThrow({ where: { - voyage: { - public: true, - } + OR: [{ + id: requestedTrip, + visibility: 1 + }, + { + id: requestedTrip, + visibility: 2 + }, + { + id: requestedTrip, + crew: { + some: { + username: event.locals.user?.username + } + } + }] + } + }); + }else{ + let tripData = await prisma.trip.findFirstOrThrow({ + where: { + id: requestedTrip, + visibility: 2 } }); - } let datapoints = await prisma.datapoint.findMany({ where: { diff --git a/src/routes/sign_up/+page.server.ts b/src/routes/sign_up/+page.server.ts index ccac57f..2838df7 100644 --- a/src/routes/sign_up/+page.server.ts +++ b/src/routes/sign_up/+page.server.ts @@ -104,16 +104,18 @@ export const actions: Actions = { activeTrip: { create: { name: 'newTrip', - voyage: { - create: { - name: 'newVoyage', - public: false - } - } } } } }); + await prisma.user.update({ + where: { + username: username, + }, + data: { + crewedTrips: {connect: {id: (await prisma.user.findFirstOrThrow({where: {username: username}})).activeTripId}} + } + }); } catch (error) { if (error instanceof Error) { console.log(error);