mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 18:13:26 +00:00
add CRUD routes + prometheus client
Co-authored-by: James Hawkins <jhawki2005@gmail.com>
This commit is contained in:
100
src/routes/users/bookmark.ts
Normal file
100
src/routes/users/bookmark.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import {
|
||||
Bookmark,
|
||||
bookmarkMetaSchema,
|
||||
formatBookmark,
|
||||
} from '@/db/models/Bookmark';
|
||||
import { StatusError } from '@/services/error';
|
||||
import { handle } from '@/services/handler';
|
||||
import { makeRouter } from '@/services/router';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const userBookmarkRouter = makeRouter((app) => {
|
||||
app.get(
|
||||
'/users/:uid/bookmarks',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, em }) => {
|
||||
await auth.assert();
|
||||
|
||||
if (auth.user.id !== params.uid)
|
||||
throw new StatusError('Cannot access other user information', 403);
|
||||
|
||||
const bookmarks = await em.find(Bookmark, {
|
||||
userId: params.uid,
|
||||
});
|
||||
|
||||
return bookmarks.map(formatBookmark);
|
||||
}),
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/users/:uid/bookmarks/:tmdbid',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
uid: z.string(),
|
||||
tmdbid: z.string(),
|
||||
}),
|
||||
body: z.object({
|
||||
meta: bookmarkMetaSchema,
|
||||
}),
|
||||
},
|
||||
},
|
||||
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 oldBookmark = await em.findOne(Bookmark, {
|
||||
userId: params.uid,
|
||||
tmdbId: params.tmdbid,
|
||||
});
|
||||
if (oldBookmark) throw new StatusError('Already bookmarked', 400);
|
||||
|
||||
const bookmark = new Bookmark();
|
||||
em.assign(bookmark, {
|
||||
userId: params.uid,
|
||||
tmdbId: params.tmdbid,
|
||||
meta: body.meta,
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
|
||||
await em.persistAndFlush(bookmark);
|
||||
return formatBookmark(bookmark);
|
||||
}),
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/users/:uid/bookmarks/:tmdbid',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
uid: z.string(),
|
||||
tmdbid: z.string(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, em }) => {
|
||||
await auth.assert();
|
||||
|
||||
if (auth.user.id !== params.uid)
|
||||
throw new StatusError('Cannot modify user other than yourself', 403);
|
||||
|
||||
const bookmark = await em.findOne(Bookmark, {
|
||||
userId: params.uid,
|
||||
tmdbId: params.tmdbid,
|
||||
});
|
||||
|
||||
if (!bookmark) return { tmdbId: params.tmdbid };
|
||||
|
||||
await em.removeAndFlush(bookmark);
|
||||
return { tmdbId: params.tmdbid };
|
||||
}),
|
||||
);
|
||||
});
|
126
src/routes/users/progress.ts
Normal file
126
src/routes/users/progress.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
import {
|
||||
ProgressItem,
|
||||
formatProgressItem,
|
||||
progressMetaSchema,
|
||||
} from '@/db/models/ProgressItem';
|
||||
import { StatusError } from '@/services/error';
|
||||
import { handle } from '@/services/handler';
|
||||
import { makeRouter } from '@/services/router';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const userProgressRouter = makeRouter((app) => {
|
||||
app.put(
|
||||
'/users/:uid/progress/:tmdbid',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
uid: z.string(),
|
||||
tmdbid: z.string(),
|
||||
}),
|
||||
body: z.object({
|
||||
meta: progressMetaSchema,
|
||||
seasonId: z.string().optional(),
|
||||
episodeId: z.string().optional(),
|
||||
duration: z.number(),
|
||||
watched: z.number(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
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);
|
||||
|
||||
let progressItem = await em.findOne(ProgressItem, {
|
||||
userId: params.uid,
|
||||
tmdbId: params.tmdbid,
|
||||
episodeId: body.episodeId,
|
||||
seasonId: body.seasonId,
|
||||
});
|
||||
if (!progressItem) {
|
||||
progressItem = new ProgressItem();
|
||||
progressItem.tmdbId = params.tmdbid;
|
||||
progressItem.userId = params.uid;
|
||||
progressItem.episodeId = body.episodeId;
|
||||
progressItem.seasonId = body.seasonId;
|
||||
}
|
||||
|
||||
em.assign(progressItem, {
|
||||
duration: body.duration,
|
||||
watched: body.watched,
|
||||
meta: body.meta,
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
|
||||
await em.persistAndFlush(progressItem);
|
||||
return formatProgressItem(progressItem);
|
||||
}),
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/users/:uid/progress/:tmdbid',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
uid: z.string(),
|
||||
tmdbid: z.string(),
|
||||
}),
|
||||
body: z.object({
|
||||
seasonId: z.string().optional(),
|
||||
episodeId: z.string().optional(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
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 progressItem = await em.findOne(ProgressItem, {
|
||||
userId: params.uid,
|
||||
tmdbId: params.tmdbid,
|
||||
episodeId: body.episodeId,
|
||||
seasonId: body.seasonId,
|
||||
});
|
||||
if (!progressItem) {
|
||||
return {
|
||||
tmdbId: params.tmdbid,
|
||||
episodeId: body.episodeId,
|
||||
seasonId: body.seasonId,
|
||||
};
|
||||
}
|
||||
|
||||
await em.removeAndFlush(progressItem);
|
||||
return {
|
||||
tmdbId: params.tmdbid,
|
||||
episodeId: body.episodeId,
|
||||
seasonId: body.seasonId,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
app.get(
|
||||
'/users/:uid/progress',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, em }) => {
|
||||
await auth.assert();
|
||||
|
||||
if (auth.user.id !== params.uid)
|
||||
throw new StatusError('Cannot modify user other than yourself', 403);
|
||||
|
||||
const items = await em.find(ProgressItem, {
|
||||
userId: params.uid,
|
||||
});
|
||||
|
||||
return items.map(formatProgressItem);
|
||||
}),
|
||||
);
|
||||
});
|
72
src/routes/users/settings.ts
Normal file
72
src/routes/users/settings.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { UserSettings, formatUserSettings } from '@/db/models/UserSettings';
|
||||
import { StatusError } from '@/services/error';
|
||||
import { handle } from '@/services/handler';
|
||||
import { makeRouter } from '@/services/router';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const userSettingsRouter = makeRouter((app) => {
|
||||
app.get(
|
||||
'/users/:uid/settings',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, em }) => {
|
||||
await auth.assert();
|
||||
|
||||
if (auth.user.id !== params.uid)
|
||||
throw new StatusError('Cannot get other user information', 403);
|
||||
|
||||
const settings = await em.findOne(UserSettings, {
|
||||
id: params.uid,
|
||||
});
|
||||
|
||||
if (!settings) return { id: params.uid };
|
||||
|
||||
return formatUserSettings(settings);
|
||||
}),
|
||||
);
|
||||
|
||||
app.put(
|
||||
'/users/:uid/settings',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
body: z.object({
|
||||
applicationLanguage: z.string().optional(),
|
||||
applicationTheme: z.string().optional(),
|
||||
defaultSubtitleLanguage: z.string().optional(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
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);
|
||||
|
||||
let settings = await em.findOne(UserSettings, {
|
||||
id: params.uid,
|
||||
});
|
||||
if (!settings) {
|
||||
settings = new UserSettings();
|
||||
settings.id = params.uid;
|
||||
}
|
||||
|
||||
if (body.applicationLanguage)
|
||||
settings.applicationLanguage = body.applicationLanguage;
|
||||
if (body.applicationTheme)
|
||||
settings.applicationTheme = body.applicationTheme;
|
||||
if (body.defaultSubtitleLanguage)
|
||||
settings.defaultSubtitleLanguage = body.defaultSubtitleLanguage;
|
||||
|
||||
await em.persistAndFlush(settings);
|
||||
return formatUserSettings(settings);
|
||||
}),
|
||||
);
|
||||
});
|
Reference in New Issue
Block a user