build-address added tested
This commit is contained in:
36
frontend/app/api/build-address/add/route.ts
Normal file
36
frontend/app/api/build-address/add/route.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
import { buildTypesAddSchema } from './schema';
|
||||
|
||||
const endpoint = "http://localhost:3001/graphql";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const body = await request.json();
|
||||
const validatedBody = buildTypesAddSchema.parse(body);
|
||||
try {
|
||||
const client = new GraphQLClient(endpoint);
|
||||
const query = gql`
|
||||
mutation CreateBuildAddress($input: CreateBuildAddressInput!) {
|
||||
createBuildAddress(input: $input) {
|
||||
_id
|
||||
buildNumber
|
||||
doorNumber
|
||||
floorNumber
|
||||
commentAddress
|
||||
letterAddress
|
||||
shortLetterAddress
|
||||
latitude
|
||||
longitude
|
||||
street
|
||||
}
|
||||
}
|
||||
`;
|
||||
const variables = { input: validatedBody };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.createBuildAddress, status: 200 });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
15
frontend/app/api/build-address/add/schema.ts
Normal file
15
frontend/app/api/build-address/add/schema.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const buildTypesAddSchema = z.object({
|
||||
buildNumber: z.string(),
|
||||
doorNumber: z.string(),
|
||||
floorNumber: z.string(),
|
||||
commentAddress: z.string(),
|
||||
letterAddress: z.string(),
|
||||
shortLetterAddress: z.string(),
|
||||
latitude: z.number(),
|
||||
longitude: z.number(),
|
||||
street: z.string().optional(),
|
||||
});
|
||||
|
||||
export type BuildTypesAdd = z.infer<typeof buildTypesAddSchema>;
|
||||
Reference in New Issue
Block a user