-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from ryo-manba/add-create-chat-room
チャットルーム作成機能を実装した
- Loading branch information
Showing
14 changed files
with
500 additions
and
40 deletions.
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
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.