66 lines
2.3 KiB
TypeScript
66 lines
2.3 KiB
TypeScript
import { Controller, Post, Body, Get, UseGuards, Patch, Param, Delete } from '@nestjs/common';
|
|
import { AdminService } from '../providers/admin.service';
|
|
import { CreateAdminDto } from '../dto/create-admin.dto';
|
|
import { UpdateAdminDto } from '../dto/update-admin.dto';
|
|
import { ApiTags, ApiOperation, ApiBody, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
|
|
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
|
import { AdminId } from 'src/common/decorators/admin-id.decorator';
|
|
import { Admin } from '../entities/admin.entity';
|
|
import { PermissionEnum } from 'src/common/enums/permission.enum';
|
|
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
|
|
|
// TODO : if admin deleted then recreated it must be doable
|
|
@Controller()
|
|
@ApiTags('admin')
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiBearerAuth()
|
|
@Permissions(PermissionEnum.MANAGE_ADMINS)
|
|
export class AdminController {
|
|
constructor(private readonly adminService: AdminService) { }
|
|
|
|
@Post('admin/admin')
|
|
@ApiOperation({ summary: 'Create a new admin' })
|
|
async create(@Body() dto: CreateAdminDto,) {
|
|
const admin = await this.adminService.create(dto);
|
|
return admin;
|
|
}
|
|
|
|
@Get('admin/admins')
|
|
@ApiOperation({ summary: 'admin' })
|
|
async getAll() {
|
|
const admin = await this.adminService.findAll();
|
|
return admin;
|
|
}
|
|
|
|
@Get('admin/admins/me')
|
|
@ApiOperation({ summary: 'admin' })
|
|
async getMe(@AdminId() adminId: string,) {
|
|
const admin = await this.adminService.findById(adminId,);
|
|
return admin;
|
|
}
|
|
|
|
|
|
@Patch('admin/admins/:adminId')
|
|
@ApiOperation({ summary: 'Update an admin' })
|
|
@ApiParam({ name: 'adminId', description: 'Admin ID' })
|
|
@ApiBody({ type: UpdateAdminDto })
|
|
update(@Param('adminId') adminId: string, @Body() dto: UpdateAdminDto,): Promise<Admin> {
|
|
return this.adminService.update(adminId, dto);
|
|
}
|
|
|
|
@Get('admin/admins/:adminId')
|
|
@ApiOperation({ summary: 'Get an admin by ID' })
|
|
@ApiParam({ name: 'id', description: 'Admin ID' })
|
|
getById(@Param('adminId') adminId: string,): Promise<Admin | null> {
|
|
return this.adminService.findById(adminId,);
|
|
}
|
|
|
|
@Delete('admin/admins/:adminId')
|
|
@ApiOperation({ summary: 'Delete an admin by ID' })
|
|
@ApiParam({ name: 'id', description: 'Admin ID' })
|
|
deleteById(@Param('adminId') adminId: string,): Promise<void> {
|
|
return this.adminService.remove(adminId,);
|
|
}
|
|
|
|
}
|