Skip to content

Commit

Permalink
made the profile and edit profile flawless
Browse files Browse the repository at this point in the history
  • Loading branch information
Anshpatel2434 committed Jul 2, 2024
1 parent 24a2a0a commit eafbde6
Show file tree
Hide file tree
Showing 18 changed files with 668 additions and 105 deletions.
9 changes: 5 additions & 4 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions backend/prisma/migrations/20240629113525_newschema/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Warnings:
- You are about to drop the `Customer` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `Order` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE "Order" DROP CONSTRAINT "Order_orderId_fkey";

-- DropTable
DROP TABLE "Customer";

-- DropTable
DROP TABLE "Order";

-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL,
"name" TEXT,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Profile" (
"id" TEXT NOT NULL,
"username" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"house_no" TEXT NOT NULL,
"street" TEXT NOT NULL,
"city_state" TEXT NOT NULL,
"area" TEXT NOT NULL,
"address" TEXT NOT NULL,
"phone_num" TEXT NOT NULL,

CONSTRAINT "Profile_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Rented" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"title" TEXT NOT NULL,
"type" TEXT NOT NULL,
"description" TEXT NOT NULL,
"quantity" INTEGER NOT NULL,
"availibility" BOOLEAN NOT NULL,
"availableFrom" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"image" TEXT,
"price" INTEGER NOT NULL,

CONSTRAINT "Rented_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Booked" (
"id" TEXT NOT NULL,
"rentedId" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"title" TEXT NOT NULL,
"type" TEXT NOT NULL,
"description" TEXT NOT NULL,
"availableTill" TIMESTAMP(3) NOT NULL,
"quantity" INTEGER NOT NULL,
"image" TEXT,
"price" INTEGER NOT NULL,

CONSTRAINT "Booked_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "Profile_userId_key" ON "Profile"("userId");

-- AddForeignKey
ALTER TABLE "Profile" ADD CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Rented" ADD CONSTRAINT "Rented_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Booked" ADD CONSTRAINT "Booked_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
54 changes: 44 additions & 10 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,53 @@ datasource db {
url = env("DATABASE_URL")
}

model Customer {
id Int @id @default(autoincrement())
model User {
id String @id @default(uuid())
name String?
email String @unique
password String
orders Order[]
profile Profile?
rented Rented[]
booked Booked[]
}

model Order{
id Int @id @default(autoincrement())
orderId Int
orderContent String
amount Float
orderAt DateTime @default(now())
customer Customer @relation(fields: [orderId], references: [id])
model Profile{
id String @id @default(uuid())
username String
user User @relation(fields: [userId], references: [id])
userId String @unique
house_no String
street String
city_state String
area String
address String
phone_num String
}
model Rented{
id String @id @default(uuid())
user User @relation(fields: [userId], references: [id])
userId String
title String
type String
description String
quantity Int
availibility Boolean
availableFrom DateTime @default(now())
image String?
price Int
}

model Booked{
id String @id @default(uuid())
user User @relation(fields: [userId], references: [id])
rentedId String
userId String
title String
type String
description String
availableTill DateTime
quantity Int
image String?
price Int
}

10 changes: 8 additions & 2 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Hono } from "hono";
import { userRouter } from "./routes/user";
import { orderRouter } from "./routes/order";
import { cors } from "hono/cors";
import { rentRouter } from "./routes/rent";
import { bookRouter } from "./routes/book";
import { rentAllRouter } from "./routes/rentAllRouter";
import { profileRouter } from "./routes/profile";

const app = new Hono();

app.use("/*", cors());
app.route("/api/v1/user", userRouter);
app.route("/api/v1/order", orderRouter);
app.route("/api/v1/rent", rentRouter);
app.route("/api/v1/book", bookRouter);
app.route("/api/v1/rentAll", rentAllRouter);
app.route("/api/v1/profile", profileRouter);

export default app;
103 changes: 103 additions & 0 deletions backend/src/routes/book.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Hono } from "hono";
import { PrismaClient } from "@prisma/client/edge";
import { withAccelerate } from "@prisma/extension-accelerate";
import { sign, verify } from "hono/jwt";
import { userRouter } from "./user";

export const bookRouter = new Hono<{
Bindings: {
DATABASE_URL: string;
JWT_SECRET: string;
};
Variables: {
userId: string;
};
}>();

bookRouter.use("/*", async (c, next) => {
const authHeader = c.req.header("authorization") || "";
try {
const user = await verify(authHeader, c.env.JWT_SECRET);
if (user) {
c.set("userId", user.id + "");
await next();
} else {
c.status(403);
return c.json({
message: "You are not logged in",
});
}
} catch (e) {
c.status(403);
return c.json({
message: "You are not logged in",
});
}
});

bookRouter.get("/all", async (c) => {
const prisma = new PrismaClient({
datasourceUrl: c.env.DATABASE_URL,
}).$extends(withAccelerate());
try {
const books = await prisma.booked.findMany({
where: {
userId: c.get("userId"),
},
});
return c.json({ books: books });
} catch (e) {
return c.json({
message: "Fail to fetch from db",
});
}
});

bookRouter.post("/add", async (c) => {
const body = await c.req.json();

const prisma = new PrismaClient({
datasourceUrl: c.env.DATABASE_URL,
}).$extends(withAccelerate());

try {
const book = await prisma.booked.create({
data: {
userId: c.get("userId"),
title: body.title,
type: body.type,
description: body.description,
quantity: Number(body.quantity),
rentedId: body.id,
availableTill: body.availableTill,
price: Number(body.price),
},
});
return c.json({
id: book.id,
});
} catch (e) {
return c.json({
message: e,
});
}
});

bookRouter.post("/delete", async (c) => {
const body = await c.req.json();
const prisma = new PrismaClient({
datasourceUrl: c.env.DATABASE_URL,
}).$extends(withAccelerate());
try {
const rent = await prisma.rented.delete({
where: { id: body.id },
});
return c.json({
message: "deleted",
});
} catch (e) {
return c.json({
message: e,
});
}
});
Loading

0 comments on commit eafbde6

Please sign in to comment.