diff --git a/Layouts/MainLayout.tsx b/Layouts/MainLayout.tsx index 57ffbf4..1e9c924 100644 --- a/Layouts/MainLayout.tsx +++ b/Layouts/MainLayout.tsx @@ -27,14 +27,10 @@ export default function Layout({ children }: Props) { -
- -
-
- - {children} -
-
+ +
+ + {children}
); diff --git a/components/Collections.tsx b/components/Collections.tsx index 99e1349..0cf0a2b 100644 --- a/components/Collections.tsx +++ b/components/Collections.tsx @@ -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() { diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts index 09e2193..2b9b237 100644 --- a/pages/api/auth/[...nextauth].ts +++ b/pages/api/auth/[...nextauth].ts @@ -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; }, diff --git a/pages/api/auth/register.ts b/pages/api/auth/register.ts index 4d7dab6..2c79af1 100644 --- a/pages/api/auth/register.ts +++ b/pages/api/auth/register.ts @@ -38,16 +38,9 @@ export default async function handler( email: body.email, password: hashedPassword, collections: { - create: [ - { - role: "owner", - collection: { - create: { - name: "First Collection", - }, - }, - }, - ], + create: { + name: "First Collection", + }, }, }, }); diff --git a/pages/api/routes/collections/getCollections.ts b/pages/api/routes/collections/getCollections.ts index c92faaa..6ab39d4 100644 --- a/pages/api/routes/collections/getCollections.ts +++ b/pages/api/routes/collections/getCollections.ts @@ -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); diff --git a/pages/api/routes/collections/postCollection.ts b/pages/api/routes/collections/postCollection.ts index eef1636..61fc928 100644 --- a/pages/api/routes/collections/postCollection.ts +++ b/pages/api/routes/collections/postCollection.ts @@ -30,17 +30,17 @@ export default async function handler( where: { email, }, - include: { + select: { collections: { where: { - collection: { - name: collectionName, - }, + name: collectionName, }, }, }, }); + console.log(typeof session.user.id); + const checkIfCollectionExists = findCollection?.collections[0]; if (checkIfCollectionExists) { @@ -69,12 +69,7 @@ export default async function handler( collections: { create: [ { - role: "owner", - collection: { - create: { - name: collectionName, - }, - }, + name: collectionName, }, ], }, diff --git a/prisma/migrations/20230131171711_/migration.sql b/prisma/migrations/20230131171711_/migration.sql deleted file mode 100644 index 4aff8b8..0000000 --- a/prisma/migrations/20230131171711_/migration.sql +++ /dev/null @@ -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; diff --git a/prisma/migrations/20230213224230_/migration.sql b/prisma/migrations/20230213224230_/migration.sql new file mode 100644 index 0000000..546f3c0 --- /dev/null +++ b/prisma/migrations/20230213224230_/migration.sql @@ -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; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 29df03b..c71b3c0 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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 } \ No newline at end of file diff --git a/types/next-auth.d.ts b/types/next-auth.d.ts index ce3db1a..cb9674b 100644 --- a/types/next-auth.d.ts +++ b/types/next-auth.d.ts @@ -3,7 +3,7 @@ import NextAuth, { DefaultSession } from "next-auth"; declare module "next-auth" { interface Session { user: { - id: any; + id: number; } & DefaultSession["user"]; } }