This commit is contained in:
88
src/actions/user.ts
Normal file
88
src/actions/user.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { ActionError, defineAction } from 'astro:actions';
|
||||
import { z } from 'astro:schema';
|
||||
import { db } from '@db/database.ts';
|
||||
import { Session } from '@util/session.ts';
|
||||
import { Permissions } from '@util/permissions.ts';
|
||||
|
||||
export const user = {
|
||||
addUser: defineAction({
|
||||
input: z.object({
|
||||
firstname: z.string(),
|
||||
lastname: z.string(),
|
||||
birthday: z.string().date(),
|
||||
telephone: z.string().nullable(),
|
||||
username: z.string(),
|
||||
uuid: z.string().nullable()
|
||||
}),
|
||||
handler: async (input, context) => {
|
||||
Session.actionSessionFromCookies(context.cookies, Permissions.Users);
|
||||
|
||||
if (await db.existsUser({ username: input.username })) {
|
||||
throw new ActionError({
|
||||
code: 'CONFLICT',
|
||||
message: 'Der Benutzername ist bereits registriert'
|
||||
});
|
||||
}
|
||||
|
||||
const { id } = await db.addUser({
|
||||
firstname: input.firstname,
|
||||
lastname: input.lastname,
|
||||
birthday: input.birthday,
|
||||
telephone: input.telephone,
|
||||
username: input.username,
|
||||
uuid: input.uuid
|
||||
});
|
||||
|
||||
return {
|
||||
id: id
|
||||
};
|
||||
}
|
||||
}),
|
||||
editUser: defineAction({
|
||||
input: z.object({
|
||||
id: z.number(),
|
||||
firstname: z.string(),
|
||||
lastname: z.string(),
|
||||
birthday: z.string().date(),
|
||||
telephone: z.string().nullable(),
|
||||
username: z.string(),
|
||||
uuid: z.string().nullable()
|
||||
}),
|
||||
handler: async (input, context) => {
|
||||
Session.actionSessionFromCookies(context.cookies, Permissions.Users);
|
||||
|
||||
const user = await db.existsUser({ username: input.username });
|
||||
if (user && user.id !== input.id) {
|
||||
throw new ActionError({
|
||||
code: 'CONFLICT',
|
||||
message: 'Ein Spieler mit dem Benutzernamen existiert bereits'
|
||||
});
|
||||
}
|
||||
|
||||
await db.editUser({
|
||||
id: input.id,
|
||||
firstname: input.firstname,
|
||||
lastname: input.lastname,
|
||||
birthday: input.birthday,
|
||||
telephone: input.telephone,
|
||||
username: input.username,
|
||||
uuid: input.uuid
|
||||
});
|
||||
}
|
||||
}),
|
||||
users: defineAction({
|
||||
input: z.object({
|
||||
username: z.string().nullish(),
|
||||
limit: z.number().optional()
|
||||
}),
|
||||
handler: async (input, context) => {
|
||||
Session.actionSessionFromCookies(context.cookies, Permissions.Users);
|
||||
|
||||
const users = await db.getUsers(input);
|
||||
|
||||
return {
|
||||
users: users
|
||||
};
|
||||
}
|
||||
})
|
||||
};
|
Reference in New Issue
Block a user