78 lines
2.1 KiB
TypeScript
78 lines
2.1 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Param,
|
|
ParseUUIDPipe,
|
|
Patch,
|
|
Post,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
|
|
import { createScheduleSchema, updateScheduleSchema } from '@hexahost/contracts';
|
|
|
|
import { CurrentUser } from '../../auth/decorators/current-user.decorator';
|
|
import { SessionGuard } from '../../auth/guards/session.guard';
|
|
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
|
|
|
|
import { SchedulesService } from './schedules.service';
|
|
|
|
@ApiTags('servers')
|
|
@Controller('servers/:serverId/schedules')
|
|
@UseGuards(SessionGuard)
|
|
export class SchedulesController {
|
|
constructor(private readonly schedulesService: SchedulesService) {}
|
|
|
|
@Get()
|
|
@ApiOperation({ summary: 'List server schedules' })
|
|
list(
|
|
@CurrentUser() user: { id: string },
|
|
@Param('serverId', ParseUUIDPipe) serverId: string,
|
|
) {
|
|
return this.schedulesService.list(user.id, serverId);
|
|
}
|
|
|
|
@Post()
|
|
@HttpCode(HttpStatus.CREATED)
|
|
create(
|
|
@CurrentUser() user: { id: string },
|
|
@Param('serverId', ParseUUIDPipe) serverId: string,
|
|
@Body(new ZodValidationPipe(createScheduleSchema)) body: unknown,
|
|
) {
|
|
return this.schedulesService.create(
|
|
user.id,
|
|
serverId,
|
|
body as Parameters<SchedulesService['create']>[2],
|
|
);
|
|
}
|
|
|
|
@Patch(':scheduleId')
|
|
update(
|
|
@CurrentUser() user: { id: string },
|
|
@Param('serverId', ParseUUIDPipe) serverId: string,
|
|
@Param('scheduleId', ParseUUIDPipe) scheduleId: string,
|
|
@Body(new ZodValidationPipe(updateScheduleSchema)) body: unknown,
|
|
) {
|
|
return this.schedulesService.update(
|
|
user.id,
|
|
serverId,
|
|
scheduleId,
|
|
body as Parameters<SchedulesService['update']>[3],
|
|
);
|
|
}
|
|
|
|
@Delete(':scheduleId')
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
|
async remove(
|
|
@CurrentUser() user: { id: string },
|
|
@Param('serverId', ParseUUIDPipe) serverId: string,
|
|
@Param('scheduleId', ParseUUIDPipe) scheduleId: string,
|
|
) {
|
|
await this.schedulesService.remove(user.id, serverId, scheduleId);
|
|
}
|
|
}
|