68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Delete,
|
|
Patch,
|
|
Body,
|
|
Param,
|
|
UseGuards,
|
|
HttpStatus,
|
|
} from '@nestjs/common';
|
|
import { ApiKeysService } from './api-keys.service';
|
|
import { CreateApiKeyDto } from './dto/api-key.dto';
|
|
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
|
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse } from '@nestjs/swagger';
|
|
|
|
@ApiTags('Admin - مدیریت کلیدهای دسترسی (API Keys)')
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Controller('admin/api-keys')
|
|
export class ApiKeysController {
|
|
constructor(private readonly apiKeysService: ApiKeysService) {}
|
|
|
|
@Get()
|
|
@ApiOperation({ summary: 'دریافت لیست کلیدهای دسترسی API' })
|
|
@ApiResponse({ status: HttpStatus.OK, description: 'لیست کلیدها با موفقیت دریافت شد' })
|
|
async findAll() {
|
|
const keys = await this.apiKeysService.findAll();
|
|
return {
|
|
success: true,
|
|
data: keys,
|
|
};
|
|
}
|
|
|
|
@Post()
|
|
@ApiOperation({ summary: 'ایجاد یک API Key جدید' })
|
|
@ApiResponse({ status: HttpStatus.CREATED, description: 'کلید جدید با موفقیت ایجاد شد' })
|
|
async create(@Body() dto: CreateApiKeyDto) {
|
|
const result = await this.apiKeysService.create(dto);
|
|
return {
|
|
success: true,
|
|
message: 'کلید دسترسی با موفقیت ایجاد شد. لطفاً آن را در جای امن ذخیره نمایید.',
|
|
data: result,
|
|
};
|
|
}
|
|
|
|
@Patch(':id/toggle')
|
|
@ApiOperation({ summary: 'تغییر وضعیت فعال/غیرفعال کلید' })
|
|
async toggleStatus(@Param('id') id: string) {
|
|
const updated = await this.apiKeysService.toggleStatus(id);
|
|
return {
|
|
success: true,
|
|
message: 'وضعیت کلید با موفقیت بهروزرسانی شد',
|
|
data: updated,
|
|
};
|
|
}
|
|
|
|
@Delete(':id')
|
|
@ApiOperation({ summary: 'حذف کلید دسترسی' })
|
|
async delete(@Param('id') id: string) {
|
|
await this.apiKeysService.delete(id);
|
|
return {
|
|
success: true,
|
|
message: 'کلید دسترسی با موفقیت حذف شد',
|
|
};
|
|
}
|
|
}
|