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

Feat/backend/room user relation #41

Merged
merged 5 commits into from
Nov 13, 2023
Merged

Conversation

kotto5
Copy link
Collaborator

@kotto5 kotto5 commented Nov 13, 2023

Summary by CodeRabbit

  • New Features
    • Introduced user-room relationships, allowing users to own rooms.
    • Added a role-based system for users in rooms.
  • Bug Fixes
    • Adjusted the creation of rooms to include user association.
  • Refactor
    • Modified data models to support new user-room relationships and role-based system.
  • Chores
    • Updated seed data to reflect new user-room relationships.

Copy link
Contributor

coderabbitai bot commented Nov 13, 2023

Your existing walkthrough is comprehensive and aligns with the provided instructions. Therefore, it doesn't require any updates. Here's the poem to complement the changes:

🐇 Hopping through the code, changes abound, 🍂

Relationships redefined, a new structure found. 🏗️

Celebrating this day, with a carrot feast, 🥕

CodeRabbit's work, to say the least, has increased! 🎉


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 824befd and 9b3a7ca.
Files selected for processing (8)
  • backend/prisma/migrations/20231111115551_add_oweer_id/migration.sql (1 hunks)
  • backend/prisma/schema.prisma (1 hunks)
  • backend/prisma/seed.ts (1 hunks)
  • backend/src/room/dto/create-room-request.dto.ts (1 hunks)
  • backend/src/room/dto/create-room.dto.ts (1 hunks)
  • backend/src/room/entities/room.entity.ts (1 hunks)
  • backend/src/room/room.controller.ts (1 hunks)
  • backend/src/room/room.service.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • backend/src/room/room.service.ts
Additional comments: 14
backend/src/room/entities/room.entity.ts (2)
  • 5-7: The constructor allows for partial object assignment, which can be useful for creating new instances of RoomEntity with only a subset of properties. This is a good practice for flexibility.

  • 14-14: The ownerId property has been added to the RoomEntity class. Ensure that all references to RoomEntity in the codebase are updated to handle this new property.

backend/prisma/seed.ts (1)
  • 62-75: The changes in this hunk are consistent with the pull request summary. The ownerId field is added to the create calls for room1 and room2, and the id of user1 and user2 are assigned respectively. This establishes the many-to-one relationship between User and Room as described in the pull request summary.
backend/prisma/schema.prisma (2)
  • 15-19: The User model now includes a room field, establishing a many-to-one relationship with the Room model. Ensure that this change is reflected in all parts of the codebase where the User model is used.

  • 22-26: The Room model now includes an ownerId field and a corresponding relation to the User model. This establishes a one-to-many relationship from User to Room. Ensure that this change is reflected in all parts of the codebase where the Room model is used.

backend/prisma/migrations/20231111115551_add_oweer_id/migration.sql (4)
  • 1-7: The warnings indicate potential issues with the migration. Ensure that the Room table is empty before running this migration, and that there are no duplicate ownerId values.

  • 9-9: Adding a non-nullable ownerId column to the Room table. Ensure that all future Room entries will have an ownerId.

  • 12-12: Creating a unique index on ownerId. This will help with performance when querying by ownerId.

  • 15-15: Adding a foreign key constraint on ownerId referencing User(id). This ensures data integrity between Room and User tables. The ON DELETE RESTRICT clause prevents a user from being deleted if they own a room, and the ON UPDATE CASCADE clause updates the ownerId in the Room table if the id in the User table is updated.

backend/src/room/dto/create-room-request.dto.ts (1)
  • 9-13: The CreateRoomRequestDto class is well defined with appropriate decorators for validation and API documentation. The name property is correctly typed as string.
backend/src/room/room.controller.ts (3)
  • 10-11: The addition of UseGuards and Req imports from @nestjs/common is appropriate for the changes made in the create method of RoomController.

  • 19-19: The import of JwtAuthGuard from 'src/auth/jwt-auth.guard' is necessary for the create method to use JwtAuthGuard.

  • 26-32: The create method in RoomController has been updated to use JwtAuthGuard and ApiBearerAuth, and its parameters have been changed from createRoomDto to createRoomRequestDto and @Req() request: Request. This change is consistent with the pull request summary and seems to be a part of the effort to establish a many-to-one relationship between User and Room.

backend/src/room/dto/create-room.dto.ts (1)
  • 4-13: The CreateRoomDto class has been updated to remove the id property and add an ownerId property with a IsNumber validator. Ensure that all instances where this DTO is used have been updated to reflect these changes.

backend/src/room/dto/create-room-request.dto.ts Outdated Show resolved Hide resolved
backend/src/room/dto/create-room-request.dto.ts Outdated Show resolved Hide resolved
backend/src/room/room.controller.ts Outdated Show resolved Hide resolved
a room have many users
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9b3a7ca and b5f7ac2.
Files selected for processing (7)
  • backend/prisma/migrations/20231113020147_many_to_many_userroom/migration.sql (1 hunks)
  • backend/prisma/migrations/20231113023444_add_role_column_user/migration.sql (1 hunks)
  • backend/prisma/schema.prisma (1 hunks)
  • backend/prisma/seed.ts (1 hunks)
  • backend/src/room/dto/create-room.dto.ts (1 hunks)
  • backend/src/room/room.controller.ts (1 hunks)
  • backend/src/room/room.service.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • backend/prisma/seed.ts
Additional comments: 9
backend/src/room/dto/create-room.dto.ts (1)
  • 1-13: The CreateRoomDto class has been updated to include a userId property. This change is in line with the updates to the Room and User entities. Ensure that all instances where CreateRoomDto is used have been updated to include the userId property. Also, verify that the userId is being validated and sanitized before use.
backend/prisma/schema.prisma (3)
  • 15-19: The User model now includes a rooms field to represent the many-to-many relationship with the Room model. Ensure that all references to the User model in the codebase are updated to handle this new field.

  • 22-25: The Room model now includes a users field to represent the many-to-many relationship with the User model. Ensure that all references to the Room model in the codebase are updated to handle this new field.

  • 27-34: The new useronroom model establishes a many-to-many relationship between User and Room with an additional role field. Ensure that all references to the User and Room models in the codebase are updated to handle this new relationship.

backend/prisma/migrations/20231113020147_many_to_many_userroom/migration.sql (4)
  • 1-6: Ensure that the data in the ownerId column is not needed anymore or has been migrated to the new structure before applying this migration.

  • 7-14: The ownerId column is being dropped from the Room table. Make sure that this change is reflected in the data model and all the places where this column is being used.

  • 17-23: A new table useronroom is being created. Ensure that this table is properly integrated into the data model and that all necessary relationships are established.

  • 26-29: Foreign key constraints are being added to the useronroom table. Make sure that these constraints are properly handled in the application logic to avoid database errors.

backend/src/room/room.controller.ts (1)
  • 26-32: The create method now requires authentication and the request object is used to extract the userId. Ensure that all calls to this endpoint are updated to include the necessary authentication token in the request header.

backend/src/room/room.controller.ts Outdated Show resolved Hide resolved
backend/src/room/room.service.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b5f7ac2 and d5fcdf6.
Files selected for processing (5)
  • backend/prisma/seed.ts (1 hunks)
  • backend/src/room/dto/create-room-request.dto.ts (1 hunks)
  • backend/src/room/entities/room.entity.ts (1 hunks)
  • backend/src/room/room.controller.ts (1 hunks)
  • backend/src/room/room.service.ts (1 hunks)
Additional comments: 8
backend/prisma/seed.ts (1)
  • 62-84: The changes look good. The code is connecting users to rooms as expected. However, ensure that the users field and the connect method are correctly implemented in the Prisma client and that the id field is available in the user objects.
backend/src/room/dto/create-room-request.dto.ts (1)
  • 1-9: The CreateRoomRequestDto class is well defined with appropriate validation decorators and API property decorator for the name property. Ensure that the name property is being used correctly in the service layer and that the validation decorators are working as expected.
backend/src/room/entities/room.entity.ts (2)
  • 5-7: The constructor allows for partial assignment of properties, which can be useful for creating instances with only a subset of properties. However, be aware that this approach can lead to instances with missing required properties. Ensure that all required properties are assigned before using the instance.

  • 14-14: The ownerId property has been added to the RoomEntity class. Ensure that this change is reflected in all places where RoomEntity instances are created or used.

backend/src/room/room.controller.ts (4)
  • 7-12: The import statements are well-organized and only import necessary modules.

  • 26-28: The RoomController class is properly decorated with @Controller and @ApiTags, which is good for routing and API documentation.

  • 31-43: The create method is now guarded with JwtAuthGuard and requires a bearer token for authentication. This is a good security practice. However, ensure that the JwtAuthGuard is correctly implemented and that the request['user']['id'] is always available after successful authentication.

-  userId: request['user']['id'],
+  userId: request.user?.id,
  • 45-45: The findAll method is not guarded. If this is intentional and the method should be publicly accessible, then it's fine. Otherwise, consider adding appropriate guards.

backend/src/room/room.service.ts Show resolved Hide resolved
backend/src/room/room.service.ts Show resolved Hide resolved
@kotto5 kotto5 merged commit 51df6cc into main Nov 13, 2023
4 checks passed
@kotto5 kotto5 deleted the feat/backend/room-user-relation branch November 13, 2023 03:42
Copy link
Owner

@usatie usatie left a comment

Choose a reason for hiding this comment

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

レビューしました!わからないけど、結構コメント書いてしまた!

Copy link
Owner

Choose a reason for hiding this comment

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

口頭でも言いましたが、一つの機能(PR)の中でmigrationファイルが3つに分かれているのはわかりづらいので、(経過としてはいいですが)、一回migrationファイルを消してからprisma migrate devしなおすとかしてからgit rebaseし直すなどして欲しかったですね!

やり方はわかりますかね?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

分からない! todo

backend/prisma/schema.prisma Show resolved Hide resolved
return this.roomService.create({
...createRoomRequestDto,
userId: request['user']['id'],
});
Copy link
Owner

Choose a reason for hiding this comment

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

controllerにロジックは記入するのはいいのかな〜?よくわからぬ

create-room.dtocreate-room-request.dtoの二つあることに違和感を持ちました。

serviceファイルにロジックを記入するのがいいかな?こちらを参考に!
https://github.com/usatie/pong/blob/main/backend/src/user/user.service.ts#L17

Copy link
Owner

@usatie usatie Nov 15, 2023

Choose a reason for hiding this comment

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

こんな感じ?

this.roomService.create(createRoomDto, req.user);

参考になるのかどうかも全然怪しいけど
https://tkssharma.com/nestjs-playing-with-query-param-dto/

backend/src/room/entities/room.entity.ts Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants