Skip to content

Commit

Permalink
Add GET /session/:id
Browse files Browse the repository at this point in the history
  • Loading branch information
projkov committed Aug 23, 2024
1 parent fa0d08c commit 913d8fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/modules/sessions/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Session } from './session.entity';

export interface SessionWithRequests extends Session {
requestsNumber: number;
}

export interface SessionWithBaseUrl extends Session {
baseUrl: string;
}
22 changes: 18 additions & 4 deletions src/modules/sessions/session.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Controller, Post, Body } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { Controller, Post, Body, Get, Param } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiParam } from '@nestjs/swagger';
import { SessionService } from './session.service';
import { CreateSessionDto } from './session.dto';
import { RequestService } from '../requests/request.service';
import { SessionWithBaseUrl, SessionWithRequests } from './interfaces';

@ApiTags('sessions')
@Controller('sessions')
Expand All @@ -14,11 +15,24 @@ export class SessionController {

@Post()
@ApiOperation({ summary: 'Create a new session' })
async create(@Body() createSessionDto: CreateSessionDto): Promise<any> {
// TODO: Remove any
async create(@Body() createSessionDto: CreateSessionDto): Promise<SessionWithBaseUrl> {
const backendUrl = process.env.BACKEND_URL || 'http://localhost:8080';
const sessionEntity = await this.sessionService.create(createSessionDto);

return { ...sessionEntity, baseUrl: `${backendUrl}/app/${sessionEntity.id}` };
}

@Get(':id')
@ApiOperation({ summary: 'Find a session by ID' })
@ApiParam({ name: 'id', description: 'Session ID' })
async findOne(@Param('id') id: string): Promise<SessionWithRequests> {
const currentSession = await this.sessionService.findOne(id);
const sessionRequests = await this.requestService.findAll({
where: { session: { id: id } },
relations: ['session'],
});
const requestsLength = sessionRequests.length;

return { ...currentSession, requestsNumber: requestsLength };
}
}

0 comments on commit 913d8fd

Please sign in to comment.