Skip to content

Commit

Permalink
♻️ using modulekey on the page query
Browse files Browse the repository at this point in the history
  • Loading branch information
PsicoThePato committed Oct 17, 2024
1 parent df0a383 commit 50e9d2b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ export default async function ModulePage({ params }: Params) {
notFound();
}

const id = slug[0];

if (!/^\d+$/.test(String(id))) {
const moduleKey = slug[0];
if (!moduleKey) {
notFound();
}

const mdl = await api.module.byId({ id: Number(id) });
const mdl = await api.module.ByKeyLastBlock({ moduleKey: moduleKey });

if (!mdl) {
notFound();
Expand Down
2 changes: 1 addition & 1 deletion apps/commune-validator/src/app/components/module-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function ModuleCard(props: ModuleCardProps) {
/>
<Link
className="flex w-full items-center justify-between border border-white/20 bg-[#898989]/5 p-2 pl-3 text-white backdrop-blur-md transition duration-200 hover:border-green-500 hover:bg-green-500/10"
href={`module/${props.id}`}
href={`module/${props.moduleKey}`}
>
View More <ArrowRightIcon className="h-5 w-5 text-green-500" />
</Link>
Expand Down
20 changes: 19 additions & 1 deletion packages/api/src/router/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TRPCRouterRecord } from "@trpc/server";
import { z } from "zod";

import { eq, sql } from "@commune-ts/db";
import { eq, sql, and } from "@commune-ts/db";
import {
computedModuleWeightsSchema,
moduleData,
Expand Down Expand Up @@ -29,6 +29,24 @@ export const moduleRouter = {
where: eq(moduleData.id, input.id),
});
}),
ByKeyLastBlock: publicProcedure
.input(z.object({ moduleKey: z.string() }))
.query(async ({ ctx, input }) => {
const queryResult = await ctx.db
.select()
.from(moduleData)
.where(
and(
sql`${moduleData.atBlock} = (SELECT MAX(${moduleData.atBlock}) FROM ${moduleData})`,
eq(moduleData.moduleKey, input.moduleKey)
)

)
.limit(1)
.then((result) => result[0]);

return queryResult;
}),
byUserModuleData: publicProcedure
.input(z.object({ userKey: z.string() }))
.query(async ({ ctx, input }) => {
Expand Down

0 comments on commit 50e9d2b

Please sign in to comment.