Many changes.

This commit is contained in:
Daniel
2023-02-19 07:02:02 +03:30
parent d19204f4c0
commit e0f4c71eb2
19 changed files with 282 additions and 122 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
import { prisma } from "@/lib/db";
import { prisma } from "@/lib/api/db";
import NextAuth from "next-auth/next";
import CredentialsProvider from "next-auth/providers/credentials";
import { AuthOptions } from "next-auth";
+1 -1
View File
@@ -1,4 +1,4 @@
import { prisma } from "@/lib/db";
import { prisma } from "@/lib/api/db";
import type { NextApiRequest, NextApiResponse } from "next";
import bcrypt from "bcrypt";
@@ -1,7 +1,8 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import { authOptions } from "pages/api/auth/[...nextauth]";
import { prisma } from "@/lib/db";
import getCollections from "@/lib/api/controllers/collections/getCollections";
import postCollections from "@/lib/api/controllers/collections/postCollection";
type Data = {
response: object[] | string;
@@ -17,24 +18,7 @@ export default async function handler(
return res.status(401).json({ response: "You must be logged in." });
}
const email: string = session.user.email;
if (req.method === "GET") return await getCollections(req, res, session);
const findCollection = await prisma.user.findFirst({
where: {
email: email,
},
include: {
collections: true,
},
});
const collections = findCollection?.collections.map((e) => {
return { id: e.id, name: e.name, createdAt: e.createdAt };
});
// console.log(session?.user?.email);
return res.status(200).json({
response: collections || [],
});
if (req.method === "POST") return await postCollections(req, res, session);
}
@@ -1,82 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import { authOptions } from "pages/api/auth/[...nextauth]";
import { prisma } from "@/lib/db";
type Data = {
response: object[] | string;
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
const session = await getServerSession(req, res, authOptions);
if (!session?.user?.email) {
return res.status(401).json({ response: "You must be logged in." });
}
const email: string = session.user.email;
const collectionName: string = req?.body?.collectionName;
if (!collectionName) {
return res
.status(401)
.json({ response: "Please enter a valid name for the collection." });
}
const findCollection = await prisma.user.findFirst({
where: {
email,
},
select: {
collections: {
where: {
name: collectionName,
},
},
},
});
console.log(typeof session.user.id);
const checkIfCollectionExists = findCollection?.collections[0];
if (checkIfCollectionExists) {
return res.status(400).json({ response: "Collection already exists." });
}
// const a = await prisma.user.update({
// where: {
// id: session.user.id,
// },
// data: {
// // collections: {
// // create: { name: "Das" },
// // },
// },
// include: {
// collections: { include: { collection: true } },
// },
// });
await prisma.user.update({
where: {
id: session.user.id,
},
data: {
collections: {
create: [
{
name: collectionName,
},
],
},
},
});
return res.status(200).json({
response: "Success",
});
}