54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { Body, Controller, Get, Post, Query, Req } from '@nestjs/common';
|
|
import { SessionService } from 'src/service/session.service';
|
|
import { UserService } from 'src/service/user.service';
|
|
|
|
@Controller('/session')
|
|
export class SessionController {
|
|
constructor(
|
|
private readonly userService: UserService,
|
|
private readonly sessionService: SessionService,
|
|
) {}
|
|
|
|
@Post('/create')
|
|
public createSession() {
|
|
this.sessionService.createSession(
|
|
new Date(Date.now() + 24 * 3600 * 1000),
|
|
10,
|
|
);
|
|
}
|
|
|
|
@Get()
|
|
public async getAllSessions(@Req() request) {
|
|
const userId = request.userId;
|
|
const sessions = await this.sessionService.getAllSessions();
|
|
return sessions.map((session) => ({
|
|
id: session.id,
|
|
size: session.size,
|
|
userCount: session.users.length,
|
|
includesRequester: session.users.find((u) => u.id === userId),
|
|
date: session.date,
|
|
status: session.status,
|
|
}));
|
|
}
|
|
|
|
@Post('/join')
|
|
public async joinSession(
|
|
@Req() request,
|
|
@Body('sessionId') sessionId: string,
|
|
) {
|
|
const userId = request.userId;
|
|
const user = await this.userService.getUserById(userId);
|
|
await this.sessionService.joinSession(user, sessionId);
|
|
}
|
|
|
|
@Post('/leave')
|
|
public async leaveSession(
|
|
@Req() request,
|
|
@Body('sessionId') sessionId: string,
|
|
) {
|
|
const userId = request.userId;
|
|
const user = await this.userService.getUserById(userId);
|
|
await this.sessionService.leaveSession(user, sessionId);
|
|
}
|
|
}
|