feat(sessions): add GET /sessions and DELETE /sessions/:id endpoints
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { Controller, Delete, Get, NotFoundException, Param, ParseIntPipe } from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { SessionService } from './session.service';
|
||||
|
||||
@ApiTags('sessions')
|
||||
@Controller('sessions')
|
||||
export class SessionController {
|
||||
constructor(private readonly sessionService: SessionService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all stored sessions' })
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'Array of sessions',
|
||||
schema: {
|
||||
type: 'array',
|
||||
items: {
|
||||
properties: {
|
||||
id: { type: 'number' },
|
||||
sessionName: { type: 'string' },
|
||||
createdAt: { type: 'string', format: 'date-time' },
|
||||
updatedAt: { type: 'string', format: 'date-time' },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
findAll() {
|
||||
return this.sessionService.findAll();
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: 'Delete a session by ID' })
|
||||
@ApiResponse({ status: 200, description: 'Session deleted' })
|
||||
@ApiResponse({ status: 404, description: 'Session not found' })
|
||||
async remove(@Param('id', ParseIntPipe) id: number): Promise<void> {
|
||||
const sessions = await this.sessionService.findAll();
|
||||
if (!sessions.find(s => s.id === id)) {
|
||||
throw new NotFoundException(`Session ${id} not found`);
|
||||
}
|
||||
await this.sessionService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,11 @@ import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SessionEntity } from './session.entity';
|
||||
import { SessionService } from './session.service';
|
||||
import { SessionController } from './session.controller';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([SessionEntity])],
|
||||
controllers: [SessionController],
|
||||
providers: [SessionService],
|
||||
exports: [SessionService],
|
||||
})
|
||||
|
||||
@@ -39,4 +39,12 @@ export class SessionService {
|
||||
findBySessionName(sessionName: string): Promise<SessionEntity | null> {
|
||||
return this.repo.findOneBy({ sessionName });
|
||||
}
|
||||
|
||||
findAll(): Promise<Pick<SessionEntity, 'id' | 'sessionName' | 'createdAt' | 'updatedAt'>[]> {
|
||||
return this.repo.find({ select: ['id', 'sessionName', 'createdAt', 'updatedAt'] });
|
||||
}
|
||||
|
||||
async remove(id: number): Promise<void> {
|
||||
await this.repo.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user