dan2811 / yamaha

https://yms-derby.vercel.app
0 stars 1 forks source link

Add “create” procedure to rooms router #2

Closed dan2811 closed 3 months ago

dan2811 commented 3 months ago

The API is written using TRPC. It stands for typesafe remote procedure call. Essentially there is a http endpoint like http://localhost:3000/api/trpc and we use a function provided by trpc on the frontend to make calls to this api endpoint. That endpoint actually has a “router” attached to it. This can be seen in the code here. This router is made up of lots of smaller routers that kind of correlate to different tables in the db, this is just a naming convention, they are not actually connected in any way. Then on these smaller routers, we can add procedures, we can call these whatever we want, they are just functions that most of the time will interact with the database and return a response. On the frontend we use a react hook which comes from trpc and this hook knows the shape of all of our routers on the backend, so we get really nice autocomplete on the frontend and it means we get type errors if we try to pass in the wrong input to the wrong procedure.

Your first task is to add a “create” procedure to the room router.

The “create” procedure should take in an object with a name and a description. It should call the db and insert a record into the rooms table. Here is an example of a “create” procedure on another router.

First thing to do is to clone the repo so you have a loca copy Then create a new branch off of main. Call it something short that describes what you will be doing. Then try and write the code. Push the changes to the remote Then open a pull request with a short description and request a review.

Ask me as many questions as you need and if you want to arrange a pair programming session, then let me know :)

dan2811 commented 3 months ago

Also, just for some context. In a future task we can use this API to create a page in the admin console that allows admins to create rooms.

One more thing you should know about trpc: there are two types of procedure, query and mutations. Query should be used for procedures that return data and do not modify anything. Mutations should be used for modifying or creating things. See the trpc docs for more info if you’re interested.

And I realised I didn’t explain the API validation that we’re using. Zod is a NPM package that provides a great way of validating inputs, it works really well with TRPC. The example I linked to earlier shows you how to use Zod to define an input schema. Then trpc and zod together will throw an error if the input is not of the expected type.