159 lines
3.7 KiB
TypeScript
159 lines
3.7 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import { lobbies, lobbyMembers } from "@/server/db/schema";
|
|
import {
|
|
createTRPCRouter,
|
|
protectedProcedure,
|
|
publicProcedure,
|
|
} from "@/server/api/trpc";
|
|
import { lobbyPatchSchema } from "@/lib/validations/lobby";
|
|
import { and, eq } from "drizzle-orm";
|
|
import { time } from "console";
|
|
|
|
export const lobbyRouter = createTRPCRouter({
|
|
// queries
|
|
getAll: protectedProcedure.query(async ({ ctx }) => {
|
|
const ownedLobbies = await ctx.db.query.lobbies.findMany({
|
|
where: eq(lobbies.createdById, ctx.session.user.id),
|
|
});
|
|
const joinedLobbies = await ctx.db.query.lobbyMembers.findMany({
|
|
where: eq(lobbyMembers.userId, ctx.session.user.id),
|
|
with: {
|
|
lobby: true,
|
|
},
|
|
});
|
|
|
|
return [
|
|
...ownedLobbies,
|
|
...(joinedLobbies?.map(({ lobby }) => lobby) ?? []),
|
|
];
|
|
}),
|
|
|
|
get: publicProcedure
|
|
.input(
|
|
z.object({
|
|
id: z.string(),
|
|
}),
|
|
)
|
|
.query(
|
|
async ({ ctx, input }) =>
|
|
await ctx.db.query.lobbies.findFirst({
|
|
where: eq(lobbies.id, input.id),
|
|
with: {
|
|
leader: {
|
|
columns: {
|
|
image: true,
|
|
name: true,
|
|
id: true,
|
|
joinedAt: true,
|
|
},
|
|
},
|
|
members: {
|
|
with: {
|
|
user: {
|
|
columns: {
|
|
image: true,
|
|
name: true,
|
|
id: true,
|
|
joinedAt: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
),
|
|
|
|
// mutations
|
|
create: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
lobby: lobbyPatchSchema,
|
|
}),
|
|
)
|
|
.mutation(async ({ ctx, input }) => {
|
|
const [lobby] = await ctx.db
|
|
.insert(lobbies)
|
|
.values({
|
|
...input.lobby,
|
|
createdById: ctx.session.user.id,
|
|
})
|
|
.returning({ id: lobbies.id });
|
|
return lobby;
|
|
}),
|
|
update: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
lobby: lobbyPatchSchema,
|
|
lobbyId: z.string(),
|
|
}),
|
|
)
|
|
.mutation(async ({ ctx, input }) => {
|
|
const [lobby] = await ctx.db
|
|
.update(lobbies)
|
|
.set(input.lobby)
|
|
.where(
|
|
and(
|
|
eq(lobbies.id, input.lobbyId),
|
|
eq(lobbies.createdById, ctx.session.user.id),
|
|
),
|
|
)
|
|
.returning({ id: lobbies.id });
|
|
return lobby;
|
|
}),
|
|
delete: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
lobbyId: z.string(),
|
|
}),
|
|
)
|
|
.mutation(async ({ ctx, input }) => {
|
|
const [lobby] = await ctx.db
|
|
.delete(lobbies)
|
|
.where(
|
|
and(
|
|
eq(lobbies.id, input.lobbyId),
|
|
eq(lobbies.createdById, ctx.session.user.id),
|
|
),
|
|
)
|
|
.returning({ id: lobbies.id });
|
|
return lobby;
|
|
}),
|
|
|
|
membership: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
lobbyId: z.string(),
|
|
join: z.boolean(),
|
|
}),
|
|
)
|
|
.mutation(async ({ ctx, input }) => {
|
|
if (input.join) {
|
|
return (
|
|
await ctx.db
|
|
.insert(lobbyMembers)
|
|
.values({
|
|
lobbyId: input.lobbyId,
|
|
userId: ctx.session.user.id,
|
|
isReady: false,
|
|
joinedAt: new Date(),
|
|
role: "member",
|
|
})
|
|
.returning()
|
|
)[0];
|
|
} else {
|
|
return (
|
|
await ctx.db
|
|
.delete(lobbyMembers)
|
|
.where(
|
|
and(
|
|
eq(lobbyMembers.lobbyId, input.lobbyId),
|
|
eq(lobbyMembers.userId, ctx.session.user.id),
|
|
),
|
|
)
|
|
.returning()
|
|
)[0];
|
|
}
|
|
}),
|
|
});
|