-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initialsed get, get all and add methods for chat schema (#47)
- Loading branch information
1 parent
8b206a7
commit 66791b8
Showing
8 changed files
with
210 additions
and
3 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
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,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, | ||
}); | ||
} | ||
} |
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,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 {} |
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,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; | ||
} | ||
} |
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 { IsNotEmpty } from 'class-validator'; | ||
|
||
export class CreateChatDTO { | ||
@IsNotEmpty() | ||
readonly sender: string; | ||
|
||
readonly original_sender: string; | ||
readonly chats: string; | ||
} |
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,8 @@ | ||
import { Document } from 'mongoose'; | ||
|
||
export interface Chat extends Document { | ||
readonly id: string; | ||
readonly sender: string; | ||
readonly original_sender: string; | ||
readonly chats: string; | ||
} |
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,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); |