Skip to content

Commit

Permalink
changed db, removed voyages, visibility of Trips added
Browse files Browse the repository at this point in the history
  • Loading branch information
derGraph committed Aug 24, 2024
1 parent 1fad5f3 commit 78124b0
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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`),
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE `trip` MODIFY `visibility` INTEGER NOT NULL DEFAULT 1;
126 changes: 0 additions & 126 deletions prisma/migrations_old/20240728152121_new/migration.sql

This file was deleted.

2 changes: 0 additions & 2 deletions prisma/migrations_old/20240801232822_add_ping/migration.sql

This file was deleted.

25 changes: 0 additions & 25 deletions prisma/migrations_old/20240801232905_add_ping2/migration.sql

This file was deleted.

3 changes: 0 additions & 3 deletions prisma/migrations_old/migration_lock.toml

This file was deleted.

20 changes: 7 additions & 13 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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())
Expand All @@ -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 {
Expand Down
29 changes: 24 additions & 5 deletions src/routes/api/Datapoints/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
14 changes: 8 additions & 6 deletions src/routes/sign_up/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 78124b0

Please sign in to comment.