Skip to content
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

Feature/13 kt 16 modify chat functionality #19

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ FROM builder
WORKDIR /app
COPY package*.json ./

ENV NODE_ENV=Production
RUN npm install
COPY --from=builder /app/dist ./dist
EXPOSE ${PORT}
Expand Down
15 changes: 12 additions & 3 deletions src/chat/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { Controller, Post, Body, Get, Query } from '@nestjs/common';
import { ChatService } from './chat.service';
import { CreateChatDto } from './dto/create-chat.dto';
import { Chat } from './entities/chat.entity';
import { FindAllChatDto, FindChatDto } from './dto/find-chat.dto';
import {
FindAllChatDto,
FindAllInMinuteChatDto,
FindChatDto,
} from './dto/find-chat.dto';

@Controller('chat')
export class ChatController {
Expand All @@ -19,9 +23,14 @@ export class ChatController {
return await this.chatService.findAll(dto);
}

@Get('/minute')
async findAllInMinute(@Query() dto: FindAllInMinuteChatDto): Promise<Chat[]> {
return await this.chatService.findAllInMinute(dto);
}

@Get()
async findOne(@Query() readChatDto: FindChatDto): Promise<Chat> {
return await this.chatService.findOne(readChatDto);
async findOne(@Query() dto: FindChatDto): Promise<Chat> {
return await this.chatService.findOne(dto);
}

/*
Expand Down
41 changes: 34 additions & 7 deletions src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,63 @@ import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

import { CreateChatDto } from './dto/create-chat.dto';

import { Chat } from './entities/chat.entity';
import { FindAllChatDto, FindChatDto } from './dto/find-chat.dto';
import {
FindAllChatDto,
FindAllInMinuteChatDto,
FindChatDto,
} from './dto/find-chat.dto';

@Injectable()
export class ChatService {
constructor(@InjectModel(Chat.name) private chatModel: Model<Chat>) {}

async create(dto: CreateChatDto): Promise<Chat> {
return await this.chatModel.create(dto);
const result = await this.chatModel.create(dto);
console.log('create', result);

return result;
}

async findAll(dto: FindAllChatDto): Promise<Chat[]> {
const oneMinute = new Date(Date.now() - 60 * 1000);
const result = await this.chatModel.find({
senderId: dto.senderId,
receiverId: dto.receiverId,
});
console.log('findAll', result);

return await this.chatModel.find({
return result;
}

async findAllInMinute(dto: FindAllInMinuteChatDto): Promise<Chat[]> {
const oneMinuteAgo = new Date(
new Date(dto.createdAt).getTime() - 60 * 1000,
);

const result = await this.chatModel.find({
senderId: dto.senderId,
receiverId: dto.receiverId,
createdAt: { $gte: oneMinute },
createdAt: {
$gte: oneMinuteAgo,
$lte: dto.createdAt,
},
});
console.log('findAllInMinute', result);

return result;
}

async findOne(dto: FindChatDto): Promise<Chat> {
return await this.chatModel
const result = await this.chatModel
.findOne({
senderId: dto.senderId,
receiverId: dto.receiverId,
messageId: dto.messageId,
})
.exec();
console.log('findOne', result);

return result;
}

/*
Expand Down
6 changes: 3 additions & 3 deletions src/chat/dto/create-chat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsDate, IsString } from 'class-validator';
import { IsDateString, IsString } from 'class-validator';

export class CreateChatDto {
@IsString()
Expand All @@ -13,9 +13,9 @@ export class CreateChatDto {
@IsString()
message: string;

@IsDate()
@IsDateString()
createdAt: Date;

@IsDate()
@IsDateString()
updatedAt: Date;
}
18 changes: 13 additions & 5 deletions src/chat/dto/find-chat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { IsDateString, IsString } from 'class-validator';

export class FindChatDto {
export class FindAllInMinuteChatDto {
@IsString()
senderId: string;

@IsString()
receiverId: string;

@IsString()
messageId: string;
@IsDateString()
createdAt: Date;
}

export class FindAllChatDto {
Expand All @@ -17,7 +17,15 @@ export class FindAllChatDto {

@IsString()
receiverId: string;
}

@IsDateString()
createdAt: Date;
export class FindChatDto {
@IsString()
senderId: string;

@IsString()
receiverId: string;

@IsString()
messageId: string;
}