Skip to content

Commit

Permalink
[fix]: API payloads, db schema, seed default values
Browse files Browse the repository at this point in the history
  • Loading branch information
sanam2405 committed Apr 28, 2024
1 parent 2b22eaf commit 38c336c
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 49 deletions.
27 changes: 0 additions & 27 deletions backend/loc/prisma/migrations/20240421103750_init/migration.sql

This file was deleted.

30 changes: 30 additions & 0 deletions backend/loc/prisma/migrations/20240428092122_dbseed/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 9 additions & 9 deletions backend/loc/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 9 additions & 0 deletions backend/loc/src/mockdata/Location.sql
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
12 changes: 12 additions & 0 deletions backend/loc/src/mockdata/User.sql
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
4 changes: 2 additions & 2 deletions backend/loc/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
Expand Down
16 changes: 7 additions & 9 deletions backend/loc/src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ userRouter.put(
id: id,
},
data: {
age,
college,
gender,
age: age,
college: college,
gender: gender,
isVisible: visibility,
},
});
Expand All @@ -78,8 +78,6 @@ userRouter.put(
},
);

// ---------------

const userLocationUpdateRequest = z.object({
id: z.string(),
lat: z.number(),
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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",
});
}
},
Expand Down
4 changes: 4 additions & 0 deletions backend/ts-backend/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions backend/ts-backend/src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,6 @@ userRouter.put(
},
);

// --------------------

const setLocationRequest = z.object({
id: z.string(),
lat: z.number(),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 38c336c

Please sign in to comment.