aboutsummaryrefslogtreecommitdiff
path: root/prisma
diff options
context:
space:
mode:
Diffstat (limited to 'prisma')
-rw-r--r--prisma/migrations/20230810051657_ondelete_cascade/migration.sql5
-rw-r--r--prisma/schema.prisma2
-rw-r--r--prisma/user.js48
3 files changed, 44 insertions, 11 deletions
diff --git a/prisma/migrations/20230810051657_ondelete_cascade/migration.sql b/prisma/migrations/20230810051657_ondelete_cascade/migration.sql
new file mode 100644
index 0000000..a521884
--- /dev/null
+++ b/prisma/migrations/20230810051657_ondelete_cascade/migration.sql
@@ -0,0 +1,5 @@
+-- DropForeignKey
+ALTER TABLE "WatchListEpisode" DROP CONSTRAINT "WatchListEpisode_userProfileId_fkey";
+
+-- AddForeignKey
+ALTER TABLE "WatchListEpisode" ADD CONSTRAINT "WatchListEpisode_userProfileId_fkey" FOREIGN KEY ("userProfileId") REFERENCES "UserProfile"("name") ON DELETE CASCADE ON UPDATE CASCADE;
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index f336e54..072415b 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -25,7 +25,7 @@ model WatchListEpisode {
duration Int?
provider String?
createdDate DateTime? @default(now())
- userProfile UserProfile @relation(fields: [userProfileId], references: [name])
+ userProfile UserProfile @relation(fields: [userProfileId], references: [name], onDelete: Cascade)
userProfileId String
watchId String
}
diff --git a/prisma/user.js b/prisma/user.js
index 8c436a5..dd61078 100644
--- a/prisma/user.js
+++ b/prisma/user.js
@@ -1,9 +1,9 @@
-// import { PrismaClient } from "@prisma/client";
+import { Prisma } from "@prisma/client";
// const prisma = new PrismaClient();
import { prisma } from "../lib/prisma";
-export const createUser = async (name, setting) => {
+export const createUser = async (name) => {
try {
const checkUser = await prisma.userProfile.findUnique({
where: {
@@ -22,6 +22,15 @@ export const createUser = async (name, setting) => {
return null;
}
} catch (error) {
+ if (error instanceof Prisma.PrismaClientKnownRequestError) {
+ if (error.code === "P2002") {
+ console.log(
+ "There is a unique constraint violation, a new user cannot be created with this name"
+ );
+ }
+ } else if (error instanceof Prisma.PrismaClientUnknownRequestError) {
+ console.log("An unknown Prisma error occurred:", error.message);
+ }
console.error(error);
throw new Error("Error creating user");
}
@@ -218,19 +227,38 @@ export const updateUserEpisode = async ({
}
};
-export const updateTimeWatched = async (id, timeWatched) => {
+export const deleteEpisode = async (name, id) => {
try {
- const user = await prisma.watchListEpisode.update({
+ const user = await prisma.watchListEpisode.deleteMany({
where: {
- id: id,
- },
- data: {
- timeWatched: timeWatched,
+ watchId: id,
+ userProfileId: name,
},
});
- return user;
+ if (user) {
+ return user;
+ } else {
+ return { message: "Episode not found" };
+ }
} catch (error) {
console.error(error);
- throw new Error("Error updating time watched");
+ throw new Error("Error deleting episode");
}
};
+
+// export const updateTimeWatched = async (id, timeWatched) => {
+// try {
+// const user = await prisma.watchListEpisode.update({
+// where: {
+// id: id,
+// },
+// data: {
+// timeWatched: timeWatched,
+// },
+// });
+// return user;
+// } catch (error) {
+// console.error(error);
+// throw new Error("Error updating time watched");
+// }
+// };