Skip to content

Commit

Permalink
feat: refactor entity routes to use userId from request (#27)
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
Citronnelle committed Jan 14, 2025
1 parent 7452fa8 commit 20a7397
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 36 deletions.
4 changes: 3 additions & 1 deletion app/api/combat_logs/[id]/hard-delete/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import CombatLogService from "@/lib/services/combat-log-service"

// HARD DELETE a single combat log by ID
export const DELETE = createRouteHandler(async (id, userId) => {
export const DELETE = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const deletedCombatLog = await CombatLogService.hardDelete(id!, userId)
return deletedCombatLog
})
10 changes: 7 additions & 3 deletions app/api/combat_logs/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import CombatLogService from "@/lib/services/combat-log-service"

// GET a single combat log by ID
export const GET = createRouteHandler(async (id, userId) => {
export const GET = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const combatLog = await CombatLogService.get(id!, userId)
return combatLog
})

// UPDATE a single combat log by ID
export const PUT = createRouteHandler(async (id, userId, req) => {
export const PUT = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const updates = await req!.json()
const updatedCombatLog = await CombatLogService.update(id!, updates, userId)
return updatedCombatLog
})

// SOFT DELETE a single combat log by ID
export const DELETE = createRouteHandler(async (id, userId) => {
export const DELETE = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const deletedCombatLog = await CombatLogService.softDelete(id!, userId)
return deletedCombatLog
})
4 changes: 3 additions & 1 deletion app/api/combat_logs/by-encounter/[encounterId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import CombatLogService from "@/lib/services/combat-log-service"

export const GET = createRouteHandler(async (_, userId, req) => {
export const GET = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const encounterId = req?.url?.split("/").pop() // Extract encounterId from URL
if (!encounterId) throw new Error("Missing encounter ID")

Expand Down
4 changes: 3 additions & 1 deletion app/api/combat_logs/by-participant/[participantId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import CombatLogService from "@/lib/services/combat-log-service"

export const GET = createRouteHandler(async (_, userId, req) => {
export const GET = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const participantId = req?.url?.split("/").pop() // Extract participantId from URL
if (!participantId) throw new Error("Missing participant ID")

Expand Down
7 changes: 5 additions & 2 deletions app/api/combat_logs/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import CombatLogService from "@/lib/services/combat-log-service"

// GET all combat logs for a specific user
export const GET = createRouteHandler(async (_, userId) => {
export const GET = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const combatLogs = await CombatLogService.getByUserId(
"user_id",
userId,
Expand All @@ -12,7 +14,8 @@ export const GET = createRouteHandler(async (_, userId) => {
}, false) // `requiresId` is false since we're not dealing with a single record

// CREATE a new combat log
export const POST = createRouteHandler(async (_, userId, req) => {
export const POST = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const data = await req!.json()
const newCombatLog = await CombatLogService.create(data, userId)
return newCombatLog
Expand Down
4 changes: 3 additions & 1 deletion app/api/encounters/[id]/hard-delete/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import EncounterService from "@/lib/services/encounter-service"

// HARD DELETE a single encounter by ID
export const DELETE = createRouteHandler(async (id, userId) => {
export const DELETE = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const deletedEncounter = await EncounterService.hardDelete(id!, userId)
return deletedEncounter
})
10 changes: 7 additions & 3 deletions app/api/encounters/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import EncounterService from "@/lib/services/encounter-service"

// GET a single encounter by ID
export const GET = createRouteHandler(async (id, userId) => {
export const GET = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const encounter = await EncounterService.get(id!, userId)
return encounter
})

// UPDATE a single encounter by ID
export const PUT = createRouteHandler(async (id, userId, req) => {
export const PUT = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const updates = await req!.json()
const updatedEncounter = await EncounterService.update(id!, updates, userId)
return updatedEncounter
})

// SOFT DELETE a single encounter by ID
export const DELETE = createRouteHandler(async (id, userId) => {
export const DELETE = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const deletedEncounter = await EncounterService.softDelete(id!, userId)
return deletedEncounter
})
7 changes: 5 additions & 2 deletions app/api/encounters/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import EncounterService from "@/lib/services/encounter-service"

// GET all encounters for a specific user
export const GET = createRouteHandler(async (_, userId) => {
export const GET = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const encounters = await EncounterService.getByUserId(
"user_id",
userId,
Expand All @@ -12,7 +14,8 @@ export const GET = createRouteHandler(async (_, userId) => {
}, false) // `requiresId` is false since we're not dealing with a single record

// CREATE a new encounter
export const POST = createRouteHandler(async (_, userId, req) => {
export const POST = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const data = await req!.json()
const newEncounter = await EncounterService.create(data, userId)
return newEncounter
Expand Down
4 changes: 3 additions & 1 deletion app/api/participants/[id]/hard-delete/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import ParticipantService from "@/lib/services/participant-service"

// HARD DELETE a single participant by ID
export const DELETE = createRouteHandler(async (id, userId) => {
export const DELETE = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const deletedParticipant = await ParticipantService.hardDelete(id!, userId)
return deletedParticipant
})
10 changes: 7 additions & 3 deletions app/api/participants/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import ParticipantService from "@/lib/services/participant-service"

// GET a single participant by ID
export const GET = createRouteHandler(async (id, userId) => {
export const GET = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const participant = await ParticipantService.get(id!, userId)
return participant
})

// UPDATE a single participant by ID
export const PUT = createRouteHandler(async (id, userId, req) => {
export const PUT = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const updates = await req!.json()
const updatedParticipant = await ParticipantService.update(
id!,
Expand All @@ -19,7 +22,8 @@ export const PUT = createRouteHandler(async (id, userId, req) => {
})

// SOFT DELETE a single participant by ID
export const DELETE = createRouteHandler(async (id, userId) => {
export const DELETE = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const deletedParticipant = await ParticipantService.softDelete(id!, userId)
return deletedParticipant
})
4 changes: 3 additions & 1 deletion app/api/participants/by-encounter/[encounterId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import ParticipantService from "@/lib/services/participant-service"

export const GET = createRouteHandler(async (_, userId, req) => {
export const GET = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const encounterId = req?.url?.split("/").pop() // Extract encounterId from URL
if (!encounterId) throw new Error("Missing encounter ID")

Expand Down
4 changes: 3 additions & 1 deletion app/api/participants/by-stat-block/[statBlockId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import ParticipantService from "@/lib/services/participant-service"

export const GET = createRouteHandler(async (_, userId, req) => {
export const GET = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const statBlockId = req?.url?.split("/").pop() // Extract statBlockId from URL
if (!statBlockId) throw new Error("Missing stat block ID")

Expand Down
7 changes: 5 additions & 2 deletions app/api/participants/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import ParticipantService from "@/lib/services/participant-service"

// GET all participants for a specific user
export const GET = createRouteHandler(async (_, userId) => {
export const GET = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const participants = await ParticipantService.getByUserId(
"user_id",
userId,
Expand All @@ -12,7 +14,8 @@ export const GET = createRouteHandler(async (_, userId) => {
}, false) // `requiresId` is false since we're not dealing with a single record

// CREATE a new participant
export const POST = createRouteHandler(async (_, userId, req) => {
export const POST = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const data = await req!.json()
const newParticipant = await ParticipantService.create(data, userId)
return newParticipant
Expand Down
5 changes: 3 additions & 2 deletions app/api/profiles/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import { getProfile, updateProfile } from "@/lib/services/profile-service"

export const GET = createRouteHandler(async (id, userId) => {
export const GET = createRouteHandler(async (id, req) => {
const profile = await getProfile(id!) // `id` will be present since `requiresId` is true by default
return profile
})

export const PUT = createRouteHandler(async (id, userId, req) => {
export const PUT = createRouteHandler(async (id, req) => {
const updates = await req!.json()
await updateProfile(id!, updates)
return { success: true }
Expand Down
4 changes: 3 additions & 1 deletion app/api/stat_blocks/[id]/hard-delete/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import StatBlockService from "@/lib/services/stat-block-service"

// HARD DELETE a single stat block by ID
export const DELETE = createRouteHandler(async (id, userId) => {
export const DELETE = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const deletedStatBlock = await StatBlockService.hardDelete(id!, userId)
return deletedStatBlock
})
10 changes: 7 additions & 3 deletions app/api/stat_blocks/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import StatBlockService from "@/lib/services/stat-block-service"

// GET a single stat block by ID
export const GET = createRouteHandler(async (id, userId) => {
export const GET = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const statBlock = await StatBlockService.get(id!, userId)
return statBlock
})

// UPDATE a single stat block by ID
export const PUT = createRouteHandler(async (id, userId, req) => {
export const PUT = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const updates = await req!.json()
const updatedStatBlock = await StatBlockService.update(id!, updates, userId)
return updatedStatBlock
})

// SOFT DELETE a single stat block by ID
export const DELETE = createRouteHandler(async (id, userId) => {
export const DELETE = createRouteHandler(async (id, req) => {
const userId = await getCurrentUserId(req)
const deletedStatBlock = await StatBlockService.softDelete(id!, userId)
return deletedStatBlock
})
7 changes: 5 additions & 2 deletions app/api/stat_blocks/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createRouteHandler } from "@/utils/api/create-route-handler"
import { getCurrentUserId } from "@/utils/api/user-utils"
import StatBlockService from "@/lib/services/stat-block-service"

// GET all stat blocks for a specific user
export const GET = createRouteHandler(async (_, userId) => {
export const GET = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const statBlocks = await StatBlockService.getByUserId(
"user_id",
userId,
Expand All @@ -12,7 +14,8 @@ export const GET = createRouteHandler(async (_, userId) => {
}, false) // `requiresId` is false since we're not dealing with a single record

// CREATE a new stat block
export const POST = createRouteHandler(async (_, userId, req) => {
export const POST = createRouteHandler(async (_, req) => {
const userId = await getCurrentUserId(req)
const data = await req!.json()
const newStatBlock = await StatBlockService.create(data, userId)
return newStatBlock
Expand Down
8 changes: 2 additions & 6 deletions utils/api/create-route-handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { validateRequest } from "@/utils/api/validate-request"
import { NextResponse } from "next/server"

type HandlerFunction = (
id: string | null,
userId: string,
req?: Request,
) => Promise<any>
type HandlerFunction = (id: string | null, req: Request) => Promise<any>

export function createRouteHandler(
handler: HandlerFunction,
Expand Down Expand Up @@ -33,7 +29,7 @@ export function createRouteHandler(
}

try {
const result = await handler(id, userIdOrError, req)
const result = await handler(id, req)
return NextResponse.json(result)
} catch (error) {
return NextResponse.json(
Expand Down
9 changes: 9 additions & 0 deletions utils/api/user-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getUserIdFromRequest } from "@/middleware"

export async function getCurrentUserId(req: Request): Promise<string> {
const userId = await getUserIdFromRequest(req)
if (!userId) {
throw new Error("Unauthorized")
}
return userId
}

0 comments on commit 20a7397

Please sign in to comment.