87 lines
2.3 KiB
TypeScript
87 lines
2.3 KiB
TypeScript
import { Body, Controller, Delete, Get, Param, Patch, Post, Query, Req, Res, UseGuards } from '@nestjs/common';
|
|
import type { Request } from 'express';
|
|
import type { Response } from 'express';
|
|
import { AuthenticatedUser } from '../auth/auth.types';
|
|
import { ContactsService } from './contacts.service';
|
|
import { CreateContactDto } from '../dto/create-contact.dto';
|
|
import { UpdateContactDto } from '../dto/update-contact.dto';
|
|
import { AuthGuard } from '../common/auth.guard';
|
|
|
|
@UseGuards(AuthGuard)
|
|
@Controller('contacts')
|
|
export class ContactsController {
|
|
constructor(private readonly contactsService: ContactsService) {}
|
|
|
|
@Get()
|
|
findAll(
|
|
@Query('page') page?: string,
|
|
@Query('limit') limit?: string,
|
|
@Query('search') search?: string,
|
|
@Query('status') status?: string,
|
|
@Query('tag') tag?: string,
|
|
) {
|
|
return this.contactsService.findAll({
|
|
page: Number(page || '1'),
|
|
limit: Number(limit || '10'),
|
|
search,
|
|
status,
|
|
tag,
|
|
});
|
|
}
|
|
|
|
@Get('export')
|
|
async exportContacts(
|
|
@Req() request: Request & { user: AuthenticatedUser },
|
|
@Res() response: Response,
|
|
@Query('search') search?: string,
|
|
@Query('status') status?: string,
|
|
@Query('tag') tag?: string,
|
|
) {
|
|
const result = await this.contactsService.exportContacts(
|
|
{
|
|
page: 1,
|
|
limit: 100000,
|
|
search,
|
|
status,
|
|
tag,
|
|
},
|
|
request.user,
|
|
request.ip,
|
|
);
|
|
|
|
response.setHeader('Content-Type', result.contentType);
|
|
response.setHeader('Content-Disposition', `attachment; filename="${result.fileName}"`);
|
|
response.send(result.buffer);
|
|
}
|
|
|
|
@Get(':id')
|
|
findOne(@Param('id') id: string) {
|
|
return this.contactsService.findOne(id);
|
|
}
|
|
|
|
@Post()
|
|
create(
|
|
@Req() request: Request & { user: AuthenticatedUser },
|
|
@Body() dto: CreateContactDto,
|
|
) {
|
|
return this.contactsService.create(dto, request.user, request.ip);
|
|
}
|
|
|
|
@Patch(':id')
|
|
update(
|
|
@Req() request: Request & { user: AuthenticatedUser },
|
|
@Param('id') id: string,
|
|
@Body() dto: UpdateContactDto,
|
|
) {
|
|
return this.contactsService.update(id, dto, request.user, request.ip);
|
|
}
|
|
|
|
@Delete(':id')
|
|
remove(
|
|
@Req() request: Request & { user: AuthenticatedUser },
|
|
@Param('id') id: string,
|
|
) {
|
|
return this.contactsService.remove(id, request.user, request.ip);
|
|
}
|
|
}
|