update to raise NOT_FOUND error when no resource found

This commit is contained in:
Brandon Egger
2023-08-29 10:30:18 -05:00
parent f180fb8fd1
commit 8895fc31b4
3 changed files with 33 additions and 15 deletions

View File

@ -1,10 +1,5 @@
import {
SkillLevel,
Skill,
Platform,
type AuditoryResource,
PaymentType,
} from "@prisma/client";
import { SkillLevel, Skill, Platform, PaymentType } from "@prisma/client";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import {
@ -25,13 +20,28 @@ export const auditoryResourceRouter = createTRPCRouter({
byId: publicProcedure
.input(z.object({ id: z.string() }))
.query(async ({ input, ctx }) => {
const resource = await ctx.prisma.auditoryResource.findUnique({
where: {
id: input.id,
},
});
try {
const resource = await ctx.prisma.auditoryResource.findUnique({
where: {
id: input.id,
},
});
return { ...resource } as AuditoryResource;
if (!resource) {
throw new TRPCError({
code: "NOT_FOUND",
message: "The resource you are looking for was not found.",
});
}
return resource;
} catch (e) {
throw new TRPCError({
code: "NOT_FOUND",
message: "The resource you are looking for was not found.",
cause: e,
});
}
}),
getAll: publicProcedure.query(({ ctx }) => {