Refactored schema and added link model.
This commit is contained in:
parent
cefeb5e7a9
commit
d19204f4c0
|
@ -27,15 +27,11 @@ export default function Layout({ children }: Props) {
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="icon" href="/favicon.ico" />
|
||||
</Head>
|
||||
<div className="flex">
|
||||
<Sidebar />
|
||||
<div className="ml-80 w-full">
|
||||
<div className="mx-auto w-full">
|
||||
<Navbar />
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
else if ((status === "unauthenticated" && !redirection) || !routeExists)
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { useEffect, useState } from "react";
|
||||
|
||||
interface Collections {
|
||||
id: any;
|
||||
id: number;
|
||||
name: string;
|
||||
role: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export default function Collections() {
|
||||
|
|
|
@ -14,7 +14,7 @@ export const authOptions: AuthOptions = {
|
|||
credentials: {},
|
||||
async authorize(credentials, req) {
|
||||
const { email, password } = credentials as {
|
||||
id: string;
|
||||
id: number;
|
||||
email: string;
|
||||
password: string;
|
||||
};
|
||||
|
@ -50,7 +50,7 @@ export const authOptions: AuthOptions = {
|
|||
},
|
||||
callbacks: {
|
||||
session: async ({ session, token }) => {
|
||||
session.user.id = token?.sub;
|
||||
session.user.id = parseInt(token?.sub as any);
|
||||
|
||||
return session;
|
||||
},
|
||||
|
|
|
@ -38,18 +38,11 @@ export default async function handler(
|
|||
email: body.email,
|
||||
password: hashedPassword,
|
||||
collections: {
|
||||
create: [
|
||||
{
|
||||
role: "owner",
|
||||
collection: {
|
||||
create: {
|
||||
name: "First Collection",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
res.status(201).json({ message: "User successfully created." });
|
||||
|
|
|
@ -24,16 +24,12 @@ export default async function handler(
|
|||
email: email,
|
||||
},
|
||||
include: {
|
||||
collections: {
|
||||
include: {
|
||||
collection: true,
|
||||
},
|
||||
},
|
||||
collections: true,
|
||||
},
|
||||
});
|
||||
|
||||
const collections = findCollection?.collections.map((e) => {
|
||||
return { id: e.collection.id, name: e.collection.name, role: e.role };
|
||||
return { id: e.id, name: e.name, createdAt: e.createdAt };
|
||||
});
|
||||
|
||||
// console.log(session?.user?.email);
|
||||
|
|
|
@ -30,17 +30,17 @@ export default async function handler(
|
|||
where: {
|
||||
email,
|
||||
},
|
||||
include: {
|
||||
select: {
|
||||
collections: {
|
||||
where: {
|
||||
collection: {
|
||||
name: collectionName,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log(typeof session.user.id);
|
||||
|
||||
const checkIfCollectionExists = findCollection?.collections[0];
|
||||
|
||||
if (checkIfCollectionExists) {
|
||||
|
@ -69,13 +69,8 @@ export default async function handler(
|
|||
collections: {
|
||||
create: [
|
||||
{
|
||||
role: "owner",
|
||||
collection: {
|
||||
create: {
|
||||
name: collectionName,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
-- CreateTable
|
||||
CREATE TABLE "User" (
|
||||
"id" TEXT NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"email" TEXT NOT NULL,
|
||||
"password" TEXT NOT NULL,
|
||||
|
||||
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Collection" (
|
||||
"id" TEXT NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
|
||||
CONSTRAINT "Collection_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "UserAndCollection" (
|
||||
"userId" TEXT NOT NULL,
|
||||
"collectionId" TEXT NOT NULL,
|
||||
"role" TEXT NOT NULL,
|
||||
|
||||
CONSTRAINT "UserAndCollection_pkey" PRIMARY KEY ("userId","collectionId")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "UserAndCollection" ADD CONSTRAINT "UserAndCollection_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "UserAndCollection" ADD CONSTRAINT "UserAndCollection_collectionId_fkey" FOREIGN KEY ("collectionId") REFERENCES "Collection"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
@ -0,0 +1,57 @@
|
|||
-- CreateTable
|
||||
CREATE TABLE "User" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"email" TEXT NOT NULL,
|
||||
"password" TEXT NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Collection" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"ownerId" INTEGER NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "Collection_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "UserAndCollection" (
|
||||
"userId" INTEGER NOT NULL,
|
||||
"collectionId" INTEGER NOT NULL,
|
||||
"canCreate" BOOLEAN NOT NULL,
|
||||
"canRead" BOOLEAN NOT NULL,
|
||||
"canUpdate" BOOLEAN NOT NULL,
|
||||
"canDelete" BOOLEAN NOT NULL,
|
||||
|
||||
CONSTRAINT "UserAndCollection_pkey" PRIMARY KEY ("userId","collectionId")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Link" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"url" TEXT NOT NULL,
|
||||
"collectionId" INTEGER NOT NULL,
|
||||
|
||||
CONSTRAINT "Link_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Collection" ADD CONSTRAINT "Collection_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "UserAndCollection" ADD CONSTRAINT "UserAndCollection_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "UserAndCollection" ADD CONSTRAINT "UserAndCollection_collectionId_fkey" FOREIGN KEY ("collectionId") REFERENCES "Collection"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Link" ADD CONSTRAINT "Link_collectionId_fkey" FOREIGN KEY ("collectionId") REFERENCES "Collection"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
@ -11,27 +11,44 @@ datasource db {
|
|||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid())
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
email String
|
||||
email String @unique
|
||||
password String
|
||||
collections UserAndCollection[]
|
||||
collections Collection[]
|
||||
collectionsJoined UserAndCollection[]
|
||||
createdAt DateTime @default(now())
|
||||
}
|
||||
|
||||
model Collection {
|
||||
id String @id @default(cuid())
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
users UserAndCollection[]
|
||||
owner User @relation(fields: [ownerId], references: [id])
|
||||
ownerId Int
|
||||
members UserAndCollection[]
|
||||
links Link[]
|
||||
createdAt DateTime @default(now())
|
||||
}
|
||||
|
||||
model UserAndCollection {
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
userId String
|
||||
userId Int
|
||||
|
||||
collection Collection @relation(fields: [collectionId], references: [id])
|
||||
collectionId String
|
||||
collectionId Int
|
||||
|
||||
role String
|
||||
canCreate Boolean
|
||||
canRead Boolean
|
||||
canUpdate Boolean
|
||||
canDelete Boolean
|
||||
|
||||
@@id([userId, collectionId])
|
||||
}
|
||||
|
||||
model Link {
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
url String
|
||||
collection Collection @relation(fields: [collectionId], references: [id])
|
||||
collectionId Int
|
||||
}
|
|
@ -3,7 +3,7 @@ import NextAuth, { DefaultSession } from "next-auth";
|
|||
declare module "next-auth" {
|
||||
interface Session {
|
||||
user: {
|
||||
id: any;
|
||||
id: number;
|
||||
} & DefaultSession["user"];
|
||||
}
|
||||
}
|
||||
|
|
Ŝarĝante…
Reference in New Issue