add update session endpoint
This commit is contained in:
parent
b30623c483
commit
72657e73c8
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "backend",
|
"name": "backend",
|
||||||
"version": "1.1.2",
|
"version": "1.1.3",
|
||||||
"private": true,
|
"private": true,
|
||||||
"homepage": "https://github.com/movie-web/backend",
|
"homepage": "https://github.com/movie-web/backend",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Session } from '@/db/models/Session';
|
import { Session, formatSession } from '@/db/models/Session';
|
||||||
import { StatusError } from '@/services/error';
|
import { StatusError } from '@/services/error';
|
||||||
import { handle } from '@/services/handler';
|
import { handle } from '@/services/handler';
|
||||||
import { makeRouter } from '@/services/router';
|
import { makeRouter } from '@/services/router';
|
||||||
|
@ -32,4 +32,32 @@ export const sessionRouter = makeRouter((app) => {
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
app.patch(
|
||||||
|
'/sessions/:sid',
|
||||||
|
{
|
||||||
|
schema: {
|
||||||
|
params: z.object({
|
||||||
|
sid: z.string(),
|
||||||
|
}),
|
||||||
|
body: z.object({
|
||||||
|
name: z.string().optional(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
handle(async ({ auth, params, body, em }) => {
|
||||||
|
await auth.assert();
|
||||||
|
|
||||||
|
const targetedSession = await em.findOne(Session, { id: params.sid });
|
||||||
|
if (!targetedSession) throw new StatusError('Not found', 404);
|
||||||
|
if (targetedSession.id !== params.sid)
|
||||||
|
throw new StatusError('Cannot edit sessions other than your own', 401);
|
||||||
|
|
||||||
|
if (body.name) targetedSession.device = body.name;
|
||||||
|
|
||||||
|
await em.persistAndFlush(targetedSession);
|
||||||
|
|
||||||
|
return formatSession(targetedSession);
|
||||||
|
}),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue