From 4bce0468ea2f8984c3d9e88810a401ce3e2b453c Mon Sep 17 00:00:00 2001 From: Keiichiro Amemiya Date: Fri, 12 Jan 2024 23:57:26 +0900 Subject: [PATCH] Simplify event type --- projects/admin/src/data-provider/create.ts | 4 +- projects/admin/src/data-provider/get-list.ts | 12 ++- projects/admin/src/data-provider/get-one.ts | 15 ++-- projects/admin/src/data-provider/update.ts | 17 ++-- projects/admin/src/resources/events.tsx | 18 ++--- .../migrations/20240112145533_/migration.sql | 22 ++++++ projects/server/prisma/schema.prisma | 33 +++----- projects/server/prisma/seed.ts | 12 +-- projects/server/src/routes/admin/events.ts | 77 ++++++++++--------- 9 files changed, 106 insertions(+), 104 deletions(-) create mode 100644 projects/server/prisma/migrations/20240112145533_/migration.sql diff --git a/projects/admin/src/data-provider/create.ts b/projects/admin/src/data-provider/create.ts index 9f8fe4f..63be40c 100644 --- a/projects/admin/src/data-provider/create.ts +++ b/projects/admin/src/data-provider/create.ts @@ -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"; @@ -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 ( diff --git a/projects/admin/src/data-provider/get-list.ts b/projects/admin/src/data-provider/get-list.ts index f750374..e43d7d7 100644 --- a/projects/admin/src/data-provider/get-list.ts +++ b/projects/admin/src/data-provider/get-list.ts @@ -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, }; } diff --git a/projects/admin/src/data-provider/get-one.ts b/projects/admin/src/data-provider/get-one.ts index 3d3adb8..949f6a9 100644 --- a/projects/admin/src/data-provider/get-one.ts +++ b/projects/admin/src/data-provider/get-one.ts @@ -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: diff --git a/projects/admin/src/data-provider/update.ts b/projects/admin/src/data-provider/update.ts index 5c13dd5..6271939 100644 --- a/projects/admin/src/data-provider/update.ts +++ b/projects/admin/src/data-provider/update.ts @@ -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 => { - 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: diff --git a/projects/admin/src/resources/events.tsx b/projects/admin/src/resources/events.tsx index 7420956..3cc3f72 100644 --- a/projects/admin/src/resources/events.tsx +++ b/projects/admin/src/resources/events.tsx @@ -10,11 +10,9 @@ import { List, TextField, CheckboxGroupInput, - ArrayField, - SingleFieldList, - ChipField, Edit, BooleanInput, + RadioButtonGroupInput, } from "react-admin"; export const EventCreate = () => { @@ -42,11 +40,7 @@ export const EventList = () => ( - - - - - + @@ -58,11 +52,11 @@ export const EventEdit = () => ( - diff --git a/projects/server/prisma/migrations/20240112145533_/migration.sql b/projects/server/prisma/migrations/20240112145533_/migration.sql new file mode 100644 index 0000000..0ad0ea8 --- /dev/null +++ b/projects/server/prisma/migrations/20240112145533_/migration.sql @@ -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"; diff --git a/projects/server/prisma/schema.prisma b/projects/server/prisma/schema.prisma index 35b9e25..a290996 100644 --- a/projects/server/prisma/schema.prisma +++ b/projects/server/prisma/schema.prisma @@ -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 diff --git a/projects/server/prisma/seed.ts b/projects/server/prisma/seed.ts index ee4fbc5..1bcb787 100644 --- a/projects/server/prisma/seed.ts +++ b/projects/server/prisma/seed.ts @@ -1,4 +1,4 @@ -import { MarathonType, PrismaClient, Role } from "@prisma/client"; +import { EventType, PrismaClient, Role } from "@prisma/client"; const prisma = new PrismaClient(); @@ -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, }, }); diff --git a/projects/server/src/routes/admin/events.ts b/projects/server/src/routes/admin/events.ts index 27a9e2f..81f3f13 100644 --- a/projects/server/src/routes/admin/events.ts +++ b/projects/server/src/routes/admin/events.ts @@ -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() })) @@ -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) @@ -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 }) => {