-
Notifications
You must be signed in to change notification settings - Fork 0
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
チャットルーム作成機能を実装した #29
Merged
Merged
チャットルーム作成機能を実装した #29
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8deab77
add: chatroom table
ryo-manba 1deef99
add: websocket package
ryo-manba 8e17b67
remove: unused imports
ryo-manba 41b0243
update: port 3000 -> 3001
ryo-manba 5aee69a
add: chat module
ryo-manba f12e48c
add: chat frontend
ryo-manba 6d376fd
Merge branch 'main' into add-create-chat-room
ryo-manba a6fe610
update: devDependencies
ryo-manba 6c12d98
add: typesync package
ryo-manba 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 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
26 changes: 26 additions & 0 deletions
26
backend/prisma/migrations/20221025054307_chatroom/migration.sql
This file contains 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,26 @@ | ||
-- CreateTable | ||
CREATE TABLE "ChatRoom" ( | ||
"id" SERIAL NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"type" BOOLEAN NOT NULL, | ||
"author" TEXT NOT NULL, | ||
"hashedPassword" TEXT NOT NULL, | ||
|
||
CONSTRAINT "ChatRoom_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Message" ( | ||
"id" SERIAL NOT NULL, | ||
"roomId" INTEGER NOT NULL, | ||
"userId" INTEGER NOT NULL, | ||
"message" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Message_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Message" ADD CONSTRAINT "Message_roomId_fkey" FOREIGN KEY ("roomId") REFERENCES "ChatRoom"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Message" ADD CONSTRAINT "Message_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains 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 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 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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ChatGateway } from './chat.gateway'; | ||
|
||
describe('ChatGateway', () => { | ||
let gateway: ChatGateway; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ChatGateway], | ||
}).compile(); | ||
|
||
gateway = module.get<ChatGateway>(ChatGateway); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(gateway).toBeDefined(); | ||
}); | ||
}); |
This file contains 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,37 @@ | ||
import { | ||
MessageBody, | ||
SubscribeMessage, | ||
WebSocketGateway, | ||
WebSocketServer, | ||
} from '@nestjs/websockets'; | ||
import { Server } from 'socket.io'; | ||
import { Logger } from '@nestjs/common'; | ||
import { PrismaService } from '../prisma.service'; | ||
import { ChatService } from './chat.service'; | ||
import { ChatRoom as ChatRoomModel } from '@prisma/client'; | ||
|
||
@WebSocketGateway({ | ||
cors: { | ||
origin: '*', | ||
}, | ||
namespace: '/chat', | ||
}) | ||
export class ChatGateway { | ||
constructor( | ||
private prisma: PrismaService, | ||
private readonly chatService: ChatService, | ||
) {} | ||
|
||
@WebSocketServer() server: Server; | ||
|
||
private logger: Logger = new Logger('ChatGateway'); | ||
|
||
@SubscribeMessage('room:create') | ||
CreateRoom(@MessageBody() data: ChatRoomModel): void { | ||
this.logger.log(`[DEBUG] room:create': ${data.name}`); | ||
// とりあえずvoidで受ける | ||
void this.chatService.createChatRoom(data); | ||
// 送信者にdataを送り返す | ||
this.server.emit('room:created', data); | ||
} | ||
} |
This file contains 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,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ChatGateway } from './chat.gateway'; | ||
import { ChatService } from './chat.service'; | ||
import { PrismaService } from '../prisma.service'; | ||
|
||
@Module({ | ||
providers: [ChatGateway, ChatService, PrismaService], | ||
}) | ||
export class ChatModule {} |
This file contains 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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ChatService } from './chat.service'; | ||
|
||
describe('ChatService', () => { | ||
let service: ChatService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ChatService], | ||
}).compile(); | ||
|
||
service = module.get<ChatService>(ChatService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains 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 { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '../prisma.service'; | ||
import { ChatRoom, Prisma } from '@prisma/client'; | ||
|
||
@Injectable() | ||
export class ChatService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async chatRoom( | ||
charRoomWhereUniqueInput: Prisma.ChatRoomWhereUniqueInput, | ||
): Promise<ChatRoom | null> { | ||
return this.prisma.chatRoom.findUnique({ | ||
where: charRoomWhereUniqueInput, | ||
}); | ||
} | ||
|
||
async chatRooms(params: { | ||
skip?: number; | ||
take?: number; | ||
cursor?: Prisma.ChatRoomWhereUniqueInput; | ||
where?: Prisma.ChatRoomWhereInput; | ||
orderBy?: Prisma.ChatRoomOrderByWithRelationInput; | ||
}): Promise<ChatRoom[]> { | ||
const { skip, take, cursor, where, orderBy } = params; | ||
|
||
return this.prisma.chatRoom.findMany({ | ||
skip, | ||
take, | ||
cursor, | ||
where, | ||
orderBy, | ||
}); | ||
} | ||
|
||
async createChatRoom(data: Prisma.ChatRoomCreateInput): Promise<ChatRoom> { | ||
return this.prisma.chatRoom.create({ | ||
data, | ||
}); | ||
} | ||
|
||
async updateChatRoom(params: { | ||
where: Prisma.ChatRoomWhereUniqueInput; | ||
data: Prisma.ChatRoomUpdateInput; | ||
}): Promise<ChatRoom> { | ||
const { where, data } = params; | ||
|
||
return this.prisma.chatRoom.update({ | ||
data, | ||
where, | ||
}); | ||
} | ||
|
||
async deleteChatRoom( | ||
where: Prisma.ChatRoomWhereUniqueInput, | ||
): Promise<ChatRoom> { | ||
return this.prisma.chatRoom.delete({ | ||
where, | ||
}); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.
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.
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.
これbackend全体で統一して使ったほうが見栄えよさそうですね
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.
確かに全体で統一してもいいかもですね。
でもnewするときに名前を指定できるから、モジュールごととかでもいいのかな。
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.
導入するかどうかを調査するissue作っときます
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.
ドキュメントで各サービスごとにインスタンス化することを推奨していたので、今の感じでいきましょう
#43