From 042e3b9862b253fb3c3e59ee628dd9e30edf7e35 Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Tue, 29 Mar 2022 20:17:08 -0600 Subject: [PATCH] Chatrooms created --- server/controllers/chat_room.controller.ts | 12 ++++++ .../migrations/1648605030863-AddChatRoom.ts | 43 +++++++++++++++++++ server/entities/chat_room.entity.ts | 19 ++++++++ server/modules/chat_room.module.ts | 13 ++++++ .../providers/services/chat_room.service.ts | 24 +++++++++++ 5 files changed, 111 insertions(+) create mode 100644 server/controllers/chat_room.controller.ts create mode 100644 server/database/migrations/1648605030863-AddChatRoom.ts create mode 100644 server/entities/chat_room.entity.ts create mode 100644 server/modules/chat_room.module.ts create mode 100644 server/providers/services/chat_room.service.ts diff --git a/server/controllers/chat_room.controller.ts b/server/controllers/chat_room.controller.ts new file mode 100644 index 0000000..5a8da66 --- /dev/null +++ b/server/controllers/chat_room.controller.ts @@ -0,0 +1,12 @@ +import { Controller, Get } from '@nestjs/common'; +import { ChatRoomService } from 'server/providers/services/chat_room.service'; + +@Controller() +export class ChatRoomController { + constructor(private chatRoomService: ChatRoomService) {} + + @Get('/chat_rooms') + async get() { + return await this.chatRoomService.all(); + } +} diff --git a/server/database/migrations/1648605030863-AddChatRoom.ts b/server/database/migrations/1648605030863-AddChatRoom.ts new file mode 100644 index 0000000..d8eed52 --- /dev/null +++ b/server/database/migrations/1648605030863-AddChatRoom.ts @@ -0,0 +1,43 @@ +import { MigrationInterface, QueryRunner, Table } from 'typeorm'; + +export class AddChatRoom1648605030863 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.createTable( + new Table({ + name: 'chatroom', + columns: [ + { + name: 'id', + type: 'int', + isPrimary: true, + isGenerated: true, + }, + { + name: 'name', + type: 'text', + isNullable: false, + }, + { + name: 'latitude', + type: 'float', + isNullable: false, + }, + { + name: 'longitude', + type: 'float', + isNullable: false, + }, + { + name: 'radius', + type: 'float', + isNullable: false, + }, + ], + }), + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.dropTable('chatroom'); + } +} diff --git a/server/entities/chat_room.entity.ts b/server/entities/chat_room.entity.ts new file mode 100644 index 0000000..9550d26 --- /dev/null +++ b/server/entities/chat_room.entity.ts @@ -0,0 +1,19 @@ +import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; + +@Entity() +export class ChatRoom { + @PrimaryGeneratedColumn() + id: number; + + @Column() + name: string; + + @Column() + latitude: number; + + @Column() + longitude: number; + + @Column() + radius: number; +} diff --git a/server/modules/chat_room.module.ts b/server/modules/chat_room.module.ts new file mode 100644 index 0000000..07b2553 --- /dev/null +++ b/server/modules/chat_room.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { ChatRoomController } from 'server/controllers/chat_room.controller'; +import { ChatRoom } from 'server/entities/chat_room.entity'; +import { ChatRoomService } from 'server/providers/services/chat_room.service'; + +@Module({ + imports: [TypeOrmModule.forFeature([ChatRoom])], + controllers: [ChatRoomController], + providers: [ChatRoomService], + exports: [TypeOrmModule], +}) +export class UsersModule {} diff --git a/server/providers/services/chat_room.service.ts b/server/providers/services/chat_room.service.ts new file mode 100644 index 0000000..6c87539 --- /dev/null +++ b/server/providers/services/chat_room.service.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { ChatRoom } from 'server/entities/chat_room.entity'; + +@Injectable() +export class ChatRoomService { + constructor( + @InjectRepository(ChatRoom) + private chatRoomRepository: Repository, + ) {} + + create(chatRoom: ChatRoom) { + return this.chatRoomRepository.save(chatRoom); + } + + all() { + return this.chatRoomRepository.find(); + } + + findById(id: number) { + return this.chatRoomRepository.findOne(id); + } +}