icydns/src/modules/management/management.controller.ts

83 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-11-06 14:19:19 +00:00
import {
Controller,
Delete,
Get,
HttpException,
Param,
Post,
Put,
Query,
UseGuards,
} from '@nestjs/common';
import { ApiExcludeController } from '@nestjs/swagger';
2022-11-12 10:14:46 +00:00
import { ManagementGuard } from 'src/guards/management.guard';
import { DatabaseService } from '../objects/database/database.service';
import { ZoneEntity } from '../objects/database/zone.entity';
2022-11-06 14:19:19 +00:00
@ApiExcludeController()
2022-11-12 10:14:46 +00:00
@UseGuards(ManagementGuard)
2022-11-06 14:19:19 +00:00
@Controller({
2022-11-12 10:14:46 +00:00
path: 'api/v1/management',
2022-11-06 14:19:19 +00:00
})
2022-11-12 10:14:46 +00:00
export class ManagementController {
constructor(private service: DatabaseService) {}
2022-11-06 14:19:19 +00:00
@Get('zones')
async getZoneList(@Query('uuid') uuid?: string) {
let list: ZoneEntity[] = [];
if (uuid) {
list = await this.service.getZonesByIcynetUUID(uuid);
} else {
list = await this.service.getAllZones();
}
return list.map(({ zone }) => zone);
}
@Get('access')
async getAccessList() {
return this.service.getZonesWithIcynet();
}
@Put('zone/:domain/:uuid')
async addZoneAccess(
@Param('uuid') uuid: string,
@Param('domain') domain: string,
) {
const success = await this.service.authorizeIcynetUser(uuid, domain);
return { success };
}
@Delete('zone/:domain/:uuid')
async removeZoneAccess(
@Param('uuid') uuid: string,
@Param('domain') domain: string,
) {
const success = await this.service.revokeIcynetUser(uuid, domain);
return { success };
}
@Post('zone/:domain/:uuid')
async getAccess(
@Param('uuid') uuid: string,
@Param('domain') domain: string,
) {
const added = await this.service.createIcynetAccessKey(uuid, domain);
if (!added) throw new HttpException('Invalid request', 400);
return { token: added.key };
}
@Put('zone/:domain')
async addZone(@Param('domain') domain: string) {
const added = await this.service.addZone(domain);
return { success: !!added };
}
@Delete('zone/:domain')
async removeZone(@Param('domain') domain: string) {
const success = await this.service.removeZone(domain);
return { success };
}
}