wip entities

This commit is contained in:
simplonco
2023-01-05 14:39:52 +01:00
parent 286d79ed06
commit f0736ab20b
11 changed files with 248 additions and 99 deletions

View File

@@ -1,11 +1,54 @@
import { Controller, Post, Body } from '@nestjs/common';
import { Controller, UseGuards, Get, Post, Body, Req } from '@nestjs/common';
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
import { ChatService } from './chat.service';
@Controller('/chat/create')
@Controller('chat')
export class ChatController {
@Post()
async handleSubmit(@Body() formData: any) {
console.log("------ create:");
console.log(formData);
constructor(
private chatService: ChatService,
) {}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('rooms')
async get_rooms() {
const rooms = await this.chatService.getRooms();
return { rooms };
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('join')
async join_room(@Body() body) {
console.log("------ create :");
console.log(typeof body);
console.log(body);
console.log(body.room_name);
const { room_name } = body;
console.log("room_name:");
console.log(room_name);
const { room_id } = body;
// get user
let user;
await this.chatService.addUserToRoom(user, room_id);
//return { message: 'Successfully joined room.' };
return body;
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('leave')
async leaveRoom(@Body() body) {
const { room_id } = body;
// get user
let user;
await this.chatService.removeUserFromRoom(user, room_id);
return { message: 'Successfully left room.' };
}
}

View File

@@ -52,6 +52,5 @@ export class ChatGateway
client.local.emit('message', client.username, message);
// this.chatService.add_message(this.server, message);
}
}

View File

@@ -4,8 +4,12 @@ import { ChatService } from './chat.service';
import { ChatGateway } from './chat.gateway';
import { UsersModule } from 'src/users/users.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Chatroom } from './entities/chat.entity';
@Module({
imports: [
TypeOrmModule.forFeature([Chatroom]),
UsersModule,
],
controllers: [

View File

@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { User } from 'src/users/entities/user.entity';
//import { InjectRepository } from '@nestjs/typeorm';
//import { User } from 'src/users/entities/user.entity';
//import { Repository } from 'typeorm';
@Injectable()
@@ -12,7 +12,28 @@ export class ChatService {
// private readonly userRepository: Repository<User>,
) { }
async add_message(server, message) {
return server.emit('message', message);
//async add_message(server, message) {
// return server.emit('message', message);
//}
async getRooms()
{
// get rooms
// return rooms;
}
async addUserToRoom(user: User, room_id: string)
{
// get room
//if !room
// create room
// add user to room
}
async removeUserFromRoom(user: User, room_id: string)
{
// get room
// remove user
}
}

View File

@@ -0,0 +1,26 @@
import {
Entity,
Column,
ManyToOne,
PrimaryGeneratedColumn
} from "typeorm";
import { User } from 'src/users/entities/user.entity';
@Entity('chatroom')
export class Chatroom {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
type: 'public' | 'private' | 'direct';
@Column({ nullable: true })
protection: boolean;
@ManyToOne(type => User, user => user.ownedRoom)
owner: User;
}

View File

@@ -3,6 +3,7 @@ import { IsEmail, Length } from "class-validator";
import { Column, Entity, JoinColumn, JoinTable, ManyToMany, OneToMany, OneToOne, PrimaryGeneratedColumn, Unique } from "typeorm";
import { Friendship } from "../../friendship/entities/friendship.entity";
import { UserStats } from "./userStat.entities";
import { Chatroom } from "src/chat/entities/chat.entity";
@Entity('user')
@@ -49,6 +50,9 @@ export class User {
@OneToMany(type => Friendship , (friendship) => friendship.receiver)
receivedFriendRequest: Friendship[];
@OneToMany(type => Chatroom, chatroom => chatroom.owner)
ownedRoom: Chatroom[];
@JoinColumn()
@OneToOne(() => UserStats, { cascade: true })
stats: UserStats;