From 894b44877bacd1d0a6829446f14373c8ce4d79de Mon Sep 17 00:00:00 2001 From: Varun Raj Date: Sun, 8 Sep 2024 10:58:06 +0530 Subject: [PATCH] feat: added filters endpoint Signed-off-by: Varun Raj --- .../api/assets/missing-location-assets.ts | 3 +- .../api/assets/missing-location-dates.ts | 1 + src/pages/api/filters/asset-filters.ts | 64 +++++++++++++++++++ src/schema/exif.schema.ts | 4 +- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/pages/api/filters/asset-filters.ts diff --git a/src/pages/api/assets/missing-location-assets.ts b/src/pages/api/assets/missing-location-assets.ts index 2d6d16a..4847b53 100644 --- a/src/pages/api/assets/missing-location-assets.ts +++ b/src/pages/api/assets/missing-location-assets.ts @@ -4,7 +4,7 @@ import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; import { parseDate } from "@/helpers/date.helper"; import { assets, exif } from "@/schema"; import { addDays } from "date-fns"; -import { and, count, desc, eq, gte, isNotNull, isNull, lte, ne, sql } from "drizzle-orm"; +import { and, eq, gte, isNotNull, isNull, lte, ne, sql } from "drizzle-orm"; import type { NextApiRequest, NextApiResponse } from "next"; export default async function handler( @@ -51,7 +51,6 @@ export default async function handler( isNotNull(assets.createdAt), gte(exif.dateTimeOriginal, startDateDate), lte(exif.dateTimeOriginal, endDateDate), - eq(assets.type, "VIDEO"), eq(assets.ownerId, currentUser.id), eq(assets.isVisible, true), )); diff --git a/src/pages/api/assets/missing-location-dates.ts b/src/pages/api/assets/missing-location-dates.ts index 86af049..ca57e77 100644 --- a/src/pages/api/assets/missing-location-dates.ts +++ b/src/pages/api/assets/missing-location-dates.ts @@ -21,6 +21,7 @@ export default async function handler( .leftJoin(exif, eq(exif.assetId, assets.id)) .where(and( isNull(exif.latitude), + isNotNull(assets.createdAt), eq(assets.type, "VIDEO"), eq(assets.ownerId, currentUser.id), eq(assets.isVisible, true), diff --git a/src/pages/api/filters/asset-filters.ts b/src/pages/api/filters/asset-filters.ts new file mode 100644 index 0000000..a679944 --- /dev/null +++ b/src/pages/api/filters/asset-filters.ts @@ -0,0 +1,64 @@ +// Next.js API route support: https://nextjs.org/docs/api-routes/introduction +import { db } from "@/config/db"; +import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; +import { assets, exif } from "@/schema"; +import { IExifColumns } from "@/schema/exif.schema"; +import { IUser } from "@/types/user"; +import { and, eq } from "drizzle-orm"; +import type { NextApiRequest, NextApiResponse } from "next"; + +const FIELDS: IExifColumns[] = [ + "make", + "model", + "lensModel", + "city", + "state", + "country", + "projectionType", + "colorspace", + "bitsPerSample", + "rating", +]; + +const getFilters = async (currentUser: IUser) => { + const filters: { + [key in IExifColumns]?: (string | number | Date)[]; + } = {}; + for (const field of FIELDS) { + const values = (await db.selectDistinct({ + [field]: exif[field], + }) + .from(exif) + .leftJoin(assets, eq(exif.assetId, assets.id)) + .where( + and( + eq(assets.ownerId, currentUser.id), + ) + ) + ).map((value) => value[field]).filter((value) => value !== null); + + filters[field] = values; + } + return filters; +} + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + + try { + const currentUser = await getCurrentUser(req); + if (!currentUser) { + return res.status(401).json({ + error: "Unauthorized", + }); + } + const filters = await getFilters(currentUser); + return res.status(200).json({ filters }); + } catch (error: any) { + res.status(500).json({ + error: error?.message, + }); + } +} diff --git a/src/schema/exif.schema.ts b/src/schema/exif.schema.ts index 128ca2e..367573b 100644 --- a/src/schema/exif.schema.ts +++ b/src/schema/exif.schema.ts @@ -31,4 +31,6 @@ export const exif = pgTable('exif', { bitsPerSample: integer('bitsPerSample'), autoStackId: varchar('autoStackId'), rating: integer('rating'), -}); \ No newline at end of file +}); + +export type IExifColumns = keyof typeof exif.$inferSelect \ No newline at end of file