From f54b4f6553c57c37d4885785798bb92e10f44204 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Sat, 18 Nov 2023 19:57:03 +0000 Subject: [PATCH] Add bookmark import endpoint --- src/routes/users/bookmark.ts | 44 +++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/routes/users/bookmark.ts b/src/routes/users/bookmark.ts index 451cae2..4f2b909 100644 --- a/src/routes/users/bookmark.ts +++ b/src/routes/users/bookmark.ts @@ -6,8 +6,14 @@ import { import { StatusError } from '@/services/error'; import { handle } from '@/services/handler'; import { makeRouter } from '@/services/router'; +import { randomUUID } from 'crypto'; import { z } from 'zod'; +const bookmarkDataSchema = z.object({ + tmdbId: z.string(), + meta: bookmarkMetaSchema, +}); + export const userBookmarkRouter = makeRouter((app) => { app.get( '/users/:uid/bookmarks', @@ -40,9 +46,7 @@ export const userBookmarkRouter = makeRouter((app) => { uid: z.string(), tmdbid: z.string(), }), - body: z.object({ - meta: bookmarkMetaSchema, - }), + body: bookmarkDataSchema, }, }, handle(async ({ auth, params, body, em }) => { @@ -70,6 +74,40 @@ export const userBookmarkRouter = makeRouter((app) => { }), ); + app.put( + '/users/:uid/bookmarks', + { + schema: { + params: z.object({ + uid: z.string(), + }), + body: z.array(bookmarkDataSchema), + }, + }, + handle(async ({ auth, params, body, em }) => { + await auth.assert(); + + if (auth.user.id !== params.uid) + throw new StatusError('Cannot modify user other than yourself', 403); + + const bookmarks = await em.upsertMany( + Bookmark, + body.map((item) => ({ + userId: params.uid, + tmdbId: item.tmdbId, + meta: item.meta, + updatedAt: new Date(), + })), + { + onConflictFields: ['tmdbId', 'userId'], + }, + ); + + await em.flush(); + return bookmarks.map(formatBookmark); + }), + ); + app.delete( '/users/:uid/bookmarks/:tmdbid', {