Skip to content

Commit

Permalink
Simplify event type
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoishin committed Jan 12, 2024
1 parent 813911f commit 4bce046
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 104 deletions.
4 changes: 2 additions & 2 deletions projects/admin/src/data-provider/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/require-await */
import type { CreateParams, CreateResult } from "react-admin";
import { trpc } from "../trpc";
import { z } from "zod";
Expand All @@ -7,7 +6,8 @@ const createEventSchema = z.object({
name: z.string(),
startsAt: z.date().transform((date) => date.toISOString()),
endsAt: z.date().transform((date) => date.toISOString()),
marathonTypes: z.array(z.enum(["ONLINE", "ONSITE"])),
published: z.boolean(),
type: z.enum(["Onsite", "Online"]),
});

export const create = async (
Expand Down
12 changes: 5 additions & 7 deletions projects/admin/src/data-provider/get-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@ export const getList = async (
switch (resource) {
case "users": {
const res = await trpc.admin.users.list.query(input);
return { data: res.data, total: res.count };
return {
data: res.data,
total: res.count,
};
}
case "events": {
const res = await trpc.admin.events.list.query(input);
return {
data: res.data.map((item) => ({
...item,
marathonTypes: item.eventMarathonTypes.map((type) => ({
name: type.marathonType,
})),
})),
data: res.data,
total: res.count,
};
}
Expand Down
15 changes: 6 additions & 9 deletions projects/admin/src/data-provider/get-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ export const getOne = async (
const { id } = paramsSchema.parse(params);
switch (resource) {
case "users": {
const data = await trpc.admin.users.get.query({ id });
return { data };
const res = await trpc.admin.users.get.query({ id });
return {
data: res,
};
}
case "events": {
const data = await trpc.admin.events.get.query({ id });
const res = await trpc.admin.events.get.query({ id });
return {
data: {
...data,
marathonTypes: data.eventMarathonTypes.map(
(type) => type.marathonType,
),
},
data: res,
};
}
default:
Expand Down
17 changes: 12 additions & 5 deletions projects/admin/src/data-provider/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ import { z } from "zod";

const paramsSchema = z.object({
id: z.string().uuid(),
data: z.object({
email: z.string().email(),
}),
});

export const update = async (
resource: string,
params: UpdateParams,
): Promise<UpdateResult> => {
const { id, data } = paramsSchema.parse(params);
const { id } = paramsSchema.parse(params);
switch (resource) {
case "users": {
const result = await trpc.admin.users.update.mutate({ id, data });
const result = await trpc.admin.users.update.mutate({
id,
data: params.data,
});
return { data: result };
}
case "events": {
const result = await trpc.admin.events.update.mutate({
id,
data: params.data,
});
return { data: result };
}
default:
Expand Down
18 changes: 6 additions & 12 deletions projects/admin/src/resources/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import {
List,
TextField,
CheckboxGroupInput,
ArrayField,
SingleFieldList,
ChipField,
Edit,
BooleanInput,
RadioButtonGroupInput,
} from "react-admin";

export const EventCreate = () => {
Expand Down Expand Up @@ -42,11 +40,7 @@ export const EventList = () => (
<TextField source="name" />
<DateField showTime source="startsAt" />
<DateField showTime source="endsAt" />
<ArrayField source="marathonTypes">
<SingleFieldList linkType={false}>
<ChipField source="name" />
</SingleFieldList>
</ArrayField>
<TextField source="type" />
<BooleanField source="published" />
</Datagrid>
</List>
Expand All @@ -58,11 +52,11 @@ export const EventEdit = () => (
<TextInput source="name" validate={[required()]} />
<DateTimeInput source="startsAt" validate={[required()]} />
<DateTimeInput source="endsAt" validate={[required()]} />
<CheckboxGroupInput
source="marathonTypes"
<RadioButtonGroupInput
source="type"
choices={[
{ id: "ONSITE", name: "Onsite" },
{ id: "ONLINE", name: "Online" },
{ id: "Onsite", name: "Onsite" },
{ id: "Online", name: "Online" },
]}
/>
<BooleanInput source="published" />
Expand Down
22 changes: 22 additions & 0 deletions projects/server/prisma/migrations/20240112145533_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Warnings:
- You are about to drop the `EventMarathonType` table. If the table is not empty, all the data it contains will be lost.
- Added the required column `type` to the `Event` table without a default value. This is not possible if the table is not empty.
*/
-- CreateEnum
CREATE TYPE "EventType" AS ENUM ('Onsite', 'Online');

-- DropForeignKey
ALTER TABLE "EventMarathonType" DROP CONSTRAINT "EventMarathonType_eventId_fkey";

-- AlterTable
ALTER TABLE "Event" ADD COLUMN "type" "EventType" NOT NULL,
ALTER COLUMN "published" DROP DEFAULT;

-- DropTable
DROP TABLE "EventMarathonType";

-- DropEnum
DROP TYPE "MarathonType";
33 changes: 10 additions & 23 deletions projects/server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -67,38 +67,25 @@ model UserRole {
updateAt DateTime @updatedAt
}

enum EventType {
Onsite
Online
}

model Event {
id String @id @default(uuid()) @db.Uuid
name String @db.VarChar(255)
id String @id @default(uuid()) @db.Uuid
name String @db.VarChar(255)
startsAt DateTime
endsAt DateTime
published Boolean @default(false)
published Boolean
type EventType
gameSubmissions GameSubmission[]
eventMarathonTypes EventMarathonType[]
gameSubmissions GameSubmission[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

enum MarathonType {
ONLINE
ONSITE
}

model EventMarathonType {
id String @id @default(uuid()) @db.Uuid
eventId String @db.Uuid
marathonType MarathonType
event Event @relation(fields: [eventId], references: [id], onUpdate: Cascade, onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([eventId, marathonType])
}

model GameSubmission {
id String @id @default(uuid()) @db.Uuid
eventId String @db.Uuid
Expand Down
12 changes: 3 additions & 9 deletions projects/server/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MarathonType, PrismaClient, Role } from "@prisma/client";
import { EventType, PrismaClient, Role } from "@prisma/client";

const prisma = new PrismaClient();

Expand Down Expand Up @@ -26,13 +26,7 @@ await prisma.event.create({
name: "RTA in Japan Sample 1995",
startsAt: new Date("1995-08-08T12:00:00+0900"),
endsAt: new Date("1995-08-15T18:00:00+0900"),
eventMarathonTypes: {
createMany: {
data: [
{ marathonType: MarathonType.ONLINE },
{ marathonType: MarathonType.ONSITE },
],
},
},
published: false,
type: EventType.Onsite,
},
});
77 changes: 40 additions & 37 deletions projects/server/src/routes/admin/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,43 @@ import { z } from "zod";
import { prisma } from "../../prisma.js";
import { adminProcedure, router } from "../../trpc.js";
import { listSchema } from "./utils.js";
import { MarathonType } from "@prisma/client";
import { EventType } from "@prisma/client";
import { TRPCError } from "@trpc/server";

const createEventSchema = z.object({
name: z.string(),
startsAt: z.string().datetime(),
endsAt: z.string().datetime(),
marathonTypes: z.array(z.enum([MarathonType.ONLINE, MarathonType.ONSITE])),
published: z.boolean(),
type: z.enum([EventType.Onsite, EventType.Online]),
});

export const eventsRouter = router({
list: adminProcedure.input(listSchema).query(async ({ input }) => {
const data = await prisma.event.findMany({
skip: input.skip,
take: input.take,
orderBy: {
name: input.orderBy === "name" ? input.order : undefined,
startsAt: input.orderBy === "startsAt" ? input.order : undefined,
endsAt: input.orderBy === "endsAt" ? input.order : undefined,
published: input.orderBy === "published" ? input.order : undefined,
},
include: {
eventMarathonTypes: true,
},
});
const count = await prisma.event.count();
return { data, count };
const [events, count] = await Promise.all([
prisma.event.findMany({
skip: input.skip,
take: input.take,
orderBy: {
name: input.orderBy === "name" ? input.order : undefined,
startsAt: input.orderBy === "startsAt" ? input.order : undefined,
endsAt: input.orderBy === "endsAt" ? input.order : undefined,
published: input.orderBy === "published" ? input.order : undefined,
},
}),
prisma.event.count(),
]);
return {
data: events.map((event) => ({
id: event.id,
name: event.name,
startsAt: event.startsAt,
endsAt: event.endsAt,
published: event.published,
type: event.type,
})),
count,
};
}),
get: adminProcedure
.input(z.object({ id: z.string().uuid() }))
Expand All @@ -36,14 +47,18 @@ export const eventsRouter = router({
where: {
id: input.id,
},
include: {
eventMarathonTypes: true,
},
});
if (!event) {
throw new Error("event not found");
throw new TRPCError({ code: "NOT_FOUND" });
}
return event;
return {
id: event.id,
name: event.name,
startsAt: event.startsAt,
endsAt: event.endsAt,
published: event.published,
type: event.type,
};
}),
create: adminProcedure
.input(createEventSchema)
Expand All @@ -53,29 +68,17 @@ export const eventsRouter = router({
name: input.name,
startsAt: input.startsAt,
endsAt: input.endsAt,
published: input.published,
type: input.type,
},
});
await prisma.eventMarathonType.createMany({
data: input.marathonTypes.map((type) => ({
eventId: event.id,
marathonType: type,
})),
});
return event;
}),
update: adminProcedure
.input(
z.object({
id: z.string().uuid(),
data: z.object({
name: z.string(),
startsAt: z.string(),
endsAt: z.string(),
published: z.boolean(),
eventMarathonTypes: z.array(
z.enum([MarathonType.ONLINE, MarathonType.ONSITE]),
),
}),
data: createEventSchema.partial(),
}),
)
.mutation(async ({ input }) => {
Expand Down

0 comments on commit 4bce046

Please sign in to comment.