-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made the profile and edit profile flawless
- Loading branch information
1 parent
24a2a0a
commit eafbde6
Showing
18 changed files
with
668 additions
and
105 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
backend/prisma/migrations/20240629113525_newschema/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
}); |
Oops, something went wrong.