Skip to content

Commit 918cdb8

Browse files
authored
Merge pull request #112 from Lemoncode/#23--Mongo-DB-persistence
Mongo DB persistence added
2 parents c3a1869 + 4498de5 commit 918cdb8

22 files changed

Lines changed: 224 additions & 117 deletions

‎back/.env‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
NODE_ENV=development
22
PORT=8081
3+
SOCKET_PORT=3000
4+
MOCK_REPOSITORY=false
5+
API_URL=/api
6+
CORS_ORIGIN=http://localhost:8080
7+
MONGODB_URI=mongodb://localhost:27017/code-paster

‎back/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"start": "run-p -l type-check:watch start:dev",
7+
"start": "run-p -l type-check:watch start:dev start:local-db",
8+
"start:mock": "cross-env MOCK_REPOSITORY=true run-p -l type-check:watch start:dev",
89
"start:dev": "nodemon --exec babel-node --extensions \".ts\" src/index.ts",
910
"start:debug": "run-p -l type-check:watch \"start:dev -- --inspect-brk\"",
1011
"start:prod": "node ./dist/index.js",
11-
"start:mock-repository": "cross-env MOCK_REPOSITORY=true npm run start",
1212
"start:local-db": "docker-compose up || echo \"Run docker-compose up manually\"",
1313
"stop:local-db": "docker-compose down || echo \"Run docker-compose down manually\"",
1414
"build": "run-p -l type-check build:prod",

‎back/src/app.ts‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import { createApp } from './express.server';
2-
import { envConstants } from './env.constants';
3-
import { api } from './api';
41
import cors from 'cors';
52
import SocketIOClient, { Socket } from 'socket.io';
6-
import {
7-
processInputMessage,
8-
InputMessageTypes,
9-
processOutputMessageCollection,
10-
Action,
11-
SocketInfo,
12-
} from './messages';
3+
4+
import { createApp } from './express.server';
5+
import { envConstants, connectToDB } from 'core';
6+
import { api } from './api';
7+
import { processInputMessage, InputMessageTypes, processOutputMessageCollection, Action, SocketInfo } from './messages';
138

149
const app = createApp();
1510

@@ -35,10 +30,14 @@ const options: cors.CorsOptions = {
3530
};
3631

3732
app.use(cors(options));
38-
app.use('/api', api);
33+
app.use(envConstants.API_URL, api);
3934

40-
app.listen(envConstants.PORT, () => {
41-
console.log(`Server ready at http://localhost:${envConstants.PORT}/api`);
35+
app.listen(envConstants.PORT, async () => {
36+
if(!envConstants.isMockRepository && envConstants.MONGODB_URI) {
37+
await connectToDB(envConstants.MONGODB_URI);
38+
}
39+
console.log(`Using ${envConstants.isMockRepository ? 'Mock' : 'DataBase'} for session storage`)
40+
console.log(`Server ready at http://localhost:${envConstants.PORT}${envConstants.API_URL}`);
4241
});
4342

4443
// whenever a user connects on port 3000 via

‎back/src/core/db/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './database.connection';

‎back/src/core/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './constants'
2+
export * from './db'
3+
export * from './servers'

‎back/src/core/servers/cors.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { envConstants } from '../constants';
1+
import { envConstants } from 'core';
22
import cors from 'cors';
33

44
const options: cors.CorsOptions = {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export * from './room.model';
1+
export * from './room.model';
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+
};

0 commit comments

Comments
 (0)