51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
HttpCode,
|
|
Param,
|
|
Post,
|
|
Query,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { AuthGuard } from '../auth/auth.guard';
|
|
import { CurrentUser } from '../auth/current-user.decorator';
|
|
import { parseLimit, rawCursorParam } from '../common/dto/pagination-query.dto';
|
|
import { User } from '../common/domain';
|
|
import { CreateMessageDto } from './dto/create-message.dto';
|
|
import {
|
|
MessagePageResponse,
|
|
MessageResponse,
|
|
toMessageResponse,
|
|
} from './dto/message.response';
|
|
import { MessagesService } from './messages.service';
|
|
|
|
@Controller('api/v1/chats/:chatId/messages')
|
|
@UseGuards(AuthGuard)
|
|
export class MessagesController {
|
|
constructor(private readonly messages: MessagesService) {}
|
|
|
|
@Post()
|
|
@HttpCode(201)
|
|
async create(
|
|
@Param('chatId') chatId: string,
|
|
@Body() dto: CreateMessageDto,
|
|
@CurrentUser() user: User,
|
|
): Promise<MessageResponse> {
|
|
const message = await this.messages.create(chatId, user.id, dto.text);
|
|
return toMessageResponse(message);
|
|
}
|
|
|
|
@Get()
|
|
async list(
|
|
@Param('chatId') chatId: string,
|
|
@CurrentUser() user: User,
|
|
@Query('limit') limitRaw: unknown,
|
|
@Query('cursor') cursorRaw: unknown,
|
|
): Promise<MessagePageResponse> {
|
|
const limit = parseLimit(limitRaw);
|
|
const cursor = rawCursorParam(cursorRaw);
|
|
return this.messages.list(chatId, user.id, limit, cursor);
|
|
}
|
|
}
|