mirror of
https://github.com/p-stream/backend.git
synced 2026-01-11 20:10:33 +00:00
Optimized bookmarks endpoint to be less verbose
This commit is contained in:
parent
b956aa7297
commit
0e45ff1b70
2 changed files with 15 additions and 88 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
import { useAuth } from '~/utils/auth';
|
import { useAuth } from '~/utils/auth';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { scopedLogger } from '~/utils/logger';
|
import { scopedLogger } from '~/utils/logger';
|
||||||
import { bookmarks } from '@prisma/client';
|
|
||||||
|
|
||||||
const log = scopedLogger('user-bookmarks');
|
const log = scopedLogger('user-bookmarks');
|
||||||
|
|
||||||
|
|
@ -12,7 +11,6 @@ const bookmarkMetaSchema = z.object({
|
||||||
type: z.enum(['movie', 'show']),
|
type: z.enum(['movie', 'show']),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Support both formats: direct fields or nested under meta
|
|
||||||
const bookmarkRequestSchema = z.object({
|
const bookmarkRequestSchema = z.object({
|
||||||
meta: bookmarkMetaSchema.optional(),
|
meta: bookmarkMetaSchema.optional(),
|
||||||
tmdbId: z.string().optional(),
|
tmdbId: z.string().optional(),
|
||||||
|
|
@ -23,14 +21,10 @@ const bookmarkRequestSchema = z.object({
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
const userId = getRouterParam(event, 'id');
|
const userId = getRouterParam(event, 'id');
|
||||||
const tmdbId = getRouterParam(event, 'tmdbid');
|
const tmdbId = getRouterParam(event, 'tmdbid');
|
||||||
|
|
||||||
const session = await useAuth().getCurrentSession();
|
const session = await useAuth().getCurrentSession();
|
||||||
|
|
||||||
if (session.user !== userId) {
|
if (session.user !== userId) {
|
||||||
throw createError({
|
throw createError({ statusCode: 403, message: 'Cannot access bookmarks for other users' });
|
||||||
statusCode: 403,
|
|
||||||
message: 'Cannot access bookmarks for other users',
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.method === 'POST') {
|
if (event.method === 'POST') {
|
||||||
|
|
@ -38,51 +32,18 @@ export default defineEventHandler(async event => {
|
||||||
const body = await readBody(event);
|
const body = await readBody(event);
|
||||||
log.info('Creating bookmark', { userId, tmdbId, body });
|
log.info('Creating bookmark', { userId, tmdbId, body });
|
||||||
|
|
||||||
// Parse and validate the request body
|
const validated = bookmarkRequestSchema.parse(body);
|
||||||
const validatedRequest = bookmarkRequestSchema.parse(body);
|
const meta = bookmarkMetaSchema.parse(validated.meta || body);
|
||||||
|
const group = validated.group ? (Array.isArray(validated.group) ? validated.group : [validated.group]) : [];
|
||||||
// Extract the meta data - either directly from meta field or from the root
|
const favoriteEpisodes = validated.favoriteEpisodes || [];
|
||||||
const metaData = validatedRequest.meta || body;
|
|
||||||
|
|
||||||
// Validate the meta data separately
|
|
||||||
const validatedMeta = bookmarkMetaSchema.parse(metaData);
|
|
||||||
|
|
||||||
// Extract group from the validated request
|
|
||||||
const groupFromBody = validatedRequest.group;
|
|
||||||
|
|
||||||
// Normalize group to always be an array if present
|
|
||||||
const normalizedGroup = groupFromBody
|
|
||||||
? (Array.isArray(groupFromBody) ? groupFromBody : [groupFromBody])
|
|
||||||
: [];
|
|
||||||
|
|
||||||
// Normalize favoriteEpisodes to always be an array
|
|
||||||
const normalizedFavoriteEpisodes = validatedRequest.favoriteEpisodes || [];
|
|
||||||
|
|
||||||
const bookmark = await prisma.bookmarks.upsert({
|
const bookmark = await prisma.bookmarks.upsert({
|
||||||
where: {
|
where: { tmdb_id_user_id: { tmdb_id: tmdbId, user_id: session.user } },
|
||||||
tmdb_id_user_id: {
|
update: { meta, group, favorite_episodes: favoriteEpisodes, updated_at: new Date() },
|
||||||
tmdb_id: tmdbId,
|
create: { user_id: session.user, tmdb_id: tmdbId, meta, group, favorite_episodes: favoriteEpisodes, updated_at: new Date() },
|
||||||
user_id: session.user,
|
});
|
||||||
},
|
|
||||||
},
|
|
||||||
update: {
|
|
||||||
meta: validatedMeta,
|
|
||||||
group: normalizedGroup,
|
|
||||||
favorite_episodes: normalizedFavoriteEpisodes,
|
|
||||||
updated_at: new Date(),
|
|
||||||
} as any,
|
|
||||||
create: {
|
|
||||||
user_id: session.user,
|
|
||||||
tmdb_id: tmdbId,
|
|
||||||
meta: validatedMeta,
|
|
||||||
group: normalizedGroup,
|
|
||||||
favorite_episodes: normalizedFavoriteEpisodes,
|
|
||||||
updated_at: new Date(),
|
|
||||||
} as any,
|
|
||||||
}) as bookmarks;
|
|
||||||
|
|
||||||
log.info('Bookmark created successfully', { userId, tmdbId });
|
log.info('Bookmark created successfully', { userId, tmdbId });
|
||||||
|
|
||||||
return {
|
return {
|
||||||
tmdbId: bookmark.tmdb_id,
|
tmdbId: bookmark.tmdb_id,
|
||||||
meta: bookmark.meta,
|
meta: bookmark.meta,
|
||||||
|
|
@ -91,51 +52,20 @@ export default defineEventHandler(async event => {
|
||||||
updatedAt: bookmark.updated_at,
|
updatedAt: bookmark.updated_at,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error('Failed to create bookmark', {
|
log.error('Failed to create bookmark', { userId, tmdbId, error: error instanceof Error ? error.message : String(error) });
|
||||||
userId,
|
if (error instanceof z.ZodError) throw createError({ statusCode: 400, message: JSON.stringify(error.errors, null, 2) });
|
||||||
tmdbId,
|
|
||||||
error: error instanceof Error ? error.message : String(error),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (error instanceof z.ZodError) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 400,
|
|
||||||
message: JSON.stringify(error.errors, null, 2),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
} else if (event.method === 'DELETE') {
|
} else if (event.method === 'DELETE') {
|
||||||
log.info('Deleting bookmark', { userId, tmdbId });
|
log.info('Deleting bookmark', { userId, tmdbId });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await prisma.bookmarks.delete({
|
await prisma.bookmarks.delete({ where: { tmdb_id_user_id: { tmdb_id: tmdbId, user_id: session.user } } });
|
||||||
where: {
|
|
||||||
tmdb_id_user_id: {
|
|
||||||
tmdb_id: tmdbId,
|
|
||||||
user_id: session.user,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
log.info('Bookmark deleted successfully', { userId, tmdbId });
|
log.info('Bookmark deleted successfully', { userId, tmdbId });
|
||||||
|
|
||||||
return { success: true, tmdbId };
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error('Failed to delete bookmark', {
|
log.error('Failed to delete bookmark', { userId, tmdbId, error: error instanceof Error ? error.message : String(error) });
|
||||||
userId,
|
|
||||||
tmdbId,
|
|
||||||
error: error instanceof Error ? error.message : String(error),
|
|
||||||
});
|
|
||||||
|
|
||||||
// If bookmark doesn't exist, still return success
|
|
||||||
return { success: true, tmdbId };
|
|
||||||
}
|
}
|
||||||
|
return { success: true, tmdbId };
|
||||||
}
|
}
|
||||||
|
|
||||||
throw createError({
|
throw createError({ statusCode: 405, message: 'Method not allowed' });
|
||||||
statusCode: 405,
|
|
||||||
message: 'Method not allowed',
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
export default defineEventHandler(() => {
|
|
||||||
return;
|
|
||||||
});
|
|
||||||
Loading…
Reference in a new issue