-
Notifications
You must be signed in to change notification settings - Fork 0
HMTV2-21: generate food timeslot #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jasutiin
wants to merge
14
commits into
main
Choose a base branch
from
HMTV2-21-generate-food-timeslot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
19d232a
feat: add meal and mealAttendance tables
jasutiin 71c49b8
feat: create trpc router for adding meals
jasutiin 57ba707
feat: add meal page to test addMeal trpc mutation
jasutiin 4063b50
fix: unique meal title & startTime, check endTime > startTime
jasutiin 6419d1e
feat: scanUserIn feature
jasutiin c208b6e
fix ci
jasutiin af08f5e
make title not unique, remove checkedInBy
jasutiin 4ed1cd0
fix: remove checkedInBy in meals router
jasutiin 133fc8e
chore: change drizzle config to pattern match schema files
jasutiin ea12a7b
fix: add mealSchema to drizzle client
jasutiin 50ddf94
fix: remove checkedInBy in page.tsx and convert str to Date
jasutiin dcf2ceb
fix: make meal procedures protected
jasutiin 62aba63
add todos for buttons in meal page.tsx for pending state
jasutiin b4a7007
Merge branch 'main' into HMTV2-21-generate-food-timeslot
jasutiin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| "use client"; | ||
|
|
||
| import { useState } from "react"; | ||
| import { api } from "@/trpc/react"; | ||
|
|
||
| export default function MealPage() { | ||
| const [title, setTitle] = useState(""); | ||
| const [startTime, setStartTime] = useState<Date | null>(null); | ||
| const [endTime, setEndTime] = useState<Date | null>(null); | ||
| const [mealId, setMealId] = useState(""); | ||
| const [userId, setUserId] = useState(""); | ||
|
|
||
| const createMeal = api.meals.addMeal.useMutation(); | ||
| const scanUserIn = api.meals.scanUserIn.useMutation(); | ||
|
|
||
| function handleCreateMeal() { | ||
| if (!startTime || !endTime) return; | ||
| createMeal.mutate({ title, startTime, endTime }); | ||
| } | ||
|
|
||
| function handleScanUserIn() { | ||
| scanUserIn.mutate({ mealId, userId }); | ||
| } | ||
|
|
||
| return ( | ||
| <main> | ||
| <header> | ||
| <h1>Meal Portal</h1> | ||
| </header> | ||
| <div> | ||
| <div> | ||
| <h2>Create a meal</h2> | ||
| <label htmlFor="title">Title:</label> | ||
| <input | ||
| id="title" | ||
| name="title" | ||
| onChange={(e) => setTitle(e.target.value)} | ||
| type="text" | ||
| /> | ||
| <label htmlFor="start-time">Start time:</label> | ||
| <input | ||
| id="start-time" | ||
| name="start-time" | ||
| onChange={(e) => { | ||
| const nextStartTime = new Date(e.target.value); | ||
| setStartTime( | ||
| Number.isNaN(nextStartTime.getTime()) ? null : nextStartTime | ||
| ); | ||
| }} | ||
| type="datetime-local" | ||
| /> | ||
| <label htmlFor="end-time">End time:</label> | ||
| <input | ||
| id="end-time" | ||
| name="end-time" | ||
| onChange={(e) => { | ||
| const nextEndTime = new Date(e.target.value); | ||
| setEndTime( | ||
| Number.isNaN(nextEndTime.getTime()) ? null : nextEndTime | ||
| ); | ||
| }} | ||
| type="datetime-local" | ||
| /> | ||
| {/* TODO: make buttons use loading state after mutations are fired */} | ||
| <button | ||
| onClick={() => { | ||
| handleCreateMeal(); | ||
| }} | ||
| type="button" | ||
| > | ||
| Submit | ||
| </button> | ||
| </div> | ||
| <div> | ||
| <h2>Scan user in for a meal</h2> | ||
| <label htmlFor="meal-id">Meal Id:</label> | ||
| <input | ||
| id="meal-id" | ||
| name="meal-id" | ||
| onChange={(e) => setMealId(e.target.value)} | ||
| type="text" | ||
| /> | ||
| <label htmlFor="user-id">User Id:</label> | ||
| <input | ||
| id="start-time" | ||
| name="start-time" | ||
| onChange={(e) => setUserId(e.target.value)} | ||
| type="text" | ||
| /> | ||
| {/* TODO: make buttons use loading state after mutations are fired */} | ||
| <button | ||
| onClick={() => { | ||
| handleScanUserIn(); | ||
| }} | ||
| type="button" | ||
| > | ||
| Submit | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </main> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { TRPCError } from "@trpc/server"; | ||
| import { z } from "zod"; | ||
| import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc"; | ||
| import { meal, mealAttendance } from "@/server/db/meal-schema"; | ||
|
|
||
| export const mealsRouter = createTRPCRouter({ | ||
| addMeal: protectedProcedure | ||
| .input( | ||
| z | ||
| .object({ | ||
| title: z.string().trim().min(1), | ||
| startTime: z.coerce.date(), | ||
| endTime: z.coerce.date() | ||
| }) | ||
| .refine((data) => data.endTime > data.startTime, { | ||
| message: "End time must be after start time.", | ||
| path: ["endTime"] | ||
| }) | ||
| ) | ||
| .mutation(async ({ input, ctx }) => { | ||
| const [newMeal] = await ctx.db | ||
| .insert(meal) | ||
| .values({ | ||
| title: input.title, | ||
| startTime: input.startTime, | ||
| endTime: input.endTime | ||
| }) | ||
| .returning(); | ||
| return newMeal; | ||
| }), | ||
|
|
||
| scanUserIn: protectedProcedure | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is nitpicky but we have an |
||
| .input( | ||
| z.object({ | ||
| mealId: z.string().uuid(), | ||
| userId: z.string().min(1) | ||
| }) | ||
| ) | ||
| .mutation(async ({ input, ctx }) => { | ||
| const [record] = await ctx.db | ||
| .insert(mealAttendance) | ||
| .values({ | ||
| mealId: input.mealId, | ||
| userId: input.userId | ||
| }) | ||
| .onConflictDoNothing({ | ||
| target: [mealAttendance.userId, mealAttendance.mealId] | ||
| }) | ||
| .returning(); | ||
|
|
||
| if (!record) { | ||
| throw new TRPCError({ | ||
| code: "CONFLICT", | ||
| message: "User is already checked in for this meal." | ||
| }); | ||
| } | ||
|
|
||
| return record; | ||
| }) | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { relations } from "drizzle-orm"; | ||
| import { | ||
| pgTableCreator, | ||
| text, | ||
| timestamp, | ||
| unique, | ||
| uuid | ||
| } from "drizzle-orm/pg-core"; | ||
| import { user } from "./auth-schema"; | ||
|
|
||
| export const createTable = pgTableCreator((name) => `hackathon_${name}`); | ||
|
|
||
| export const meal = createTable("meal", { | ||
| id: uuid("id").primaryKey().defaultRandom(), | ||
| title: text("title").notNull(), // can be breakfast, lunch, dinner, breakfast leftovers... | ||
| startTime: timestamp("start_time", { withTimezone: true }).notNull().unique(), | ||
| endTime: timestamp("end_time", { withTimezone: true }).notNull(), | ||
| createdAt: timestamp("created_at", { withTimezone: true }) | ||
| .defaultNow() | ||
| .notNull(), | ||
| updatedAt: timestamp("updated_at", { withTimezone: true }) | ||
| .defaultNow() | ||
| .$onUpdate(() => new Date()) | ||
| .notNull() | ||
| }); | ||
|
|
||
| export const mealAttendance = createTable( | ||
| "meal_attendance", | ||
| { | ||
| id: uuid("id").primaryKey().defaultRandom(), | ||
| userId: text("user_id") | ||
| .notNull() | ||
| .references(() => user.id, { onDelete: "cascade" }), | ||
| mealId: uuid("meal_id") | ||
| .notNull() | ||
| .references(() => meal.id, { onDelete: "cascade" }), | ||
| // createdAt is used to check when the user checked in for the meal | ||
| createdAt: timestamp("created_at", { withTimezone: true }) | ||
| .defaultNow() | ||
| .notNull(), | ||
| updatedAt: timestamp("updated_at", { withTimezone: true }) | ||
| .defaultNow() | ||
| .$onUpdate(() => new Date()) | ||
| .notNull() | ||
| }, | ||
| (t) => [ | ||
| unique().on(t.userId, t.mealId) // each user can only attend a meal once | ||
| ] | ||
| ); | ||
|
|
||
| export const mealRelations = relations(meal, ({ many }) => ({ | ||
| attendance: many(mealAttendance) | ||
| })); | ||
|
|
||
| export const mealAttendanceRelations = relations(mealAttendance, ({ one }) => ({ | ||
| user: one(user, { | ||
| fields: [mealAttendance.userId], | ||
| references: [user.id] | ||
| }), | ||
| meal: one(meal, { | ||
| fields: [mealAttendance.mealId], | ||
| references: [meal.id] | ||
| }) | ||
| })); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.