trips) adding package-tours option
This commit is contained in:
39
src/routes/(protected)/admin/api/countries/+server.ts
Normal file
39
src/routes/(protected)/admin/api/countries/+server.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { requireAdmin } from '$lib/server/admin/auth.js';
|
||||
import * as data from '$lib/server/admin/data.js';
|
||||
|
||||
export const GET: RequestHandler = async (event) => {
|
||||
requireAdmin((await event.locals.auth())?.user?.id);
|
||||
const q = event.url.searchParams.get('q') ?? undefined;
|
||||
return json(data.listCountries(q));
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async (event) => {
|
||||
requireAdmin((await event.locals.auth())?.user?.id);
|
||||
const body = await event.request.json();
|
||||
const { name, country_code } = body as { name?: string; country_code?: string };
|
||||
if (!name?.trim() || !country_code?.trim()) {
|
||||
return json({ error: 'name and country_code required' }, { status: 400 });
|
||||
}
|
||||
return json(data.createCountry(name, country_code));
|
||||
};
|
||||
|
||||
export const PATCH: RequestHandler = async (event) => {
|
||||
requireAdmin((await event.locals.auth())?.user?.id);
|
||||
const body = await event.request.json();
|
||||
const { id, name, country_code } = body as { id?: number; name?: string; country_code?: string };
|
||||
if (id == null || !name?.trim() || !country_code?.trim()) {
|
||||
return json({ error: 'id, name and country_code required' }, { status: 400 });
|
||||
}
|
||||
data.updateCountry(id, name, country_code);
|
||||
return json({ ok: true });
|
||||
};
|
||||
|
||||
export const DELETE: RequestHandler = async (event) => {
|
||||
requireAdmin((await event.locals.auth())?.user?.id);
|
||||
const id = parseInt(event.url.searchParams.get('id') ?? '');
|
||||
if (isNaN(id)) return json({ error: 'id required' }, { status: 400 });
|
||||
data.deleteCountry(id);
|
||||
return json({ ok: true });
|
||||
};
|
||||
Reference in New Issue
Block a user