Skip to content

Commit

Permalink
Merge pull request #137 from p0nch0d3v/133-access-to-room-by-name
Browse files Browse the repository at this point in the history
room service refactor
  • Loading branch information
p0nch0d3v authored Oct 7, 2024
2 parents 5ad2873 + c30370f commit d7d73aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
50 changes: 16 additions & 34 deletions apps/api/src/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,10 @@ export class RoomService {
else {
return { success: false, id: null, error: 'There is already a room with the same name' };;
}

}

async exists(roomDto: JoinRoomDTO): Promise<boolean> {
let lookupById = false;
if (this.validateUUID(roomDto.id)) {
lookupById = true;
}

let savedRoom = null;
if (lookupById === true) {
savedRoom = await this.roomsRepository.findOne({
where: [
{ id: roomDto.id }
]
});
}
else {
const queryResult = await (await this.query()).where('LOWER(name) = LOWER(:name)', { name: roomDto.id }).getRawOne();
if (queryResult !== undefined && queryResult !== null) {
savedRoom = new RoomDTO(queryResult.room_id, queryResult.room_name, queryResult.room_admin, queryResult.room_serie, queryResult.room_values, queryResult.room_created_at, queryResult.room_hasPassword);
}
}
const savedRoom = await this.get(roomDto.id);

if (savedRoom !== undefined && savedRoom !== null) {
if (savedRoom.password !== undefined && savedRoom.password !== null && roomDto.password !== undefined && roomDto.password !== null) {
Expand All @@ -69,12 +50,7 @@ export class RoomService {
return false;
}

return await this.roomsRepository.exists({
where: {
id: id,
password: Not(IsNull())
}
});
return (await this.get(id)).hasPassword
}

async getByUniqueId(id: string): Promise<RoomDTO> {
Expand All @@ -94,24 +70,30 @@ export class RoomService {
savedRoom.serie,
savedRoom.values,
savedRoom.created_at,
savedRoom.password,
savedRoom.password !== undefined && savedRoom.password !== null && savedRoom.password.length > 0)
: null;
}

async getByName(name: string): Promise<RoomDTO> {
const queryResult = await (await this.query()).where('LOWER(name) = LOWER(:name)', { name: name }).getRawOne();
if (queryResult !== undefined && queryResult !== null) {
return new RoomDTO(queryResult.room_id, queryResult.room_name, queryResult.room_admin, queryResult.room_serie, queryResult.room_values, queryResult.room_created_at, queryResult.room_hasPassword);
}
return null;
}

async get(id: string): Promise<RoomDTO> {
let lookupById = false;
if (this.validateUUID(id)) {
lookupById = true;
}
let savedRoom = null;
if (lookupById === true) {
savedRoom = await this.roomsRepository.findOne({
where: { id: id }
});
savedRoom = await this.getByUniqueId(id);
}
else {
const queryResult = await (await this.query()).where('LOWER(name) = LOWER(:name)', { name: id }).getRawOne();
savedRoom = new RoomDTO(queryResult.room_id, queryResult.room_name, queryResult.room_admin, queryResult.room_serie, queryResult.room_values, queryResult.room_created_at, queryResult.room_hasPassword);
savedRoom = await this.getByName(id);
}
return savedRoom;
}
Expand All @@ -120,7 +102,7 @@ export class RoomService {
const rooms = await (await this.query()).getRawMany();
const allRooms = [];
rooms.forEach(room => {
allRooms.push(new RoomDTO(room.room_id, room.room_name, room.room_admin, room.room_serie, room.room_values, room.room_created_at, room.room_hasPassword));
allRooms.push(new RoomDTO(room.room_id, room.room_name, room.room_admin, room.room_serie, room.room_values, room.room_created_at, room.room_password, room.room_hasPassword));
});

return allRooms;
Expand All @@ -138,7 +120,7 @@ export class RoomService {
const rooms = await (await this.query()).take(10).getRawMany();
const allRooms = [];
rooms.forEach(room => {
allRooms.push(new RoomDTO(room.room_id, room.room_name, room.room_admin, room.room_cards, room.room_created_at, room.room_hasPassword));
allRooms.push(new RoomDTO(room.room_id, room.room_name, room.room_admin, room.room_cards, room.room_created_at, room.room_password, room.room_hasPassword));
});
return allRooms;
}
Expand All @@ -157,7 +139,7 @@ export class RoomService {
private async query(): Promise<SelectQueryBuilder<Room>> {
return await this.roomsRepository
.createQueryBuilder('room')
.select(['room.id', 'room.name', 'room.admin', 'room.serie', 'room.values', 'room.cards', 'room.created_at'])
.select(['room.id', 'room.name', 'room.admin', 'room.serie', 'room.values', 'room.cards', 'room.created_at', 'room.password'])
.addSelect("password is not NULL", "room_hasPassword")
.orderBy("created_at", "DESC");
}
Expand Down
4 changes: 3 additions & 1 deletion apps/models/DTO/room.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

export class RoomDTO {
constructor(id: string | undefined, name: string | undefined, admin: string | undefined, serie: string | undefined, values: string | undefined, created_at: Date | undefined, hasPassword?: boolean | undefined) {
constructor(id: string | undefined, name: string | undefined, admin: string | undefined, serie: string | undefined, values: string | undefined, created_at: Date | undefined, password: string | undefined, hasPassword?: boolean | undefined) {
this.id = id;
this.name = name;
this.admin = admin;
this.serie = serie;
this.values = values;
this.created_at = created_at;
this.password = password;
this.hasPassword = hasPassword;
}
id: string | undefined;
Expand All @@ -15,6 +16,7 @@ export class RoomDTO {
hide?: boolean| undefined;
serie: string | null | undefined;
values: string | null | undefined;
password: string | null | undefined;
hasPassword?: boolean| undefined;
created_at: Date| undefined
}

0 comments on commit d7d73aa

Please sign in to comment.