-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add endpoint to retrieve all flights (#110)
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8f5edba
There was a problem hiding this comment.
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