added promise return type in service

This commit is contained in:
simplonco
2023-01-10 17:11:48 +01:00
parent 8f07be709e
commit 4af3654e58
5 changed files with 47 additions and 19 deletions

View File

@@ -3,7 +3,6 @@ import { ChatController } from './chat.controller';
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/chatroom.entity';
import { User } from 'src/users/entities/user.entity';

View File

@@ -18,7 +18,7 @@ export class ChatService {
private readonly userRepository: Repository<User>,
@InjectRepository(Chatroom)
private readonly chatroomRepository: Repository<Chatroom>,
) { }
) {}
// temp for test
@@ -30,7 +30,7 @@ export class ChatService {
/* GETTERS ************************************************
*/
async getMyRooms(username: string)
async getMyRooms(username: string): Promise<Chatroom[]>
{
console.log("-- in getMyRooms service");
const rooms = await this.chatroomRepository
@@ -42,7 +42,17 @@ export class ChatService {
return rooms;
}
async getAllRooms()
async getMyDirects(username: string): Promise<Chatroom[]>
{
console.log("-- in getAllNotMyRooms service");
const my_rooms = await this.getMyRooms(username);
const directs = my_rooms;
console.log("-- out getAllNotMyRooms service");
return directs;
}
async getAllRooms(): Promise<Chatroom[]>
{
console.log("-- in getAllRooms service");
const rooms = await this.chatroomRepository
@@ -53,7 +63,22 @@ export class ChatService {
return rooms;
}
async getAllNotMyRooms(username: string)
async getAllNotMyRooms(username: string): Promise<Chatroom[]>
{
console.log("-- in getAllNotMyRooms service");
const user_db = await this.getUserByName(username);
const rooms = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.type != :type', { type: 'private' })
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` })
.getMany();
console.log("-- out getAllNotMyRooms service");
return rooms;
}
/*
async getAllUsersNotRooms(username: string)
{
console.log("-- in getAllNotMyRooms service");
const user_db = await this.getUserByName(username);
@@ -69,8 +94,9 @@ export class ChatService {
console.log("-- out getAllNotMyRooms service");
return rooms;
}
*/
async getMessagesFromCurrentRoom(username: string)
async getMessagesFromCurrentRoom(username: string): Promise<messagesDto[]>
{
console.log("-- in getMessagesFromCurrentRoom service");
const user_db = await this.getUserByName(username);
@@ -81,7 +107,7 @@ export class ChatService {
return currentRoom.messages;
}
async getCurrentRoomName(username: string)
async getCurrentRoomName(username: string): Promise<string>
{
console.log("-- in getCurrentRoomName service");
console.log('username:', username);
@@ -92,7 +118,7 @@ export class ChatService {
return user_db.currentRoom;
}
async getRoomByName(room_name: string)
async getRoomByName(room_name: string): Promise<Chatroom>
{
console.log("-- in getRoomByName service");
const room = await this.chatroomRepository
@@ -104,7 +130,7 @@ export class ChatService {
return room;
}
async getRoomById(id: number)
async getRoomById(id: number): Promise<Chatroom>
{
console.log("-- in getRoomById service");
const room = await this.chatroomRepository
@@ -120,7 +146,7 @@ export class ChatService {
/* SETTERS ************************************************
*/
async setCurrentRoom(username: string, room_name: string)
async setCurrentRoom(username: string, room_name: string): Promise<string>
{
console.log("-- in setCurrentRoom service");
const user_db = await this.getUserByName(username);
@@ -136,7 +162,7 @@ export class ChatService {
/* ADDERS *************************************************
*/
async addUserToNewRoom(username: string, createRoomDto: createRoomDto)
async addUserToNewRoom(username: string, createRoomDto: createRoomDto): Promise<string>
{
console.log("-- in addUserToNewRoom service");
const room = await this.getRoomByName(createRoomDto.room_name);
@@ -156,7 +182,7 @@ export class ChatService {
return "successfull room creation";
}
async addUserToRoom(username: string, room_name: string)
async addUserToRoom(username: string, room_name: string): Promise<string>
{
console.log("-- in addUserToRoom service");
const room = await this.getRoomByName(room_name);
@@ -173,7 +199,7 @@ export class ChatService {
return "successfully joining room";
}
async addMessageToRoom(room_name: string, username: string, message: string)
async addMessageToRoom(room_name: string, username: string, message: string): Promise<void>
{
console.log("-- in addMessageToRoom service");
//const user_db = await this.getUserByName(username);
@@ -194,7 +220,7 @@ export class ChatService {
/* REMOVERS ***********************************************
*/
async removeUserFromRoom(username: string, room_name: string)
async removeUserFromRoom(username: string, room_name: string): Promise<string>
{
console.log("-- in removeUserFromRoom service");
const room = await this.getRoomByName(room_name);
@@ -217,7 +243,7 @@ export class ChatService {
/* SEARCH IN USER *****************************************
*/
async getUserByName(username: string)
async getUserByName(username: string): Promise<User>
{
console.log("-- in getUserByName service");
const user = await this.userRepository

View File

@@ -3,7 +3,10 @@ import { IsNull } from "typeorm";
export class messagesDto
{
@IsArray()
messages: { name: string; message: string }[];
@IsString()
name: string;
@IsString()
message: string;
}

View File

@@ -7,6 +7,7 @@ import {
PrimaryGeneratedColumn
} from "typeorm";
import { User } from 'src/users/entities/user.entity';
import { messagesDto } from '../dto/messages.dto';
@Entity('chatroom')
export class Chatroom {
@@ -33,6 +34,6 @@ export class Chatroom {
users: string[]; // usernames
@Column("json")
messages: { name: string, message: string }[];
messages: messagesDto[];
}

View File

@@ -45,7 +45,6 @@ export class UsersService {
isEnabledTwoFactorAuth: user.isEnabledTwoFactorAuth,
status: user.status,
stats: user.stats,
currentRoom: user.currentRoom,
};
console.log(`Returned Partial User.` + partialUser.username + user.username);
return partialUser;