From 38c336c792666d9037af285b503b745a8cd0ede5 Mon Sep 17 00:00:00 2001 From: Manas Pratim Biwas Date: Sun, 28 Apr 2024 15:52:05 +0530 Subject: [PATCH] [fix]: API payloads, db schema, seed default values --- .../20240421103750_init/migration.sql | 27 ----------------- .../20240428092122_dbseed/migration.sql | 30 +++++++++++++++++++ backend/loc/prisma/schema.prisma | 18 +++++------ backend/loc/src/mockdata/Location.sql | 9 ++++++ backend/loc/src/mockdata/User.sql | 12 ++++++++ backend/loc/src/routes/auth.ts | 4 +-- backend/loc/src/routes/user.ts | 16 +++++----- backend/ts-backend/src/routes/auth.ts | 4 +++ backend/ts-backend/src/routes/user.ts | 4 +-- 9 files changed, 75 insertions(+), 49 deletions(-) delete mode 100644 backend/loc/prisma/migrations/20240421103750_init/migration.sql create mode 100644 backend/loc/prisma/migrations/20240428092122_dbseed/migration.sql diff --git a/backend/loc/prisma/migrations/20240421103750_init/migration.sql b/backend/loc/prisma/migrations/20240421103750_init/migration.sql deleted file mode 100644 index ec4d841..0000000 --- a/backend/loc/prisma/migrations/20240421103750_init/migration.sql +++ /dev/null @@ -1,27 +0,0 @@ --- CreateTable -CREATE TABLE "Location" ( - "id" TEXT NOT NULL DEFAULT gen_random_uuid(), - "latitude" DOUBLE PRECISION, - "longitude" DOUBLE PRECISION, - "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "Location_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "User" ( - "id" TEXT NOT NULL DEFAULT gen_random_uuid(), - "name" TEXT NOT NULL, - "email" TEXT NOT NULL, - "age" DOUBLE PRECISION NOT NULL, - "gender" TEXT NOT NULL, - "college" TEXT NOT NULL, - "isVisible" BOOLEAN NOT NULL, - "locationId" TEXT, - - CONSTRAINT "User_pkey" PRIMARY KEY ("id") -); - --- AddForeignKey -ALTER TABLE "User" ADD CONSTRAINT "User_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "Location"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/backend/loc/prisma/migrations/20240428092122_dbseed/migration.sql b/backend/loc/prisma/migrations/20240428092122_dbseed/migration.sql new file mode 100644 index 0000000..a5a4146 --- /dev/null +++ b/backend/loc/prisma/migrations/20240428092122_dbseed/migration.sql @@ -0,0 +1,30 @@ +-- CreateTable +CREATE TABLE "Location" ( + "id" UUID NOT NULL DEFAULT gen_random_uuid(), + "latitude" DOUBLE PRECISION NOT NULL, + "longitude" DOUBLE PRECISION NOT NULL, + "createdAt" TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(6) NOT NULL, + + CONSTRAINT "Location_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "User" ( + "id" UUID NOT NULL DEFAULT gen_random_uuid(), + "name" TEXT NOT NULL, + "email" TEXT NOT NULL, + "age" DOUBLE PRECISION NOT NULL, + "gender" TEXT NOT NULL, + "college" TEXT NOT NULL, + "isVisible" BOOLEAN NOT NULL, + "locationId" UUID, + + CONSTRAINT "User_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); + +-- AddForeignKey +ALTER TABLE "User" ADD CONSTRAINT "User_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "Location"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/backend/loc/prisma/schema.prisma b/backend/loc/prisma/schema.prisma index 1ab4ce1..d0b25ca 100644 --- a/backend/loc/prisma/schema.prisma +++ b/backend/loc/prisma/schema.prisma @@ -9,22 +9,22 @@ datasource db { } model Location { - id String @id @default(dbgenerated("gen_random_uuid()")) - latitude Float? - longitude Float? - createdAt DateTime @default(now()) - updatedAt DateTime + id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid + latitude Float + longitude Float + createdAt DateTime @default(now()) @db.Timestamp(6) + updatedAt DateTime @db.Timestamp(6) users User[] } model User { - id String @id @default(dbgenerated("gen_random_uuid()")) + id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid name String - email String + email String @unique age Float gender String college String isVisible Boolean - locationId String? - location Location? @relation(fields: [locationId], references: [id]) + locationId String? @db.Uuid + location Location? @relation(fields: [locationId], references: [id], onDelete: Cascade) } diff --git a/backend/loc/src/mockdata/Location.sql b/backend/loc/src/mockdata/Location.sql index 4ce053d..6c8d551 100644 --- a/backend/loc/src/mockdata/Location.sql +++ b/backend/loc/src/mockdata/Location.sql @@ -1,3 +1,12 @@ +-- Create the Location table +create table if not exists "Location" ( + "id" UUID DEFAULT gen_random_uuid() PRIMARY KEY, + "latitude" FLOAT NOT NULL, + "longitude" FLOAT NOT NULL, + "createdAt" TIMESTAMP DEFAULT NOW() NOT NULL, + "updatedAt" TIMESTAMP NOT NULL +); + insert into "Location" (id, latitude, longitude, "createdAt", "updatedAt") values ('86761b49-65d3-4b4a-92b0-dbe601fb9b13', 23.702434, 87.758946, '2023-10-19T13:28:50Z', '2024-04-23T17:29:09Z'); insert into "Location" (id, latitude, longitude, "createdAt", "updatedAt") values ('7f1e5f07-95a7-4d18-85ca-f47480999465', 22.885584, 87.640407, '2024-01-13T17:31:02Z', '2024-03-28T08:27:34Z'); insert into "Location" (id, latitude, longitude, "createdAt", "updatedAt") values ('b036ebaa-6740-4830-9dbe-7c90cd4277f1', 22.569763, 86.503533, '2023-12-26T05:26:55Z', '2024-03-20T00:19:18Z'); diff --git a/backend/loc/src/mockdata/User.sql b/backend/loc/src/mockdata/User.sql index edd4d05..e64476a 100644 --- a/backend/loc/src/mockdata/User.sql +++ b/backend/loc/src/mockdata/User.sql @@ -1,3 +1,15 @@ +-- Create the User table +create table if not EXISTS "User" ( + "id" UUID DEFAULT gen_random_uuid() PRIMARY KEY, + "name" TEXT NOT NULL, + "email" TEXT UNIQUE NOT NULL, + "age" FLOAT NOT NULL, + "gender" TEXT NOT NULL, + "college" TEXT NOT NULL, + "isVisible" BOOLEAN NOT NULL, + "locationId" UUID REFERENCES "Location"("id") ON DELETE CASCADE ON UPDATE CASCADE +); + insert into "User" (id, name, email, age, gender, college, "isVisible", "locationId") values ('16dc80c4-3da9-48f7-b4e8-7575df800ec1', 'Kent Petrina', 'kpetrina0@163.com', '51', 'Female', 'Calcutta University', TRUE, '86761b49-65d3-4b4a-92b0-dbe601fb9b13'); insert into "User" (id, name, email, age, gender, college, "isVisible", "locationId") values ('414dceb4-9c7f-4a6a-b3f4-16a3f7ac2afb', 'Loleta Blakemore', 'lblakemore1@parallels.com', '3', 'Male', 'Jadavpur University', TRUE, '7f1e5f07-95a7-4d18-85ca-f47480999465'); insert into "User" (id, name, email, age, gender, college, "isVisible", "locationId") values ('82fc9d3c-f61d-4562-8c9a-b27a41f66399', 'Elwood Devonish', 'edevonish2@cam.ac.uk', '69', 'Male', 'Calcutta University', TRUE, 'b036ebaa-6740-4830-9dbe-7c90cd4277f1'); diff --git a/backend/loc/src/routes/auth.ts b/backend/loc/src/routes/auth.ts index fa8db9b..a9df591 100644 --- a/backend/loc/src/routes/auth.ts +++ b/backend/loc/src/routes/auth.ts @@ -52,9 +52,9 @@ authRouter.post( id: uuidv4(), name, email, - age: 18, + age: 18.5, gender: "Non Binary", - college: "IIT", + college: "Jadavpur University", isVisible: true, }, }); diff --git a/backend/loc/src/routes/user.ts b/backend/loc/src/routes/user.ts index 129ae3c..c4526ea 100644 --- a/backend/loc/src/routes/user.ts +++ b/backend/loc/src/routes/user.ts @@ -58,9 +58,9 @@ userRouter.put( id: id, }, data: { - age, - college, - gender, + age: age, + college: college, + gender: gender, isVisible: visibility, }, }); @@ -78,8 +78,6 @@ userRouter.put( }, ); -// --------------- - const userLocationUpdateRequest = z.object({ id: z.string(), lat: z.number(), @@ -119,8 +117,8 @@ userRouter.put( return; } - // Check if the user already has a location - if (existingUser.locationId) { + // Check if the user already has a location, the update the previous loc + if (existingUser.locationId !== null) { // Update the user's location const updatedUser = await prisma.user.update({ where: { @@ -164,14 +162,14 @@ userRouter.put( }); res.status(HttpStatusCode.OK).json({ - msg: "User location updated successfully", + msg: "User location created successfully", user: updatedUser, }); } } catch (error) { console.error("Error updating user location:", error); res.status(HttpStatusCode.INTERNAL_SERVER_ERROR).json({ - msg: "Failed to update user location", + msg: "Failed to create / update user location", }); } }, diff --git a/backend/ts-backend/src/routes/auth.ts b/backend/ts-backend/src/routes/auth.ts index 1a76deb..2563ece 100644 --- a/backend/ts-backend/src/routes/auth.ts +++ b/backend/ts-backend/src/routes/auth.ts @@ -115,6 +115,10 @@ authRouter.post( username, email, password: hashedPassword, + age: 18.5, + gender: "Non Binary", + college: "Jadavpur University", + visibility: true, postgresId, }); await user.save(); diff --git a/backend/ts-backend/src/routes/user.ts b/backend/ts-backend/src/routes/user.ts index 958cc05..b34e470 100644 --- a/backend/ts-backend/src/routes/user.ts +++ b/backend/ts-backend/src/routes/user.ts @@ -393,8 +393,6 @@ userRouter.put( }, ); -// -------------------- - const setLocationRequest = z.object({ id: z.string(), lat: z.number(), @@ -424,9 +422,11 @@ userRouter.put( * lat: * type: number * description: Latitude of the user + * default: 22.123456 * lng: * type: number * description: Longitude of the user + * default: 88.654321 * responses: * 200: * description: Success