Template
105 lines
2.7 KiB
TypeScript
105 lines
2.7 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
HttpException,
|
|
HttpStatus,
|
|
Param,
|
|
ParseIntPipe,
|
|
Post,
|
|
Put,
|
|
Sse,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
|
|
import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
import { AuthGuard } from 'src/users/guards/auth.guard';
|
|
|
|
import { fromEvent, map, Observable } from 'rxjs';
|
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
|
|
|
import { ConceptService } from './concept.service';
|
|
import { Concept } from './entities/concept.entity';
|
|
import { ConceptPipe } from './concept.pipe';
|
|
import { ConceptRecieveDTO } from './dto/conceptrecieve.dto';
|
|
import { ConceptResponseDTO } from 'events/dto/conceptresponse.dto';
|
|
|
|
@ApiTags('concepts')
|
|
@Controller('concepts')
|
|
export class ConceptController {
|
|
constructor(
|
|
private readonly conceptService: ConceptService,
|
|
private eventEmitter: EventEmitter2,
|
|
) {}
|
|
|
|
@Sse('/subscribe')
|
|
subscribe(): Observable<{ data: string }> {
|
|
return fromEvent(this.eventEmitter, this.conceptService.sse_prefix).pipe(
|
|
map((payload) => {
|
|
return {
|
|
data: JSON.stringify(payload),
|
|
};
|
|
}),
|
|
);
|
|
}
|
|
|
|
@Delete(':id')
|
|
@ApiOperation({ summary: 'Delete a concept' })
|
|
@UseGuards(AuthGuard)
|
|
async delete(@Param('id') id: number) {
|
|
const concept = await this.conceptService.getById(id);
|
|
if (!concept) {
|
|
throw new HttpException('Concept not found', HttpStatus.NOT_FOUND);
|
|
}
|
|
await this.conceptService.delete(concept);
|
|
}
|
|
|
|
@Post('/')
|
|
@ApiOperation({ summary: 'Submit a concept' })
|
|
@ApiBody({ type: ConceptRecieveDTO })
|
|
@UseGuards(AuthGuard)
|
|
async create(@Body('value') value: string) {
|
|
const concept = await this.conceptService.create(value, 'placeholder.jpg');
|
|
return new ConceptResponseDTO(concept);
|
|
}
|
|
|
|
@Get('/')
|
|
@ApiOperation({ summary: 'List enabled concept' })
|
|
async read() {
|
|
return (await this.conceptService.listActive()).map(
|
|
(w) => new ConceptResponseDTO(w),
|
|
);
|
|
}
|
|
|
|
@Get('/all')
|
|
@ApiOperation({ summary: 'List all concepts' })
|
|
async readAll() {
|
|
return (await this.conceptService.list()).map(
|
|
(w) => new ConceptResponseDTO(w),
|
|
);
|
|
}
|
|
|
|
@Put(':id/enable')
|
|
@ApiOperation({ summary: 'Enable a concept' })
|
|
@UseGuards(AuthGuard)
|
|
async enable(
|
|
@Param('id', ParseIntPipe) _id: string,
|
|
@Param('id', ConceptPipe) concept: Concept,
|
|
) {
|
|
await this.conceptService.enable(concept);
|
|
return new ConceptResponseDTO(concept);
|
|
}
|
|
|
|
@Put(':id/disable')
|
|
@ApiOperation({ summary: 'Disable a word' })
|
|
@UseGuards(AuthGuard)
|
|
async disable(
|
|
@Param('id', ParseIntPipe) _id: string,
|
|
@Param('id', ConceptPipe) concept: Concept,
|
|
) {
|
|
await this.conceptService.disable(concept);
|
|
return new ConceptResponseDTO(concept);
|
|
}
|
|
}
|