updated build-sites
This commit is contained in:
33
frontend/app/api/build-sites/add/route.ts
Normal file
33
frontend/app/api/build-sites/add/route.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
import { addBuildSitesSchema } from './schema';
|
||||
|
||||
const endpoint = "http://localhost:3001/graphql";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const body = await request.json();
|
||||
const validatedBody = addBuildSitesSchema.parse(body);
|
||||
console.dir({ validatedBody })
|
||||
try {
|
||||
const client = new GraphQLClient(endpoint);
|
||||
const query = gql`
|
||||
mutation CreateBuildSite($input: CreateBuildSitesInput!) {
|
||||
createBuildSite(input: $input) {
|
||||
_id
|
||||
siteName
|
||||
siteNo
|
||||
createdAt
|
||||
expiryStarts
|
||||
expiryEnds
|
||||
}
|
||||
}
|
||||
`;
|
||||
const variables = { input: validatedBody };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.createBuildSite, status: 200 });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
10
frontend/app/api/build-sites/add/schema.ts
Normal file
10
frontend/app/api/build-sites/add/schema.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const addBuildSitesSchema = z.object({
|
||||
siteName: z.string(),
|
||||
siteNo: z.string(),
|
||||
expiryStarts: z.string().optional(),
|
||||
expiryEnds: z.string().optional(),
|
||||
});
|
||||
|
||||
export type AddBuildSites = z.infer<typeof addBuildSitesSchema>;
|
||||
23
frontend/app/api/build-sites/delete/route.ts
Normal file
23
frontend/app/api/build-sites/delete/route.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
|
||||
const endpoint = "http://localhost:3001/graphql";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
|
||||
const searchParams = new URL(request.url).searchParams;
|
||||
const uuid = searchParams.get('uuid');
|
||||
if (!uuid) { return NextResponse.json({ error: 'UUID not found in search params' }, { status: 400 }) }
|
||||
try {
|
||||
const client = new GraphQLClient(endpoint);
|
||||
const query = gql`mutation DeleteBuildSite($uuid: String!) { deleteBuildSite(uuid: $uuid) }`;
|
||||
const variables = { uuid: uuid };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.deleteBuildSite, status: 200 });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
|
||||
}
|
||||
35
frontend/app/api/build-sites/list/route.ts
Normal file
35
frontend/app/api/build-sites/list/route.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
|
||||
const endpoint = "http://localhost:3001/graphql";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const body = await request.json();
|
||||
const { limit, skip, sort, filters } = body;
|
||||
try {
|
||||
const client = new GraphQLClient(endpoint);
|
||||
const query = gql`
|
||||
query BuildSites($input: ListArguments!) {
|
||||
buildSites(input: $input) {
|
||||
data {
|
||||
_id
|
||||
uuid
|
||||
siteNo
|
||||
siteName
|
||||
createdAt
|
||||
updatedAt
|
||||
expiryEnds
|
||||
expiryStarts
|
||||
}
|
||||
totalCount
|
||||
}
|
||||
}`;
|
||||
const variables = { input: { limit, skip, sort, filters } };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.buildSites.data, totalCount: data.buildSites.totalCount }, { status: 200 });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
31
frontend/app/api/build-sites/update/route.ts
Normal file
31
frontend/app/api/build-sites/update/route.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
import { UpdateBuildSitesSchema } from './schema';
|
||||
|
||||
const endpoint = "http://localhost:3001/graphql";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const searchUrl = new URL(request.url);
|
||||
const uuid = searchUrl.searchParams.get("uuid") || "";
|
||||
const body = await request.json();
|
||||
const validatedBody = UpdateBuildSitesSchema.parse(body);
|
||||
if (uuid === "") { return NextResponse.json({ error: "UUID is required" }, { status: 400 }) }
|
||||
try {
|
||||
const client = new GraphQLClient(endpoint);
|
||||
const query = gql`
|
||||
mutation UpdateBuildSite($uuid: String!, $input: UpdateBuildSitesInput!) {
|
||||
updateBuildSite(uuid: $uuid, input: $input) {
|
||||
uuid
|
||||
siteNo
|
||||
siteName
|
||||
}
|
||||
}`;
|
||||
const variables = { uuid: uuid, input: validatedBody };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.updateBuildSite, status: 200 });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
10
frontend/app/api/build-sites/update/schema.ts
Normal file
10
frontend/app/api/build-sites/update/schema.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const UpdateBuildSitesSchema = z.object({
|
||||
siteName: z.string().optional(),
|
||||
siteNo: z.string().optional(),
|
||||
expiryStarts: z.string().optional(),
|
||||
expiryEnds: z.string().optional(),
|
||||
})
|
||||
|
||||
export type UpdateBuildSites = z.infer<typeof UpdateBuildSitesSchema>;
|
||||
Reference in New Issue
Block a user