Skip to content

Commit 4498de5

Browse files
committed
Dals splitted in two entities: roomRepository and sessionRepository
1 parent 80ddef5 commit 4498de5

11 files changed

Lines changed: 132 additions & 117 deletions

File tree

‎back/src/dals/room/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './room.model';
2-
export * from './room.context';
2+
export * from './room.context';
3+
export * from './repository';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { envConstants } from 'core';
2+
import * as mockRepository from './room.mock';
3+
import * as repository from './room.repository';
4+
import { RoomRepositoryContract } from './room.contract';
5+
6+
export const roomRepository: RoomRepositoryContract = envConstants.isMockRepository
7+
? mockRepository
8+
: repository;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { RoomInfo } from 'dals';
2+
3+
export interface RoomRepositoryContract {
4+
saveRoomInfo: (newRoomInfo: RoomInfo, action: string) => Promise<void>;
5+
isRoomAvailable: (room: string) => Promise<boolean>;
6+
getRoomContent:(room: string) => Promise<string>;
7+
}
8+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import produce from 'immer';
2+
import { InputMessageTypes } from 'messages';
3+
import { RoomInfo, UserSession } from 'dals'
4+
5+
let userCollectionSession: UserSession[] = [];
6+
let roomCollectionSession: RoomInfo[] = [];
7+
8+
export const isRoomAvailable = async (room: string): Promise<boolean> =>
9+
!userCollectionSession.find((session) => session.room === room);
10+
11+
export const saveRoomInfo = async (newRoomInfo: RoomInfo, action: string): Promise<void> => {
12+
const roomIndex: number = await getRoomIndexByName(newRoomInfo);
13+
const content: string = newRoomInfo.content;
14+
roomCollectionSession =
15+
roomIndex === -1
16+
? await insertRoom(newRoomInfo)
17+
: await updateRoomContent(roomIndex, content, action);
18+
};
19+
20+
export const getRoomContent = async (room: string): Promise<string> => {
21+
const roomInfo = roomCollectionSession.find(
22+
(roomInfo: RoomInfo) => roomInfo.room === room
23+
);
24+
25+
return roomInfo ? roomInfo.content : '';
26+
};
27+
28+
const getRoomIndexByName = async (newRoomInfo: RoomInfo): Promise<number> => {
29+
return roomCollectionSession.findIndex(
30+
(roomInfo: RoomInfo) => roomInfo.room === newRoomInfo.room
31+
);
32+
};
33+
34+
const updateRoomContent = async (index: number, content: string, action:string): Promise<RoomInfo[]> => {
35+
return produce(roomCollectionSession, (draftState: RoomInfo[]) => {
36+
switch (action) {
37+
case InputMessageTypes.TRAINER_APPEND_TEXT:
38+
draftState[index].content += '\n' + content;
39+
break;
40+
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
41+
draftState[index].content = content;
42+
break;
43+
};
44+
});
45+
};
46+
47+
const insertRoom = async (newRoomInfo: RoomInfo): Promise<RoomInfo[]> => {
48+
return produce(roomCollectionSession, (draftState: RoomInfo[]) => {
49+
draftState.push(newRoomInfo);
50+
});
51+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { RoomInfo, RoomContext } from 'dals'
2+
import { InputMessageTypes } from 'messages';
3+
4+
export const isRoomAvailable = async (room: string): Promise<boolean> => {
5+
const roomAvailable = !(await RoomContext.exists({ room }));
6+
return roomAvailable;
7+
}
8+
9+
export const saveRoomInfo = async (newRoomInfo: RoomInfo, action: string): Promise<void> => {
10+
await isRoomAvailable(newRoomInfo.room) ? await insertRoom(newRoomInfo) : await updateRoomContent(newRoomInfo, action);
11+
};
12+
13+
export const getRoomContent = async (room: string): Promise<string> => {
14+
const roomInfo: RoomInfo = await getRoomByName(room);
15+
return roomInfo ? roomInfo.content : '';
16+
};
17+
18+
const getRoomByName = async (room: string): Promise<RoomInfo> => {
19+
return (await RoomContext.findOne({room: room}).lean());
20+
};
21+
22+
const updateRoomContent = async (newRoomInfo: RoomInfo, action:string): Promise<void> => {
23+
const room: RoomInfo = await getRoomByName(newRoomInfo.room);
24+
25+
let newContent = "";
26+
27+
if(room) {
28+
newContent = room.content;
29+
}
30+
31+
switch (action) {
32+
case InputMessageTypes.TRAINER_APPEND_TEXT:
33+
newContent += '\n' + newRoomInfo.content;
34+
break;
35+
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
36+
newContent = newRoomInfo.content;
37+
break;
38+
};
39+
40+
await RoomContext.updateOne({room: newRoomInfo.room}, {content: newContent});
41+
42+
};
43+
44+
const insertRoom = async (newRoomInfo: RoomInfo): Promise<void> => {
45+
await RoomContext.create(newRoomInfo);
46+
};
47+
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { envConstants } from 'core/constants';
1+
import { envConstants } from 'core';
22
import * as mockRepository from './session.mock';
33
import * as repository from './session.repository';
4-
import { SessionRepository } from './session.contract';
4+
import { SessionRepositoryContract } from './session.contract';
55

6-
export const sessionRepository: SessionRepository = envConstants.isMockRepository
6+
export const sessionRepository: SessionRepositoryContract = envConstants.isMockRepository
77
? mockRepository
88
: repository;
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import { RoomInfo } from 'dals/room';
2-
import { ConnectSessionInfo } from 'dals/session';
3-
import { UserSession } from '../session.model';
1+
import { UserSession } from 'dals';
42

5-
export interface SessionRepository {
3+
export interface SessionRepositoryContract {
64
isTrainerUser: (connectionId: string) => Promise<boolean>;
7-
saveRoomInfo: (newRoomInfo: RoomInfo, action: string) => Promise<void>;
85
isExistingConnection: (connectionId: string) => Promise<boolean>;
9-
isRoomAvailable: (room: string) => Promise<boolean>;
106
addNewUser: (userSession: UserSession) => Promise<void>;
117
getRoomFromConnectionId: (connectionId: string) => Promise<string>;
12-
getRoomContent:(room: string) => Promise<string>;
13-
}
14-
8+
}
Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import produce from 'immer';
2-
import { InputMessageTypes } from 'messages';
3-
import {ConnectSessionInfo, RoomInfo, UserSession} from 'dals'
1+
import { UserSession } from 'dals'
42

53
let userCollectionSession: UserSession[] = [];
6-
let roomCollectionSession: RoomInfo[] = [];
7-
8-
export const isRoomAvailable = async (room: string): Promise<boolean> =>
9-
!userCollectionSession.find((session) => session.room === room);
104

115
export const addNewUser = async (newUser: UserSession): Promise<void> => {
126
const {connectionId, room, trainerToken, isTrainer } = newUser;
@@ -21,23 +15,6 @@ export const addNewUser = async (newUser: UserSession): Promise<void> => {
2115
];
2216
};
2317

24-
export const saveRoomInfo = async (newRoomInfo: RoomInfo, action: string): Promise<void> => {
25-
const roomIndex: number = await getRoomIndexByName(newRoomInfo);
26-
const content: string = newRoomInfo.content;
27-
roomCollectionSession =
28-
roomIndex === -1
29-
? await insertRoom(newRoomInfo)
30-
: await updateRoomContent(roomIndex, content, action);
31-
};
32-
33-
export const getRoomContent = async (room: string): Promise<string> => {
34-
const roomInfo = roomCollectionSession.find(
35-
(roomInfo: RoomInfo) => roomInfo.room === room
36-
);
37-
38-
return roomInfo ? roomInfo.content : '';
39-
};
40-
4118
export const isTrainerUser = async (connectionId: string): Promise<boolean> => {
4219
const session = userCollectionSession.find(
4320
(session) => session.connectionId === connectionId && session.isTrainer
@@ -55,29 +32,4 @@ export const getRoomFromConnectionId = async (connectionId: string): Promise<str
5532
(session) => session.connectionId === connectionId
5633
);
5734
return session ? session.room : '';
58-
};
59-
60-
const getRoomIndexByName = async (newRoomInfo: RoomInfo): Promise<number> => {
61-
return roomCollectionSession.findIndex(
62-
(roomInfo: RoomInfo) => roomInfo.room === newRoomInfo.room
63-
);
64-
};
65-
66-
const updateRoomContent = async (index: number, content: string, action:string): Promise<RoomInfo[]> => {
67-
return produce(roomCollectionSession, (draftState: RoomInfo[]) => {
68-
switch (action) {
69-
case InputMessageTypes.TRAINER_APPEND_TEXT:
70-
draftState[index].content += '\n' + content;
71-
break;
72-
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
73-
draftState[index].content = content;
74-
break;
75-
};
76-
});
77-
};
78-
79-
const insertRoom = async (newRoomInfo: RoomInfo): Promise<RoomInfo[]> => {
80-
return produce(roomCollectionSession, (draftState: RoomInfo[]) => {
81-
draftState.push(newRoomInfo);
82-
});
83-
};
35+
};

‎back/src/dals/session/repository/session.repository.ts‎

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,4 @@
1-
import { InputMessageTypes } from 'messages';
2-
import {RoomInfo, UserSession, UserSessionContext, RoomContext} from 'dals'
3-
4-
export const isRoomAvailable = async (room: string): Promise<boolean> => {
5-
const roomAvailable = !(await RoomContext.exists({ room }));
6-
return roomAvailable;
7-
}
8-
9-
export const saveRoomInfo = async (newRoomInfo: RoomInfo, action: string): Promise<void> => {
10-
await isRoomAvailable(newRoomInfo.room) ? await insertRoom(newRoomInfo) : await updateRoomContent(newRoomInfo, action);
11-
};
12-
13-
export const getRoomContent = async (room: string): Promise<string> => {
14-
const roomInfo: RoomInfo = await getRoomByName(room);
15-
return roomInfo ? roomInfo.content : '';
16-
};
17-
18-
const getRoomByName = async (room: string): Promise<RoomInfo> => {
19-
return (await RoomContext.findOne({room: room}).lean());
20-
};
21-
22-
const updateRoomContent = async (newRoomInfo: RoomInfo, action:string): Promise<void> => {
23-
const room: RoomInfo = await getRoomByName(newRoomInfo.room);
24-
25-
let newContent = "";
26-
27-
if(room) {
28-
newContent = room.content;
29-
}
30-
31-
switch (action) {
32-
case InputMessageTypes.TRAINER_APPEND_TEXT:
33-
newContent += '\n' + newRoomInfo.content;
34-
break;
35-
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
36-
newContent = newRoomInfo.content;
37-
break;
38-
};
39-
40-
await RoomContext.updateOne({room: newRoomInfo.room}, {content: newContent});
41-
42-
};
43-
44-
const insertRoom = async (newRoomInfo: RoomInfo): Promise<void> => {
45-
await RoomContext.create(newRoomInfo);
46-
};
47-
48-
const getRoomCollection = async (): Promise<RoomInfo[]> => {
49-
return (await RoomContext.find().lean());
50-
}
1+
import { UserSession, UserSessionContext } from 'dals'
512

523
export const addNewUser = async ( newUser: UserSession): Promise<void> => {
534
await UserSessionContext.create(newUser);

‎back/src/messages/input-processor.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { InputMessageTypes, OutputMessageTypes } from './messages.consts';
22
import { Action, InputEstablishConnectionTrainer, SocketInfo } from './messages.model';
3-
import {sessionRepository } from 'dals';
4-
const {addNewUser, getRoomFromConnectionId, isExistingConnection, isRoomAvailable, isTrainerUser, saveRoomInfo} = sessionRepository;
3+
import {sessionRepository, roomRepository } from 'dals';
4+
const {addNewUser, getRoomFromConnectionId, isExistingConnection, isTrainerUser } = sessionRepository;
5+
const { isRoomAvailable, saveRoomInfo} = roomRepository;
56

67
export const processInputMessage = async (socketInfo: SocketInfo, action: Action): Promise<Action[]> => {
78
let outputActionCollection: Action[] = [];

0 commit comments

Comments
 (0)