This commit is contained in:
1 parent
3fe7a12481
commit
0259aad7fd
2 files changed
+96
-4
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { z } from 'astro:schema';
|
||||
import { db } from '@db/database.ts';
|
||||
import { externalApi } from '../_api.ts';
|
||||
|
||||
export const PUT = externalApi({
|
||||
auth: true,
|
||||
input: z.object({
|
||||
firstname: z.string().trim().min(1),
|
||||
lastname: z.string().trim().min(1),
|
||||
birthday: z.string().date(),
|
||||
phone: z.string().trim(),
|
||||
username: z.string().trim().min(1),
|
||||
uuid: z.string().uuid(),
|
||||
edition: z.enum(['java', 'bedrock'])
|
||||
}),
|
||||
handler: async ({ input }) => {
|
||||
const existing = await db.existsUser({
|
||||
username: input.username,
|
||||
uuid: input.uuid
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return new Response(JSON.stringify({ error: 'User with this username or uuid already exists' }), {
|
||||
status: 409,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const user = await db.addUser({
|
||||
firstname: input.firstname,
|
||||
lastname: input.lastname,
|
||||
birthday: input.birthday,
|
||||
telephone: input.phone || null,
|
||||
username: input.username,
|
||||
edition: input.edition,
|
||||
uuid: input.uuid
|
||||
});
|
||||
|
||||
return new Response(JSON.stringify({ id: user.id }), {
|
||||
status: 201,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user