mirror of
https://github.com/p-stream/backend.git
synced 2026-01-11 20:10:33 +00:00
Update watch-history.ts
This commit is contained in:
parent
9120767784
commit
54fb091b18
1 changed files with 0 additions and 112 deletions
|
|
@ -76,118 +76,6 @@ export default defineEventHandler(async event => {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.path.includes('/watch-history/')) {
|
|
||||||
const segments = event.path.split('/');
|
|
||||||
const tmdbId = segments[segments.length - 1];
|
|
||||||
|
|
||||||
if (method === 'PUT') {
|
|
||||||
const body = await readBody(event);
|
|
||||||
const validatedBody = watchHistoryItemSchema.parse(body);
|
|
||||||
|
|
||||||
const watchedAt = defaultAndCoerceDateTime(validatedBody.watchedAt);
|
|
||||||
const now = new Date();
|
|
||||||
|
|
||||||
const existingItem = await prisma.watch_history.findUnique({
|
|
||||||
where: {
|
|
||||||
tmdb_id_user_id_season_id_episode_id: {
|
|
||||||
tmdb_id: tmdbId,
|
|
||||||
user_id: userId,
|
|
||||||
season_id: validatedBody.seasonId || null,
|
|
||||||
episode_id: validatedBody.episodeId || null,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
let watchHistoryItem;
|
|
||||||
|
|
||||||
if (existingItem) {
|
|
||||||
watchHistoryItem = await prisma.watch_history.update({
|
|
||||||
where: {
|
|
||||||
id: existingItem.id,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
duration: BigInt(validatedBody.duration),
|
|
||||||
watched: BigInt(validatedBody.watched),
|
|
||||||
watched_at: watchedAt,
|
|
||||||
completed: validatedBody.completed,
|
|
||||||
meta: validatedBody.meta,
|
|
||||||
updated_at: now,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
watchHistoryItem = await prisma.watch_history.create({
|
|
||||||
data: {
|
|
||||||
id: randomUUID(),
|
|
||||||
tmdb_id: tmdbId,
|
|
||||||
user_id: userId,
|
|
||||||
season_id: validatedBody.seasonId || null,
|
|
||||||
episode_id: validatedBody.episodeId || null,
|
|
||||||
season_number: validatedBody.seasonNumber || null,
|
|
||||||
episode_number: validatedBody.episodeNumber || null,
|
|
||||||
duration: BigInt(validatedBody.duration),
|
|
||||||
watched: BigInt(validatedBody.watched),
|
|
||||||
watched_at: watchedAt,
|
|
||||||
completed: validatedBody.completed,
|
|
||||||
meta: validatedBody.meta,
|
|
||||||
updated_at: now,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: watchHistoryItem.id,
|
|
||||||
tmdbId: watchHistoryItem.tmdb_id,
|
|
||||||
userId: watchHistoryItem.user_id,
|
|
||||||
seasonId: watchHistoryItem.season_id,
|
|
||||||
episodeId: watchHistoryItem.episode_id,
|
|
||||||
seasonNumber: watchHistoryItem.season_number,
|
|
||||||
episodeNumber: watchHistoryItem.episode_number,
|
|
||||||
meta: watchHistoryItem.meta,
|
|
||||||
duration: Number(watchHistoryItem.duration),
|
|
||||||
watched: Number(watchHistoryItem.watched),
|
|
||||||
watchedAt: watchHistoryItem.watched_at.toISOString(),
|
|
||||||
completed: watchHistoryItem.completed,
|
|
||||||
updatedAt: watchHistoryItem.updated_at.toISOString(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (method === 'DELETE') {
|
|
||||||
const body = await readBody(event).catch(() => ({}));
|
|
||||||
|
|
||||||
const whereClause: any = {
|
|
||||||
user_id: userId,
|
|
||||||
tmdb_id: tmdbId,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (body.seasonId) whereClause.season_id = body.seasonId;
|
|
||||||
if (body.episodeId) whereClause.episode_id = body.episodeId;
|
|
||||||
|
|
||||||
const itemsToDelete = await prisma.watch_history.findMany({
|
|
||||||
where: whereClause,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (itemsToDelete.length === 0) {
|
|
||||||
return {
|
|
||||||
count: 0,
|
|
||||||
tmdbId,
|
|
||||||
episodeId: body.episodeId,
|
|
||||||
seasonId: body.seasonId,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
await prisma.watch_history.deleteMany({
|
|
||||||
where: whereClause,
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
count: itemsToDelete.length,
|
|
||||||
tmdbId,
|
|
||||||
episodeId: body.episodeId,
|
|
||||||
seasonId: body.seasonId,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw createError({
|
throw createError({
|
||||||
statusCode: 405,
|
statusCode: 405,
|
||||||
message: 'Method not allowed',
|
message: 'Method not allowed',
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue