new rest endpoints

This commit is contained in:
Atridad Lahiji 2023-08-08 12:59:02 -06:00 committed by atridadl
parent a4956e9926
commit 5097e55423
No known key found for this signature in database
6 changed files with 61 additions and 47 deletions

6
pnpm-lock.yaml generated
View file

@ -1653,7 +1653,7 @@ packages:
hasBin: true
dependencies:
caniuse-lite: 1.0.30001519
electron-to-chromium: 1.4.487
electron-to-chromium: 1.4.488
node-releases: 2.0.13
update-browserslist-db: 1.0.11(browserslist@4.21.10)
dev: false
@ -2186,8 +2186,8 @@ packages:
semver: 7.5.4
dev: false
/electron-to-chromium@1.4.487:
resolution: {integrity: sha512-XbCRs/34l31np/p33m+5tdBrdXu9jJkZxSbNxj5I0H1KtV2ZMSB+i/HYqDiRzHaFx2T5EdytjoBRe8QRJE2vQg==}
/electron-to-chromium@1.4.488:
resolution: {integrity: sha512-Dv4sTjiW7t/UWGL+H8ZkgIjtUAVZDgb/PwGWvMsCT7jipzUV/u5skbLXPFKb6iV0tiddVi/bcS2/kUrczeWgIQ==}
dev: false
/emoji-regex@9.2.2:

View file

@ -61,7 +61,7 @@ const AdminBody: React.FC = () => {
isLoading: usersCountLoading,
isFetching: usersCountFetching,
refetch: refetchUsersCount,
} = api.user.countAll.useQuery();
} = api.rest.userCount.useQuery();
const {
data: users,
isLoading: usersLoading,
@ -73,13 +73,13 @@ const AdminBody: React.FC = () => {
isLoading: roomsCountLoading,
isFetching: roomsCountFetching,
refetch: refetchRoomsCount,
} = api.room.countAll.useQuery();
} = api.rest.roomCount.useQuery();
const {
data: votesCount,
isLoading: votesCountLoading,
isFetching: votesCountFetching,
refetch: refetchVotesCount,
} = api.vote.countAll.useQuery();
} = api.rest.voteCount.useQuery();
const getProviders = (user: {
createdAt: Date;

View file

@ -3,6 +3,7 @@ import { z } from "zod";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { TRPCError } from "@trpc/server";
import { fetchCache, setCache } from "~/server/redis";
export const restRouter = createTRPCRouter({
dbWarmer: publicProcedure
@ -18,4 +19,58 @@ export const restRouter = createTRPCRouter({
throw new TRPCError({ code: "UNAUTHORIZED" });
}
}),
voteCount: publicProcedure
.meta({ openapi: { method: "GET", path: "/rest/votes/count" } })
.input(z.void())
.output(z.number())
.query(async ({ ctx }) => {
const cachedResult = await fetchCache<number>(`kv_votecount_admin`);
if (cachedResult) {
return cachedResult;
} else {
const votesCount = await ctx.prisma.vote.count();
await setCache(`kv_votecount_admin`, votesCount);
return votesCount;
}
}),
userCount: publicProcedure
.meta({ openapi: { method: "GET", path: "/rest/users/count" } })
.input(z.void())
.output(z.number())
.query(async ({ ctx }) => {
const cachedResult = await fetchCache<number>(`kv_usercount`);
if (cachedResult) {
return cachedResult;
} else {
const usersCount = await ctx.prisma.user.count();
await setCache(`kv_usercount`, usersCount);
return usersCount;
}
}),
roomCount: publicProcedure
.meta({ openapi: { method: "GET", path: "/rest/rooms/count" } })
.input(z.void())
.output(z.number())
.query(async ({ ctx }) => {
const cachedResult = await fetchCache<number>(`kv_roomcount`);
if (cachedResult) {
return cachedResult;
} else {
const roomsCount = await ctx.prisma.room.count();
await setCache(`kv_roomcount`, roomsCount);
return roomsCount;
}
}),
});

View file

@ -97,20 +97,6 @@ export const roomRouter = createTRPCRouter({
}
}),
countAll: adminProcedure.query(async ({ ctx }) => {
const cachedResult = await fetchCache<number>(`kv_roomcount_admin`);
if (cachedResult) {
return cachedResult;
} else {
const roomsCount = await ctx.prisma.room.count();
await setCache(`kv_roomcount_admin`, roomsCount);
return roomsCount;
}
}),
// Update One
set: protectedProcedure
.input(

View file

@ -14,20 +14,6 @@ import { fetchCache, invalidateCache, setCache } from "~/server/redis";
const resend = new Resend(process.env.RESEND_API_KEY);
export const userRouter = createTRPCRouter({
countAll: adminProcedure.query(async ({ ctx }) => {
const cachedResult = await fetchCache<number>(`kv_usercount_admin`);
if (cachedResult) {
return cachedResult;
} else {
const usersCount = await ctx.prisma.user.count();
await setCache(`kv_usercount_admin`, usersCount);
return usersCount;
}
}),
getProviders: protectedProcedure.query(async ({ ctx }) => {
const providers = await ctx.prisma.user.findUnique({
where: {

View file

@ -11,19 +11,6 @@ import { fetchCache, invalidateCache, setCache } from "~/server/redis";
import { EventTypes } from "~/utils/types";
export const voteRouter = createTRPCRouter({
countAll: adminProcedure.query(async ({ ctx }) => {
const cachedResult = await fetchCache<number>(`kv_votecount_admin`);
if (cachedResult) {
return cachedResult;
} else {
const votesCount = await ctx.prisma.vote.count();
await setCache(`kv_votecount_admin`, votesCount);
return votesCount;
}
}),
getAllByRoomId: protectedProcedure
.input(z.object({ roomId: z.string() }))
.query(async ({ ctx, input }) => {