From 8f5edba618e153e48c9fededd6f41513fa643b93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Henrique?= Date: Thu, 19 Jan 2023 21:24:48 -0300 Subject: [PATCH] feat: add endpoint to retrieve all flights (#110) --- packages/app/next.config.js | 3 ++ packages/app/pages/api/flights/all.ts | 41 +++++++++++++++++++ packages/app/pages/api/index.ts | 59 +++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 packages/app/pages/api/flights/all.ts diff --git a/packages/app/next.config.js b/packages/app/next.config.js index 435e4e39..57dcd311 100644 --- a/packages/app/next.config.js +++ b/packages/app/next.config.js @@ -10,6 +10,9 @@ const nextConfig = { config.plugins.push(new webpack.IgnorePlugin({ resourceRegExp: /^pg-native$/ })) return config }, + api: { + responseLimit: false, + }, } module.exports = nextConfig \ No newline at end of file diff --git a/packages/app/pages/api/flights/all.ts b/packages/app/pages/api/flights/all.ts new file mode 100644 index 00000000..01edbab3 --- /dev/null +++ b/packages/app/pages/api/flights/all.ts @@ -0,0 +1,41 @@ +import { FlightModel } from "@mach/database"; +import type { NextApiRequest, NextApiResponse } from "next"; +import NextCors from "nextjs-cors"; +import z from "zod"; + +const schema = z.object({ + departureIcao: z + .preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string())) + .transform((values) => values.map((value) => value.toUpperCase())) + .optional(), + arrivalIcao: z + .preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string())) + .transform((values) => values.map((value) => value.toUpperCase())) + .optional(), + company: z + .preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string())) + .transform((values) => values.map((value) => value.toUpperCase())) + .optional(), +}); + +export default async (req: NextApiRequest, res: NextApiResponse) => { + try { + await NextCors(req, res, { methods: ["GET"], origin: "*" }); + + const data = schema.safeParse(req.query); + + if (!data.success) { + return res.status(400).json({ message: "Bad Request" }); + } + + const items = await FlightModel.findAll({ + where: { ...data.data }, + order: ["id"], + }); + + res.status(200).json(items); + } catch (error) { + console.error(error); + return res.status(500).json({ message: "Internal server error" }); + } +}; diff --git a/packages/app/pages/api/index.ts b/packages/app/pages/api/index.ts index 12a17776..25f41c2a 100644 --- a/packages/app/pages/api/index.ts +++ b/packages/app/pages/api/index.ts @@ -112,6 +112,65 @@ const openApi = { }, }, }, + "/flights/all": { + get: { + tags: ["Flight"], + summary: "Returns a list of Flights", + parameters: [ + { + in: "query", + name: "company", + style: "form", + explode: true, + schema: { + type: "array", + items: { + type: "string", + }, + }, + }, + { + in: "query", + name: "departureIcao", + style: "form", + explode: true, + schema: { + type: "array", + items: { + type: "string", + }, + }, + }, + { + in: "query", + name: "arrivalIcao", + style: "form", + explode: true, + schema: { + type: "array", + items: { + type: "string", + }, + }, + }, + ], + responses: { + "200": { + description: "A List of Flights", + content: { + "application/json": { + schema: { + type: "array", + items: { + $ref: "#/components/schemas/Flight", + }, + }, + }, + }, + }, + }, + }, + }, "/flights/{id}": { get: { tags: ["Flight"],