58 lines
1.4 KiB
TypeScript
58 lines
1.4 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 { ChatsService } from './chats.service';
|
|
import {
|
|
ChatPageResponse,
|
|
ChatResponse,
|
|
toChatResponse,
|
|
} from './dto/chat.response';
|
|
import { CreateChatDto } from './dto/create-chat.dto';
|
|
|
|
@Controller('api/v1/chats')
|
|
@UseGuards(AuthGuard)
|
|
export class ChatsController {
|
|
constructor(private readonly chats: ChatsService) {}
|
|
|
|
@Post()
|
|
@HttpCode(201)
|
|
async create(
|
|
@Body() dto: CreateChatDto,
|
|
@CurrentUser() user: User,
|
|
): Promise<ChatResponse> {
|
|
const chat = await this.chats.create(dto.title, dto.member_ids, user.id);
|
|
return toChatResponse(chat);
|
|
}
|
|
|
|
@Get()
|
|
async list(
|
|
@CurrentUser() user: User,
|
|
@Query('limit') limitRaw: unknown,
|
|
@Query('cursor') cursorRaw: unknown,
|
|
): Promise<ChatPageResponse> {
|
|
const limit = parseLimit(limitRaw);
|
|
const cursor = rawCursorParam(cursorRaw);
|
|
return this.chats.listForMember(user.id, limit, cursor);
|
|
}
|
|
|
|
@Get(':chatId')
|
|
async getOne(
|
|
@Param('chatId') chatId: string,
|
|
@CurrentUser() user: User,
|
|
): Promise<ChatResponse> {
|
|
const chat = await this.chats.getForMember(chatId, user.id);
|
|
return toChatResponse(chat);
|
|
}
|
|
}
|