Skip to content

Commit

Permalink
Add Update Endpoints and Fix other endpoints for new schema (#189)
Browse files Browse the repository at this point in the history
* Modified update endpoint to change the playground_frequency

Also updated the zod typechecking and DELETE endpoint to work with new schema

* feat: Add endpoint to update multiple antenna playground_frequencies given a list of ids

* Added new labeledFrequencies type and changed request body sctucture accordingly

* Fixed merge error
  • Loading branch information
JonMike8 authored Apr 19, 2024
1 parent 826d3b3 commit 69e00a3
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 11 deletions.
58 changes: 56 additions & 2 deletions app/api/v1/antenna/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { isAntenna } from '@/app/api/v1/antenna/validate';
import { pool } from '@/app/api/v1/connection';
import StatusError from '@/app/api/(utils)/StatusError';

export async function DELETE(_: Request, context: { params: { id: string } }) {
export async function DELETE(_: Request, context: { params: { id: number } }) {
try {
if (context.params.id.trim().length == 0)
if (!context.params.id)
throw new StatusError(
500,
'Internal Error: Id is missing from parameter'
Expand Down Expand Up @@ -42,3 +42,57 @@ export async function DELETE(_: Request, context: { params: { id: string } }) {
return NextResponse.json({ message }, { status });
}
}
export async function PUT(
request: Request,
context: { params: { id: number } }
) {
try {
if (!context.params.id) {
throw {
status: 500,
message: 'Internal Error: Id is missing from parameter',
};
}

const id = context.params.id;

const { frequency } = (await request.json()) as { frequency: number };

const client = await pool.connect();

const query = `
UPDATE Antennas
SET playground_frequency = $1
WHERE id = $2
RETURNING *;
`;
const result = await client.query(query, [frequency, id]);

if (result.rowCount === 0) {
throw new StatusError(404, `Antenna with ${id} does not exist.`);
}
if (!isAntenna(result.rows[0])) {
throw new StatusError(
500,
'Antenna must be updated with correct data types / values.'
);
}

const updatedAntenna = result.rows[0];

client.release();

return NextResponse.json(updatedAntenna, { status: 200 });
} catch (error) {
let message = 'Internal Server Error';
let status = 500;
if (typeof error === 'object') {
if (error && 'message' in error && typeof error.message === 'string')
message = error.message;
if (error && 'status' in error && typeof error.status === 'number')
status = error.status;
}

return NextResponse.json({ message }, { status });
}
}
53 changes: 53 additions & 0 deletions app/api/v1/antenna/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { NextResponse } from 'next/server';
import { pool } from '../connection';
import { isAntenna, Antenna } from '@/app/api/v1/antenna/validate';
import StatusError from '@/app/api/(utils)/StatusError';
import { labeledFrequencies } from '@/app/types';

export async function GET() {
try {
Expand All @@ -14,3 +17,53 @@ export async function GET() {
return NextResponse.json({ message }, { status: 500 });
}
}

// request body should be:
// {
// "dataArray": [
// {labeledFrequencies}
// ]
// }
export async function PUT(request: Request) {
const updatedAntennas: Antenna[] = [];
try {
const { dataArray }: { dataArray: labeledFrequencies[] } =
(await request.json()) as { dataArray: labeledFrequencies[] };
const client = await pool.connect();

for (const { id, frequency } of dataArray) {
const query = `
UPDATE Antennas
SET playground_frequency = $1
WHERE id = $2
RETURNING *
`;
const result = await client.query(query, [frequency, id]);
if (result.rowCount === 0) {
throw new StatusError(404, `Antenna with ${id} does not exist.`);
}
if (!isAntenna(result.rows[0])) {
throw new StatusError(
500,
'Antenna must be updated with correct data types/values'
);
}
const updatedAntenna = result.rows[0];
updatedAntennas.push(updatedAntenna);
}
client.release();

return NextResponse.json(updatedAntennas, { status: 200 });
} catch (error) {
let message = 'Internal Server Error';
let status = 500;
if (typeof error === 'object') {
if (error && 'message' in error && typeof error.message === 'string')
message = error.message;
if (error && 'status' in error && typeof error.status === 'number')
status = error.status;
}

return NextResponse.json({ message }, { status });
}
}
17 changes: 9 additions & 8 deletions app/api/v1/antenna/validate.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { z } from 'zod';

export type Antenna = {
id: string;
id: number;
name: string;
hostname: string;
model: string;
modelname: string;
frequency: number;
playground_frequency: number;
latitude: string;
longitude: number;
longitude: string;
azimuth: number;
typeAntenna: number;
typeantenna: number;
antenna_status: string;
cpu: number;
ram: string;
ram: number;
};

export function isAntenna(antenna: unknown): antenna is Antenna {
const antennaSchema = z.object({
id: z.string(),
id: z.number(),
name: z.string(),
hostname: z.string(),
model: z.string(),
Expand All @@ -29,9 +29,10 @@ export function isAntenna(antenna: unknown): antenna is Antenna {
latitude: z.string(),
longitude: z.string(),
azimuth: z.number().int().gte(0).lt(360),
typeAntenna: z.number().gte(0).lte(2),
cpu: z.string(),
ram: z.string(),
typeantenna: z.number().gte(0).lte(2),
antenna_status: z.string(),
cpu: z.number(),
ram: z.number(),
});

const res = antennaSchema.safeParse(antenna);
Expand Down
5 changes: 4 additions & 1 deletion app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ export interface ReducedPoints {
[key: string]: ReducedContent;
}

export interface labeledFrequencies {
id: number;
frequency: number;
}
export interface SectorlobeData {
id: string;
center: LatLngTuple;
sectorVertices: LatLngExpression[];
frequency: number;
}

// Props

export interface InfoProps {
Expand Down

0 comments on commit 69e00a3

Please sign in to comment.