Skip to content
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

Merged
merged 32 commits into from
Jan 20, 2025

Conversation

heem42
Copy link
Contributor

@heem42 heem42 commented Dec 26, 2024

Summary

Added routes to handle create, delete and update the teams

Tests

Manually tested using Postman

Additional information

Issue #64

@heem42 heem42 self-assigned this Dec 26, 2024
import { StatusCodes } from 'src/constants/status-codes.ts';
import type { TeamCreateBody } from 'src/types/data/team';

export const createTeamSql = async (context: Context<EnvironmentBindings>) => {
Copy link
Collaborator

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:

  1. Request handler to do all things related to the request.
  2. DB insertion handler to communicate with the database.
  3. Data validator to throw an error in case there is any problem with the data.

Copy link
Contributor

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);
Copy link
Collaborator

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.

Copy link
Contributor

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.

Issue #71

}

if (!body.happenedAt) {
return context.json({ error: 'happenedAt is required to create a team' });
Copy link
Collaborator

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.

Copy link
Contributor

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';
Copy link
Collaborator

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

Copy link
Contributor

@pmjuu pmjuu Jan 14, 2025

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>) => {
Copy link
Collaborator

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.

Copy link
Contributor

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>) => {
Copy link
Collaborator

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.

Copy link
Contributor

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.

@madcampos
Copy link
Collaborator

@kbventures what are your thoughts on adding a validation lidrary, like Zod or Superstruct?

@pmjuu pmjuu linked an issue Jan 10, 2025 that may be closed by this pull request
updateValues.push(body.happenedAt);
}

if (updateFields.length === 0) {
Copy link
Collaborator

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(
Copy link
Collaborator

@madcampos madcampos Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick:

Suggested change
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);
Copy link
Collaborator

@madcampos madcampos Jan 15, 2025

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.

@madcampos madcampos requested review from madcampos and pmjuu January 15, 2025 19:47
Copy link
Contributor

@kbventures kbventures left a 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>) => {
Copy link
Contributor

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>) => {
Copy link
Contributor

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>) => {
Copy link
Contributor

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 kbventures added this to the MVP for consuming data milestone Jan 16, 2025
Copy link
Collaborator

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‏

@heem42
Copy link
Contributor Author

heem42 commented Jan 18, 2025

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';
Copy link
Contributor

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

@kbventures kbventures merged commit 2d50644 into setup-d1-database Jan 20, 2025
@kbventures kbventures deleted the team-crud branch January 20, 2025 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Team CRUD
5 participants