feat: added filters endpoint

Signed-off-by: Varun Raj <mailme@varunraj.in>
This commit is contained in:
Varun Raj
2024-09-08 10:58:06 +05:30
parent a596fa4943
commit 894b44877b
4 changed files with 69 additions and 3 deletions
@@ -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),
));
@@ -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),
+64
View File
@@ -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,
});
}
}
+3 -1
View File
@@ -31,4 +31,6 @@ export const exif = pgTable('exif', {
bitsPerSample: integer('bitsPerSample'),
autoStackId: varchar('autoStackId'),
rating: integer('rating'),
});
});
export type IExifColumns = keyof typeof exif.$inferSelect