123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import {
- Controller,
- Get,
- Delete,
- Query,
- HttpException,
- HttpStatus,
- } from '@nestjs/common';
- import { ApiQuery, ApiResponse } from '@nestjs/swagger';
- import { LogEntryDto } from './dto/logentry.dto';
- import { LoggingService } from './logging.service';
- import { LogEntry } from './models/logentry.model';
- @Controller('logging')
- export class LoggingController {
- constructor(private readonly loggingService: LoggingService) {}
- private static logEntryToDto(entry: LogEntry): LogEntryDto {
- return {
- component: entry.component,
- level: entry.level,
- timestamp: entry.timestamp,
- message: entry.message,
- };
- }
- @Get('/all')
- @ApiResponse({
- status: 201,
- description: 'All log entries',
- type: [LogEntryDto],
- })
- @ApiResponse({
- status: 404,
- description: 'No messages found',
- })
- async allMessages(): Promise<LogEntryDto[]> {
- return this.loggingService.allMessages().then((response) => {
- if (response === null) {
- throw new HttpException(
- 'Unable to find the log messages',
- HttpStatus.NOT_FOUND,
- );
- }
- const messages: LogEntryDto[] = [];
- response.forEach((entry) =>
- messages.push(LoggingController.logEntryToDto(entry)),
- );
- return messages;
- });
- }
- @Get('/component')
- @ApiQuery({
- name: 'component',
- description: 'The component filter for the messages',
- type: String,
- })
- @ApiResponse({
- status: 201,
- description: 'All log entries',
- type: [LogEntryDto],
- })
- @ApiResponse({
- status: 404,
- description: 'No messages found',
- })
- async componentMessages(
- @Query('component') component: string,
- ): Promise<LogEntryDto[]> {
- return this.loggingService.componentMessages(component).then((response) => {
- if (response === null) {
- throw new HttpException(
- 'Unable to find the log messages',
- HttpStatus.NOT_FOUND,
- );
- }
- const messages: LogEntryDto[] = [];
- response.forEach((entry) =>
- messages.push(LoggingController.logEntryToDto(entry)),
- );
- return messages;
- });
- }
- @Delete('/deleteAll')
- @ApiResponse({
- status: 200,
- description: 'All log entries are deleted',
- })
- async deleteAll(): Promise<void> {
- return this.loggingService.deleteAllMessages();
- }
- @Delete('/deleteComponent')
- @ApiQuery({
- name: 'component',
- description: 'The component filter for the messages',
- type: String,
- })
- @ApiResponse({
- status: 200,
- description: 'All log entries are deleted',
- })
- async deleteComponent(@Query('component') component: string): Promise<void> {
- return this.loggingService.deleteComponentMessages(component);
- }
- }
|