Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Update Endpoints and Fix other endpoints for new schema #189

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
miguel-merlin marked this conversation as resolved.
Show resolved Hide resolved
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,
miguel-merlin marked this conversation as resolved.
Show resolved Hide resolved
message: 'Internal Error: Id is missing from parameter',
miguel-merlin marked this conversation as resolved.
Show resolved Hide resolved
};
}

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,
miguel-merlin marked this conversation as resolved.
Show resolved Hide resolved
'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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary comments

// {
// "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,
miguel-merlin marked this conversation as resolved.
Show resolved Hide resolved
'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;
JonMike8 marked this conversation as resolved.
Show resolved Hide resolved
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(),
JonMike8 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading