-
Notifications
You must be signed in to change notification settings - Fork 2
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
add routes to handle create, delete and update team #68
Conversation
import { StatusCodes } from 'src/constants/status-codes.ts'; | ||
import type { TeamCreateBody } from 'src/types/data/team'; | ||
|
||
export const createTeamSql = async (context: Context<EnvironmentBindings>) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: So, even if we are not doing MVC, it would be good to have some separation of concerns here.
That means, interactions with the database like creating the SQL for it should consume the already parsed object.
Think it this way: who's responsibility is to validate if the request is actually a JSON instead of something else?
That makes more sense to be handled by the request handler, as it handles "low level" things, like validating a request is actually in the proper format (i.e. headers are correct, the JSON is a JSON, etc.).
So, here it makes more sense here to split this function in three parts:
- Request handler to do all things related to the request.
- DB insertion handler to communicate with the database.
- Data validator to throw an error in case there is any problem with the data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made the changes for @heem42 utilizing the above structure(Zod for validation) but we will need to move the functions to there own files in another pull requests.
const body: TeamCreateBody = await context.req.json(); | ||
|
||
if (!body.name) { | ||
return context.json({ error: 'name is required to create a team' }, StatusCodes.INTERNAL_SERVER_ERROR); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: On the same lines about separation of concerns, here would be better to throw an error instead of sending a response.
That way, the only part of the code that returns a response to the network is the request handler, every other function either returns or throws an error.
Also, on this case the status code should be a 415 Unsupported Media Type
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added suggestion to CONTRIBUTION.MD update and updated error handling.
} | ||
|
||
if (!body.happenedAt) { | ||
return context.json({ error: 'happenedAt is required to create a team' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Here again on the sense of "separation of concerns", what about we have all of those checks for validation in a validateTeamRequestData
function?
Most probably we will want to run other validations, like, if the name is a string and is not empty, etc. So if we move those checks to a function, they will be all in one place and will be easier to manage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created issue to address this #70
@@ -1,6 +1,6 @@ | |||
import type { Context } from 'hono'; | |||
import { StatusCodes } from '../../constants/status-codes.ts'; | |||
import type { Profile } from '../../db/profile'; | |||
import type { Profile } from 'src/types/data/profile'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: Please keep relative imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're using vscode, you can add /.vscode/settings.json
like this below.
{
"typescript.preferences.importModuleSpecifier": "relative"
}
@@ -3,28 +3,26 @@ import { StatusCodes } from '../../constants/status-codes.ts'; | |||
|
|||
export const getAllTeamsSql = async (context: Context<EnvironmentBindings>) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Again here the same comment: let's split the code into request handler, sql handler, and validator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every entity will have a directory container all three files or are we utilizing another file/directory structure?
import { StatusCodes } from 'src/constants/status-codes.ts'; | ||
import type { TeamUpdateBody } from 'src/types/data/team'; | ||
|
||
export const updateTeamById = async (context: Context<EnvironmentBindings>) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Same as before, specially here as we need strong validation. Split the function into request handler, sql handler, and validator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File split in 3 functions which will moved to individual files in future pull requests.
@kbventures what are your thoughts on adding a validation lidrary, like Zod or Superstruct? |
updateValues.push(body.happenedAt); | ||
} | ||
|
||
if (updateFields.length === 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: error handling should be first in processing to shortcut the function.
It makse sure our code only executes on reliable data and avoids computing things if it will error out anyways.
const insertedAt = new Date().toISOString(); | ||
const happenedAt = new Date(body.happenedAt).toISOString(); | ||
|
||
const createTeam = await context.env.database.prepare( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick:
const createTeam = await context.env.database.prepare( | |
const teamCreationResult = await context.env.database.prepare( |
.bind(id, body.name, SCHEMA_VERSION, body.description ?? '', happenedAt, insertedAt) | ||
.run(); | ||
|
||
return context.json(createTeam); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: We don't want to return the result straight from the database here, we want to process the response from the database and handle errors and other exceptions!
Please review Cloudflare's docs and return a meaningful and informative message to the client.
Think about it, a message saying "Error 0x234571" doesn't provide a good experience for the client. We should provide a better message, including the correct HTTP status code if the operation was successful or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work @heem42
Normally we would wait to let you make the changes but this issue is blocking other road map items.
@@ -3,28 +3,26 @@ import { StatusCodes } from '../../constants/status-codes.ts'; | |||
|
|||
export const getAllTeamsSql = async (context: Context<EnvironmentBindings>) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every entity will have a directory container all three files or are we utilizing another file/directory structure?
import { StatusCodes } from 'src/constants/status-codes.ts'; | ||
import type { TeamCreateBody } from 'src/types/data/team'; | ||
|
||
export const createTeamSql = async (context: Context<EnvironmentBindings>) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made the changes for @heem42 utilizing the above structure(Zod for validation) but we will need to move the functions to there own files in another pull requests.
import { StatusCodes } from 'src/constants/status-codes.ts'; | ||
import type { TeamUpdateBody } from 'src/types/data/team'; | ||
|
||
export const updateTeamById = async (context: Context<EnvironmentBindings>) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File split in 3 functions which will moved to individual files in future pull requests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: Use tabs for formatting
Co-authored-by: Marco Campos <[email protected]>
Co-authored-by: Marco Campos <[email protected]>
Co-authored-by: Marco Campos <[email protected]>
Thank you, @madcampos, for your thoughtful comments and to @kbventures for implementing those fixes. Apologies for the delay in responding—I've been tied up with other work. I really appreciate your efforts on this! Please let me know if there are any further changes required. |
@@ -0,0 +1,59 @@ | |||
import { z } from 'zod'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you installed zod? I can't find zod dependency added in the package.json in your PR
Summary
Added routes to handle create, delete and update the teams
Tests
Manually tested using Postman
Additional information
Issue #64