Skip to content

Commit

Permalink
feat: add endpoint to retrieve all flights (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh authored Jan 20, 2023
1 parent 0d6eeb3 commit 8f5edba
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const nextConfig = {
config.plugins.push(new webpack.IgnorePlugin({ resourceRegExp: /^pg-native$/ }))
return config
},
api: {
responseLimit: false,
},
}

module.exports = nextConfig
41 changes: 41 additions & 0 deletions packages/app/pages/api/flights/all.ts
Original file line number Diff line number Diff line change
@@ -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" });
}
};
59 changes: 59 additions & 0 deletions packages/app/pages/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

1 comment on commit 8f5edba

@vercel
Copy link

@vercel vercel bot commented on 8f5edba Jan 20, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

mach – ./

mach-jpedroh.vercel.app
mach-five.vercel.app
mach-git-master-jpedroh.vercel.app
mach.jpedroh.dev

Please sign in to comment.