diff options
| author | Factiven <[email protected]> | 2023-12-24 13:03:54 +0700 |
|---|---|---|
| committer | Factiven <[email protected]> | 2023-12-24 13:03:54 +0700 |
| commit | 50a0f0240d7fef133eb5acc1bea2b1168b08e9db (patch) | |
| tree | 307e09e505580415a58d64b5fc3580e9235869f1 /prisma | |
| parent | Update README.md (#104) (diff) | |
| download | moopa-50a0f0240d7fef133eb5acc1bea2b1168b08e9db.tar.xz moopa-50a0f0240d7fef133eb5acc1bea2b1168b08e9db.zip | |
migrate to typescript
Diffstat (limited to 'prisma')
| -rw-r--r-- | prisma/user.ts (renamed from prisma/user.js) | 112 |
1 files changed, 51 insertions, 61 deletions
diff --git a/prisma/user.js b/prisma/user.ts index c2ba5fd..8a0d856 100644 --- a/prisma/user.js +++ b/prisma/user.ts @@ -1,9 +1,24 @@ -import { Prisma } from "@prisma/client"; -// const prisma = new PrismaClient(); +import { Prisma, UserProfile, WatchListEpisode } from "@prisma/client"; import { prisma } from "../lib/prisma"; -export const createUser = async (name) => { +interface UpdateUserEpisodeParams { + name: string; + id: string; + watchId: string; + title: string; + image: string; + number: number; + duration: number; + timeWatched: number; + aniTitle: string; + provider: string; + nextId: string; + nextNumber: number; + dub: boolean; +} + +export const createUser = async (name: string): Promise<UserProfile | null> => { try { const checkUser = await prisma.userProfile.findUnique({ where: { @@ -36,9 +51,12 @@ export const createUser = async (name) => { } }; -export const updateUser = async (name, setting) => { +export const updateUser = async ( + name: string, + setting: any +): Promise<{ name: string; setting: any } | null> => { try { - const user = await prisma.userProfile.updateMany({ + await prisma.userProfile.updateMany({ where: { name: name, }, @@ -46,58 +64,17 @@ export const updateUser = async (name, setting) => { setting, }, }); - return user; - // const checkAnime = await prisma.watchListItem.findUnique({ - // where: { - // title: anime.title, - // userProfileId: name, - // }, - // }); - // if (checkAnime) { - // const checkEpisode = await prisma.watchListEpisode.findUnique({ - // where: { - // url: anime.id, - // }, - // }); - // if (checkEpisode) { - // return null; - // } else { - // const user = await prisma.watchListItem.update({ - // where: { - // title: anime.title, - // userProfileId: name, - // }, - // }); - // } - // } else { - // const user = await prisma.userProfile.update({ - // where: { name: name }, - // data: { - // watchList: { - // create: { - // title: anime.title, - // episodes: { - // create: { - // url: anime.id, - // }, - // }, - // }, - // }, - // }, - // include: { - // watchList: true, - // }, - // }); - - // return user; - // } + return { name: name, setting: setting }; } catch (error) { console.error(error); throw new Error("Error updating user"); } }; -export const getUser = async (name, list = true) => { +export const getUser = async ( + name: string, + list = true +): Promise<any | null> => { try { if (!name) { const user = await prisma.userProfile.findMany({ @@ -127,7 +104,7 @@ export const getUser = async (name, list = true) => { } }; -export const deleteUser = async (name) => { +export const deleteUser = async (name: string): Promise<UserProfile | null> => { try { const user = await prisma.userProfile.delete({ where: { @@ -141,7 +118,11 @@ export const deleteUser = async (name) => { } }; -export const createList = async (name, id, title) => { +export const createList = async ( + name: string, + id: string, + title: string +): Promise<UserProfile | null> => { try { const checkEpisode = await prisma.watchListEpisode.findFirst({ where: { @@ -175,7 +156,10 @@ export const createList = async (name, id, title) => { } }; -export const getEpisode = async (name, id) => { +export const getEpisode = async ( + name: string, + id: string +): Promise<WatchListEpisode[] | null> => { try { const episode = await prisma.watchListEpisode.findMany({ where: { @@ -212,9 +196,9 @@ export const updateUserEpisode = async ({ nextId, nextNumber, dub, -}) => { +}: UpdateUserEpisodeParams) => { try { - const user = await prisma.watchListEpisode.updateMany({ + await prisma.watchListEpisode.updateMany({ where: { userProfileId: name, watchId: watchId, @@ -235,14 +219,17 @@ export const updateUserEpisode = async ({ }, }); - return user; + // return user; } catch (error) { console.error(error); throw new Error("Error updating user episode"); } }; -export const deleteEpisode = async (name, id) => { +export const deleteEpisode = async ( + name: string, + id: string +): Promise<{ success?: boolean; message?: string } | null> => { try { const user = await prisma.watchListEpisode.deleteMany({ where: { @@ -251,7 +238,7 @@ export const deleteEpisode = async (name, id) => { }, }); if (user) { - return user; + return { success: true }; } else { return { message: "Episode not found" }; } @@ -261,7 +248,10 @@ export const deleteEpisode = async (name, id) => { } }; -export const deleteList = async (name, id) => { +export const deleteList = async ( + name: string, + id: string +): Promise<{ success?: boolean; message?: string } | null> => { try { const user = await prisma.watchListEpisode.deleteMany({ where: { @@ -270,7 +260,7 @@ export const deleteList = async (name, id) => { }, }); if (user) { - return user; + return { success: true }; } else { return { message: "Episode not found" }; } |