Skip to content

Commit

Permalink
Fix missing api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Allypost committed Sep 19, 2023
1 parent 8d9fde6 commit 4aa0874
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions src/server/api/routers/gallery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { type Prisma } from "@prisma/client";
import { z } from "zod";

import { prisma } from "~/server/db";
import { utcDate } from "~/utils/date";

import { createTRPCRouter, publicProcedure } from "../trpc";

Expand Down Expand Up @@ -50,4 +52,113 @@ export const galleryRouter = createTRPCRouter({

return gallery;
}),

getGalleryOverview: publicProcedure
.input(
z.object({
eventType: z.enum(["live", "foto"]),
forYear: z
.number()
.int()
.min(2000)
.max(2100)
.default(new Date().getFullYear()),
filter: z
.object({
titleOrPhotographerName: z.string().transform((v) => v.trim()),
})
.partial()
.optional(),
}),
)
.query(async ({ input: { forYear, eventType, filter } }) => {
const startOfYear = utcDate({
year: forYear,
month: 1,
day: 1,
});
const startOfYearAfter = utcDate({
year: forYear + 1,
month: 1,
day: 1,
});

const galleriesFilter = (
[
filter?.titleOrPhotographerName
? {
OR: [
{
title: {
contains: filter.titleOrPhotographerName,
mode: "insensitive",
},
},
{
photographer: {
name: {
contains: filter.titleOrPhotographerName,
mode: "insensitive",
},
},
},
],
}
: undefined,
{
category: eventType,
},
{
dateOfEvent: {
gte: startOfYear,
},
},
{
dateOfEvent: {
lt: startOfYearAfter,
},
},
] satisfies (Prisma.GalleryAlbumWhereInput | undefined)[]
).filter(Boolean);

const galleries = await prisma.galleryAlbum.findMany({
select: {
id: true,
title: true,
slug: true,
thumb: true,
dateOfEvent: true,
photographer: {
select: {
name: true,
},
},
},
where: {
AND: galleriesFilter,
},
});

const entries = galleries.reduce<Record<number, typeof galleries>>(
(acc, gallery) => {
const month = gallery.dateOfEvent.getMonth() + 1;

if (!acc[month]) {
acc[month] = [];
}

acc[month]!.push(gallery);

return acc;
},
{},
);

return {
forYear,
eventType,
filter,
groupedByMonth: entries,
};
}),
});

0 comments on commit 4aa0874

Please sign in to comment.