mirror of
https://github.com/vcscsvcscs/GenerationsHeritage.git
synced 2025-08-13 22:39:06 +02:00
implement api proxy
This commit is contained in:
@@ -1,56 +1,79 @@
|
||||
import type { components } from '$lib/api/api.gen.js';
|
||||
import { first_name, last_name, missing_field, mothers_first_name } from '$lib/paraglide/messages';
|
||||
|
||||
export function validatePersonRegistration(data: components['schemas']['PersonRegistration']): string | null {
|
||||
if (!data.first_name || data.first_name.trim() === "") {
|
||||
return "First name is required.";
|
||||
}
|
||||
export function validatePersonRegistration(
|
||||
data: components['schemas']['PersonRegistration']
|
||||
): string | null {
|
||||
if (!data.first_name || data.first_name.trim() === '') {
|
||||
return missing_field({
|
||||
field: first_name(),
|
||||
});
|
||||
}
|
||||
|
||||
if (!data.last_name || data.last_name.trim() === "") {
|
||||
return "Last name is required.";
|
||||
}
|
||||
if (!data.last_name || data.last_name.trim() === '') {
|
||||
return missing_field({
|
||||
field: last_name(),
|
||||
});
|
||||
}
|
||||
|
||||
if (data.email !== undefined && data.email !== null && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data.email)) {
|
||||
return "Invalid email format.";
|
||||
}
|
||||
if (
|
||||
data.email !== undefined &&
|
||||
data.email !== null &&
|
||||
!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data.email)
|
||||
) {
|
||||
return 'Invalid email format.';
|
||||
}
|
||||
|
||||
if (!data.born || isNaN(Date.parse(data.born))) {
|
||||
return "Valid birth date is required.";
|
||||
}
|
||||
if (!data.born || !Date.parse(data.born)) {
|
||||
return 'Valid birth date is required.';
|
||||
}
|
||||
|
||||
if (!data.biological_sex || !['male', 'female', 'intersex', 'unknown', 'other'].includes(data.biological_sex.toString())){
|
||||
return 'Invalid value for biological sex. Must be male female, intersex, unknown, or other.';
|
||||
}
|
||||
if (
|
||||
!data.biological_sex ||
|
||||
!['male', 'female', 'intersex', 'unknown', 'other'].includes(data.biological_sex.toString())
|
||||
) {
|
||||
return 'Invalid value for biological sex. Must be male female, intersex, unknown, or other.';
|
||||
}
|
||||
|
||||
if (!data.mothers_first_name || data.mothers_first_name.trim() === "") {
|
||||
return "Mother's first name is required.";
|
||||
}
|
||||
if (!data.mothers_first_name || data.mothers_first_name.trim() === '') {
|
||||
return missing_field({
|
||||
field: mothers_first_name(),
|
||||
});
|
||||
}
|
||||
|
||||
if (!data.mothers_last_name || data.mothers_last_name.trim() === "") {
|
||||
return "Mother's last name is required.";
|
||||
}
|
||||
if (!data.mothers_last_name || data.mothers_last_name.trim() === '') {
|
||||
return missing_field({
|
||||
field: 'Mother\'s last name',
|
||||
});
|
||||
}
|
||||
|
||||
return null; // No errors
|
||||
return null; // No errors
|
||||
}
|
||||
|
||||
export function validateFamilyRelationship(relationship: components['schemas']['FamilyRelationship'] & {type:string}): string | null {
|
||||
const validRelationships = [
|
||||
"child",
|
||||
"parent",
|
||||
"spouse",
|
||||
"sibling"
|
||||
];
|
||||
export function validateFamilyRelationship(
|
||||
relationship: components['schemas']['FamilyRelationship'] & { type: string }
|
||||
): string | null {
|
||||
const validRelationships = ['child', 'parent', 'spouse', 'sibling'];
|
||||
|
||||
if (!validRelationships.includes(relationship.type)) {
|
||||
return `Invalid family relationship. Must be one of ${validRelationships.join(', ')}.`;
|
||||
}
|
||||
if (!validRelationships.includes(relationship.type)) {
|
||||
return `Invalid family relationship. Must be one of ${validRelationships.join(', ')}.`;
|
||||
}
|
||||
|
||||
if (relationship.from !== undefined && relationship.from !== null && isNaN(Date.parse(relationship.from))) {
|
||||
return "Valid date is required for 'from' field.";
|
||||
}
|
||||
if (
|
||||
relationship.from !== undefined &&
|
||||
relationship.from !== null &&
|
||||
isNaN(Date.parse(relationship.from))
|
||||
) {
|
||||
return "Valid date is required for 'from' field.";
|
||||
}
|
||||
|
||||
if (relationship.to !== undefined && relationship.to !== null && isNaN(Date.parse(relationship.to))) {
|
||||
return "Valid date is required for 'to' field.";
|
||||
}
|
||||
if (
|
||||
relationship.to !== undefined &&
|
||||
relationship.to !== null &&
|
||||
isNaN(Date.parse(relationship.to))
|
||||
) {
|
||||
return "Valid date is required for 'to' field.";
|
||||
}
|
||||
|
||||
return null; // No errors
|
||||
}
|
||||
return null; // No errors
|
||||
}
|
||||
|
24
apps/app/src/routes/api/admin/[ID1]/+server.ts
Normal file
24
apps/app/src/routes/api/admin/[ID1]/+server.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { client } from '$lib/api/client'
|
||||
import { redirect } from '@sveltejs/kit'
|
||||
import type { RequestEvent } from './$types'
|
||||
|
||||
|
||||
export async function GET(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.GET(
|
||||
'/admin/{id1}',
|
||||
{
|
||||
params: {
|
||||
path: { id1: Number(event.params.ID1) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
64
apps/app/src/routes/api/admin/[ID1]/[ID2]/+server.ts
Normal file
64
apps/app/src/routes/api/admin/[ID1]/[ID2]/+server.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { client } from '$lib/api/client'
|
||||
import { redirect } from '@sveltejs/kit'
|
||||
import type { RequestEvent } from './$types'
|
||||
|
||||
|
||||
export async function GET(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.GET(
|
||||
'/admin/{id1}/{id2}',
|
||||
{
|
||||
params: {
|
||||
path: { id1: Number(event.params.ID1), id2: Number(event.params.ID2) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.POST(
|
||||
'/admin/{id1}/{id2}',
|
||||
{
|
||||
params: {
|
||||
path: { id1: Number(event.params.ID1), id2: Number(event.params.ID2) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
||||
|
||||
export async function DELETE(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.DELETE(
|
||||
'/admin/{id1}/{id2}',
|
||||
{
|
||||
params: {
|
||||
path: { id1: Number(event.params.ID1), id2: Number(event.params.ID2) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
94
apps/app/src/routes/api/comment/[ID]/+server.ts
Normal file
94
apps/app/src/routes/api/comment/[ID]/+server.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { client } from '$lib/api/client';
|
||||
import type { RequestEvent } from './$types';
|
||||
import type { components } from '$lib/api/api.gen';
|
||||
|
||||
|
||||
export async function POST(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
let message = await event.request.json() as components['schemas']['Message']
|
||||
message.edited = null;
|
||||
message.sent_at = new Date(Date.now()).toISOString();
|
||||
|
||||
const response = await client.POST(
|
||||
'/comment/{id}',
|
||||
{
|
||||
params: {
|
||||
path: { id: Number(event.params.ID) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
},
|
||||
body: message
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
||||
|
||||
export async function GET(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.GET(
|
||||
'/comment/{id}',
|
||||
{
|
||||
params: {
|
||||
path: { id: Number(event.params.ID) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
||||
|
||||
export async function DELETE(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.DELETE(
|
||||
'/comment/{id}',
|
||||
{
|
||||
params: {
|
||||
path: { id: Number(event.params.ID) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
||||
|
||||
export async function PATCH(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
let message = await event.request.json() as components['schemas']['Message']
|
||||
message.edited = new Date(Date.now()).toISOString();
|
||||
|
||||
const response = await client.PATCH(
|
||||
'/comment/{id}',
|
||||
{
|
||||
params: {
|
||||
path: { id: Number(event.params.ID) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
},
|
||||
body: message
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
import { error, redirect } from '@sveltejs/kit';
|
||||
import { client } from '$lib/api/client';
|
||||
import type { RequestEvent } from './$types';
|
||||
import type { components } from '$lib/api/api.gen';
|
||||
|
||||
export async function POST(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.POST(
|
||||
'/person',
|
||||
{
|
||||
params: {
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
},
|
||||
body: await event.request.json() as components['schemas']['PersonRegistration']
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
||||
|
65
apps/app/src/routes/api/person/[ID]/+server.ts
Normal file
65
apps/app/src/routes/api/person/[ID]/+server.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { client } from '$lib/api/client';
|
||||
import type { RequestEvent } from './$types';
|
||||
import type { components } from '$lib/api/api.gen';
|
||||
|
||||
export async function GET(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.GET(
|
||||
'/person/{id}',
|
||||
{
|
||||
params: {
|
||||
path: { id: Number(event.params.ID) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
||||
|
||||
export async function DELETE(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.DELETE(
|
||||
'/person/{id}',
|
||||
{
|
||||
params: {
|
||||
path: { id: Number(event.params.ID) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
||||
|
||||
export async function PATCH(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.PATCH(
|
||||
'/person/{id}',
|
||||
{
|
||||
params: {
|
||||
path: { id: Number(event.params.ID) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
},
|
||||
body: await event.request.json() as components['schemas']['PersonProperties']
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
23
apps/app/src/routes/api/person/[ID]/hard-delete/+server.ts
Normal file
23
apps/app/src/routes/api/person/[ID]/hard-delete/+server.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { error, redirect } from '@sveltejs/kit';
|
||||
import { client } from '$lib/api/client';
|
||||
import type { RequestEvent } from './$types';
|
||||
|
||||
export async function DELETE(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.DELETE(
|
||||
'/person/{id}/hard-delete',
|
||||
{
|
||||
params: {
|
||||
path: { id: Number(event.params.ID) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { client } from '$lib/api/client';
|
||||
import type { RequestEvent } from './$types';
|
||||
import type { components } from '$lib/api/api.gen';
|
||||
|
||||
export async function POST(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.POST(
|
||||
'/person_and_relationship/{id}',
|
||||
{
|
||||
params: {
|
||||
path: { id: Number(event.params.ID) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
},
|
||||
body: await event.request.json() as {
|
||||
person: components["schemas"]["PersonRegistration"];
|
||||
type?: "child" | "parent" | "spouse" | "sibling";
|
||||
relationship: components["schemas"]["FamilyRelationship"];
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
29
apps/app/src/routes/api/relationship/+server.ts
Normal file
29
apps/app/src/routes/api/relationship/+server.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { client } from '$lib/api/client';
|
||||
import type { RequestEvent } from './$types';
|
||||
import type { components } from '$lib/api/api.gen';
|
||||
|
||||
export async function POST(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.POST(
|
||||
'/relationship',
|
||||
{
|
||||
params: {
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
},
|
||||
body: event.request.json() as {
|
||||
id1?: number;
|
||||
id2?: number;
|
||||
type?: "child" | "parent" | "spouse" | "sibling";
|
||||
relationship?: components["schemas"]["FamilyRelationship"];
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
67
apps/app/src/routes/api/relationship/[ID1]/[ID2]/+server.ts
Normal file
67
apps/app/src/routes/api/relationship/[ID1]/[ID2]/+server.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { client } from '$lib/api/client';
|
||||
import type { RequestEvent } from './$types';
|
||||
import type { components } from '$lib/api/api.gen';
|
||||
|
||||
export async function GET(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.GET(
|
||||
'/relationship/{id1}/{id2}',
|
||||
{
|
||||
params: {
|
||||
path: { id1: Number(event.params.ID1), id2: Number(event.params.ID2) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
||||
|
||||
export async function PATCH(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.PATCH(
|
||||
'/relationship/{id1}/{id2}',
|
||||
{
|
||||
params: {
|
||||
path: { id1: Number(event.params.ID1), id2: Number(event.params.ID2) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
},
|
||||
body: {
|
||||
relationship: event.request.json() as components['schemas']['FamilyRelationship']
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
||||
|
||||
export async function DELETE(event: RequestEvent): Promise<Response> {
|
||||
if (event.locals.session === null) {
|
||||
return redirect(302, '/login');
|
||||
}
|
||||
|
||||
const response = await client.DELETE(
|
||||
'/relationship/{id1}/{id2}',
|
||||
{
|
||||
params: {
|
||||
path: { id1: Number(event.params.ID1), id2: Number(event.params.ID2) },
|
||||
header: { 'X-User-ID': event.locals.session.userId }
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(await response.response.json(), {
|
||||
status: response.response.status,
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user