Skip to content

Commit

Permalink
initialsed get, get all and add methods for chat schema (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Devesh21700Kumar authored Apr 8, 2021
1 parent 8b206a7 commit 66791b8
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AuthModule } from './auth/auth.module';
import { MongooseModule } from '@nestjs/mongoose';
import { FirebaseModule } from './firebase/firebase.module';
import { UserModule } from './user/user.module';
import { ChatModule } from './chat/chat.module';
import * as path from 'path';
import { CourseModule } from './modules/course/course.module';

Expand All @@ -19,6 +20,7 @@ import { CourseModule } from './modules/course/course.module';
MongooseModule.forRoot('mongodb://localhost/nest'),
UserModule,
CourseModule,
ChatModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
101 changes: 101 additions & 0 deletions src/chat/chat.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {
Controller,
Get,
Res,
HttpStatus,
Post,
Body,
Put,
Query,
NotFoundException,
Delete,
Param,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { ChatService } from './chat.service'; //eslint-disable-line
import { CreateChatDTO } from './dto/create-chat.dto'; //eslint-disable-line
import { ApiCreatedResponse, ApiProperty } from '@nestjs/swagger';

class ChatResponseBody {
@ApiProperty({ required: true, example: '605e3fd9acc33583fb389aec' })
_id: string;

@ApiProperty({ required: true, example: 'Noob' })
first_name: string;

@ApiProperty({ required: true, example: 'Coder' })
last_name: string;

@ApiProperty({ required: true, example: '[email protected]' })
email: string;

@ApiProperty({ required: true, example: '+919999999999' })
phone: string;

@ApiProperty({ required: true, example: 'A-88, Mayur Vihar, Delhi' })
address: string;

@ApiProperty({ required: true, example: 'I am Noob Coder' })
description: string;
}

@Controller('Chat')
export class ChatController {
constructor(private ChatService: ChatService) {}

// add a Chat
@Post()
@UsePipes(ValidationPipe)
async addChat(@Res() res, @Body() CreateChatDTO: CreateChatDTO) {
const Chat = await this.ChatService.addChat(CreateChatDTO);
return res.status(HttpStatus.OK).json({
message: 'Chat has been created successfully',
Chat,
});
}

// Retrieve Chats list
@ApiCreatedResponse({ type: [ChatResponseBody] })
@Get()
async getAllChat(@Res() res) {
const Chats = await this.ChatService.getAllChat();
return res.status(HttpStatus.OK).json(Chats);
}

// Fetch a particular Chat using ID
@ApiCreatedResponse({ type: ChatResponseBody })
@Get('/:chatId')
async getChat(@Res() res, @Param('chatId') chatId: string) {
const Chat = await this.ChatService.getChat(chatId);
return res.status(HttpStatus.OK).json(Chat);
}

@Put('/update')
async updateChat(
@Res() res,
@Query('uid') uid,
@Body() createChatDTO: CreateChatDTO,
) {
console.log('ChatId', uid);
const Chat = await this.ChatService.updateChat(uid, createChatDTO);

if (!Chat) throw new NotFoundException('Chat does not exist!');

return res.status(HttpStatus.OK).json({
message: 'Chat has been successfully updated',
Chat: Chat,
});
}

// Delete a Chat
@Delete('/delete')
async deleteChat(@Res() res, @Query('uid') uid) {
const Chat = await this.ChatService.deleteChat(uid);
if (!Chat) throw new NotFoundException('Chat does not exist');
return res.status(HttpStatus.OK).json({
message: 'Chat has been deleted',
Chat: Chat,
});
}
}
11 changes: 11 additions & 0 deletions src/chat/chat.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { ChatController } from './chat.controller';
import { ChatService } from './chat.service';
import { MongooseModule } from '@nestjs/mongoose';
import { ChatSchema } from '../schemas/chat.schema';
@Module({
imports: [MongooseModule.forFeature([{ name: 'Chat', schema: ChatSchema }])],
controllers: [ChatController],
providers: [ChatService],
})
export class ChatModule {}
55 changes: 55 additions & 0 deletions src/chat/chat.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { Chat } from './interfaces/chat.interface';
import { CreateChatDTO } from './dto/create-chat.dto';

@Injectable()
export class ChatService {
constructor(@InjectModel('Chat') private readonly ChatModel: Model<Chat>) {}

// fetch all Chats
async getAllChat(): Promise<Chat[]> {
const Chats = await this.ChatModel.find().exec();
return Chats;
}

// Get a single Chat
async getChat(ChatId): Promise<Chat> {
const Chat = await this.ChatModel.findById(ChatId).exec();

if (Chat) {
return Chat;
}

throw new HttpException(
{
status: HttpStatus.NOT_FOUND,
error: 'Chat Not Found',
},
HttpStatus.NOT_FOUND,
);
}

// post a single Chat
async addChat(CreateChatDTO: CreateChatDTO): Promise<Chat> {
const newChat = await new this.ChatModel(CreateChatDTO);
return newChat.save();
}

// Edit Chat details
async updateChat(ChatID, CreateChatDTO: CreateChatDTO): Promise<Chat> {
const updatedChat = await this.ChatModel.findByIdAndUpdate(
ChatID,
CreateChatDTO,
{ new: true },
);
return updatedChat;
}

// Delete a Chat
async deleteChat(ChatID): Promise<any> {
const deletedChat = await this.ChatModel.findByIdAndRemove(ChatID);
return deletedChat;
}
}
9 changes: 9 additions & 0 deletions src/chat/dto/create-chat.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IsNotEmpty } from 'class-validator';

export class CreateChatDTO {
@IsNotEmpty()
readonly sender: string;

readonly original_sender: string;
readonly chats: string;
}
8 changes: 8 additions & 0 deletions src/chat/interfaces/chat.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Document } from 'mongoose';

export interface Chat extends Document {
readonly id: string;
readonly sender: string;
readonly original_sender: string;
readonly chats: string;
}
6 changes: 3 additions & 3 deletions src/modules/course/course.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export class CourseController {
}

// get selected course
@Get('/:CourseID')
async getSelectedCourse(@Res() res: Response, @Param('CourseID') CourseID) {
const course = await this.courseService.getSelectedCourse(CourseID);
@Get('/:courseID')
async getSelectedCourse(@Res() res: Response, @Param('courseID') courseID) {
const course = await this.courseService.getSelectedCourse(courseID);
if (!course) {
throw new NotFoundException('Selected course not found');
}
Expand Down
21 changes: 21 additions & 0 deletions src/schemas/chat.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type ChatDocument = Chat & Document;

@Schema()
export class Chat {
@Prop({ required: true })
id: string;

@Prop({ required: true })
sender: string;

@Prop({ required: true })
original_sender: string;

@Prop({ required: true })
chats: string;
}

export const ChatSchema = SchemaFactory.createForClass(Chat);

0 comments on commit 66791b8

Please sign in to comment.