From 96a8eaa33a83dd315fd3df3f218f153b2d53f832 Mon Sep 17 00:00:00 2001 From: Aaron Delasy Date: Mon, 21 Oct 2024 13:29:21 +0300 Subject: [PATCH] Move attempts to separate file --- app/play/[level]/[attempt]/page.tsx | 2 +- convex/_generated/api.d.ts | 2 ++ convex/attempts.ts | 47 +++++++++++++++++++++++++++++ convex/playerresults.ts | 44 --------------------------- 4 files changed, 50 insertions(+), 45 deletions(-) create mode 100644 convex/attempts.ts diff --git a/app/play/[level]/[attempt]/page.tsx b/app/play/[level]/[attempt]/page.tsx index 04d4333..cd1faee 100644 --- a/app/play/[level]/[attempt]/page.tsx +++ b/app/play/[level]/[attempt]/page.tsx @@ -16,7 +16,7 @@ export default function PlayLevelAttemptPage({ const level = Number.parseInt(params.level, 10); const attemptNum = Number.parseInt(params.attempt, 10); - const attempt = useQuery(api.playerresults.getUserAttempt, { + const attempt = useQuery(api.attempts.getUserAttempt, { level, attempt: attemptNum, }); diff --git a/convex/_generated/api.d.ts b/convex/_generated/api.d.ts index 96013c0..7ed685a 100644 --- a/convex/_generated/api.d.ts +++ b/convex/_generated/api.d.ts @@ -15,6 +15,7 @@ import type { FilterApi, FunctionReference, } from "convex/server"; +import type * as attempts from "../attempts.js"; import type * as auth from "../auth.js"; import type * as constants from "../constants.js"; import type * as crons from "../crons.js"; @@ -40,6 +41,7 @@ import type * as users from "../users.js"; * ``` */ declare const fullApi: ApiFromModules<{ + attempts: typeof attempts; auth: typeof auth; constants: typeof constants; crons: typeof crons; diff --git a/convex/attempts.ts b/convex/attempts.ts new file mode 100644 index 0000000..aa69667 --- /dev/null +++ b/convex/attempts.ts @@ -0,0 +1,47 @@ +import { getAuthUserId } from "@convex-dev/auth/server"; +import { v } from "convex/values"; +import { api } from "./_generated/api"; +import { Id } from "./_generated/dataModel"; +import { query } from "./_generated/server"; + +export const getUserAttempt = query({ + args: { + attempt: v.number(), + level: v.number(), + }, + handler: async (ctx, args) => { + const userId = await getAuthUserId(ctx); + + if (!userId) { + return null; + } + + const map = await ctx.runQuery(api.maps.getMapByLevel, { + level: args.level, + }); + + if (map === null) { + return null; + } + + const userResults = await ctx.db + .query("userResults") + .withIndex("by_mapId_userId", (q) => + q.eq("mapId", map._id).eq("userId", userId), + ) + .collect(); + + if (userResults.length !== 1) { + return null; + } + + const userResult = userResults[0]; + + if (userResult.attempts.length < args.attempt) { + return null; + } + + const attemptId: Id<"attempts"> = userResult.attempts[args.attempt - 1]; + return await ctx.db.get(attemptId); + }, +}); diff --git a/convex/playerresults.ts b/convex/playerresults.ts index 21056aa..639a4a9 100644 --- a/convex/playerresults.ts +++ b/convex/playerresults.ts @@ -1,51 +1,7 @@ import { getAuthUserId } from "@convex-dev/auth/server"; import { v } from "convex/values"; -import { api } from "./_generated/api"; -import { Id } from "./_generated/dataModel"; import { mutation, query } from "./_generated/server"; -export const getUserAttempt = query({ - args: { - attempt: v.number(), - level: v.number(), - }, - handler: async (ctx, args) => { - const userId = await getAuthUserId(ctx); - - if (!userId) { - return null; - } - - const map = await ctx.runQuery(api.maps.getMapByLevel, { - level: args.level, - }); - - if (map === null) { - return null; - } - - const userResults = await ctx.db - .query("userResults") - .withIndex("by_mapId_userId", (q) => - q.eq("mapId", map._id).eq("userId", userId), - ) - .collect(); - - if (userResults.length !== 1) { - return null; - } - - const userResult = userResults[0]; - - if (userResult.attempts.length < args.attempt) { - return null; - } - - const attemptId: Id<"attempts"> = userResult.attempts[args.attempt - 1]; - return await ctx.db.get(attemptId); - }, -}); - export const getUserMapStatus = query({ handler: async (ctx) => { const userId = await getAuthUserId(ctx);