From 49dd1aa14c0cf4a1d3e1f5315baef49cc7a790aa Mon Sep 17 00:00:00 2001 From: Andrii Arsenin Date: Tue, 7 Apr 2026 12:44:08 +0300 Subject: [PATCH] feat(sessions): add GET /sessions and DELETE /sessions/:id endpoints --- src/session/session.controller.ts | 42 +++++++++++++++++++++++++++++++ src/session/session.module.ts | 2 ++ src/session/session.service.ts | 8 ++++++ 3 files changed, 52 insertions(+) create mode 100644 src/session/session.controller.ts diff --git a/src/session/session.controller.ts b/src/session/session.controller.ts new file mode 100644 index 0000000..e8aa461 --- /dev/null +++ b/src/session/session.controller.ts @@ -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 { + 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); + } +} diff --git a/src/session/session.module.ts b/src/session/session.module.ts index 92fb086..c5bcaa0 100644 --- a/src/session/session.module.ts +++ b/src/session/session.module.ts @@ -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], }) diff --git a/src/session/session.service.ts b/src/session/session.service.ts index 94f3f16..637c687 100644 --- a/src/session/session.service.ts +++ b/src/session/session.service.ts @@ -39,4 +39,12 @@ export class SessionService { findBySessionName(sessionName: string): Promise { return this.repo.findOneBy({ sessionName }); } + + findAll(): Promise[]> { + return this.repo.find({ select: ['id', 'sessionName', 'createdAt', 'updatedAt'] }); + } + + async remove(id: number): Promise { + await this.repo.delete(id); + } }