Skip to content

Commit

Permalink
feat: add API queries for cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Sep 17, 2023
1 parent 4f88509 commit da66e39
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
1 change: 0 additions & 1 deletion packages/app/app/api/flights/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export async function GET(
}

const flight = await db.query.flights.findFirst({
columns: { cycle: false },
where: (flights, { eq }) => eq(flights.id, data.data.id),
})
if (flight === null) {
Expand Down
7 changes: 5 additions & 2 deletions packages/app/app/api/flights/all/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const schema = z.object({
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
cycle: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => new Date(value)))
.optional(),
})

export async function GET(request: Request) {
Expand Down Expand Up @@ -55,11 +59,10 @@ export async function GET(request: Request) {
}

const items = await db.query.flights.findMany({
columns: { cycle: false },
orderBy: (fields, { desc }) => desc(fields.id),
where: (fields, { sql, and, eq }) =>
and(
eq(fields.cycle, currentCycleSubquery),
data.data.cycle ? sql`${fields.cycle} IN ${data.data.cycle}` : eq(fields.cycle, currentCycleSubquery),
data.data.departureIcao &&
sql`${fields.departureIcao} IN ${data.data.departureIcao}`,
data.data.arrivalIcao &&
Expand Down
6 changes: 5 additions & 1 deletion packages/app/app/api/flights/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const schema = z.object({
(x) => (x ? Number(x) : undefined),
z.number().min(0).default(0)
),
cycle: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => new Date(value)))
.optional(),
})

export async function GET(request: Request) {
Expand Down Expand Up @@ -64,7 +68,7 @@ export async function GET(request: Request) {
}

const criteria = and(
eq(flights.cycle, currentCycleSubquery),
data.data.cycle ? sql`${flights.cycle} IN ${data.data.cycle}` : eq(flights.cycle, currentCycleSubquery),
data.data.departureIcao &&
sql`${flights.departureIcao} IN ${data.data.departureIcao}`,
data.data.arrivalIcao &&
Expand Down
28 changes: 28 additions & 0 deletions packages/app/app/api/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ const openApi = {
},
},
},
{
in: 'query',
name: 'cycle',
style: 'form',
explode: true,
schema: {
type: 'array',
items: {
type: 'string',
},
},
},
],
responses: {
'200': {
Expand Down Expand Up @@ -178,6 +190,18 @@ const openApi = {
},
},
},
{
in: 'query',
name: 'cycle',
style: 'form',
explode: true,
schema: {
type: 'array',
items: {
type: 'string',
},
},
},
],
responses: {
'200': {
Expand Down Expand Up @@ -319,6 +343,10 @@ const openApi = {
type: 'string',
enum: ['IFR', 'Y', 'Z'],
},
cycle: {
type: 'string',
format: 'date-time',
},
createdAt: {
type: 'string',
format: 'date-time',
Expand Down

0 comments on commit da66e39

Please sign in to comment.