Skip to content

Commit

Permalink
Fix missing api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Allypost committed Aug 3, 2023
1 parent 930d65e commit f5713cb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/server/api/routers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import { z } from "zod";

import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { prisma } from "~/server/db";

const utcDate = (year: number, month: number, day: number) => {
const date = new Date(Date.UTC(year, month - 1, day));
date.setUTCHours(0, 0, 0, 0);
return date;
};
import { utcDate } from "~/utils/date";

export const eventsRouter = createTRPCRouter({
getUpcomingEvents: publicProcedure.query(async () => {
Expand Down Expand Up @@ -101,9 +96,8 @@ export const eventsRouter = createTRPCRouter({
.input(z.object({ year: z.number() }).optional())
.query(async ({ input }) => {
const forYear = input?.year ?? new Date().getFullYear();
const startOfYear = utcDate(forYear, 1, 1);
const startOfYearAfter = utcDate(forYear + 1, 1, 1);
await new Promise((resolve) => setTimeout(resolve, 1000));
const startOfYear = utcDate({ year: forYear, month: 1, day: 1 });
const startOfYearAfter = utcDate({ year: forYear + 1, month: 1, day: 1 });

const events = await prisma.events_event.findMany({
where: {
Expand Down Expand Up @@ -157,7 +151,7 @@ export const eventsRouter = createTRPCRouter({

getYearsWithEvents: publicProcedure.query(async () => {
const eventYears: { date: number }[] = await prisma.$queryRawUnsafe(
`select distinct date_part('year', date) as date from ${Prisma.ModelName.events_event} order by date_part('year', date) desc`,
`select distinct date_part('year', ${Prisma.Events_eventScalarFieldEnum.date}) as date from ${Prisma.ModelName.events_event} order by date_part('year', ${Prisma.Events_eventScalarFieldEnum.date}) desc`,
);

return eventYears.map((e) => e.date);
Expand Down
46 changes: 46 additions & 0 deletions src/server/api/routers/news.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Prisma } from "@prisma/client";
import { htmlToText } from "html-to-text";
import { z } from "zod";

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

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

Expand Down Expand Up @@ -41,6 +43,50 @@ export const newsRouter = createTRPCRouter({
});
}),

getNewsForYear: publicProcedure
.input(z.object({ year: z.number() }).optional())
.query(async ({ input }) => {
const forYear = input?.year ?? new Date().getFullYear();
const startOfYear = utcDate({ year: forYear, month: 1, day: 1 });
const startOfYearAfter = utcDate({ year: forYear + 1, month: 1, day: 1 });

const news = await prisma.news_news.findMany({
where: {
AND: [
{
// eslint-disable-next-line camelcase
created_at: {
gte: startOfYear,
},
},
{
// eslint-disable-next-line camelcase
created_at: {
lt: startOfYearAfter,
},
},
],
},
orderBy: {
// eslint-disable-next-line camelcase
created_at: "desc",
},
});

return {
forYear,
items: news,
};
}),

getYearsWithNews: publicProcedure.query(async () => {
const eventYears: { date: number }[] = await prisma.$queryRawUnsafe(
`select distinct date_part('year', ${Prisma.News_newsScalarFieldEnum.created_at}) as date from ${Prisma.ModelName.news_news} order by date_part('year', ${Prisma.News_newsScalarFieldEnum.created_at}) desc`,
);

return eventYears.map((e) => e.date);
}),

getNewsItem: publicProcedure
.input(
z.object({
Expand Down
15 changes: 15 additions & 0 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const utcDate = (
fromDate: Date | { year: number; month: number; day: number },
) => {
const info =
fromDate instanceof Date
? {
year: fromDate.getUTCFullYear(),
month: fromDate.getUTCMonth() + 1,
day: fromDate.getUTCDate(),
}
: fromDate;
const date = new Date(Date.UTC(info.year, info.month - 1, info.day));
date.setUTCHours(0, 0, 0, 0);
return date;
};

0 comments on commit f5713cb

Please sign in to comment.