Skip to content

Commit a29a1cc

Browse files
committed
can delete and add
1 parent 817fc94 commit a29a1cc

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

src/app/main/scores/scores.controller.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ export class ScoresController {
88

99
@Get()
1010
getScores(): any {
11-
return this.scoresService.getScores();
11+
return this.scoresService.getLiveScores();
12+
}
13+
@Get('add/batchID/:batchID')
14+
getScore(@Param('batchID') batchID: string): any {
15+
return this.scoresService.addRandomStuff(batchID);
16+
}
17+
@Get('delete/batchID/:batchID')
18+
deleteScore(@Param('batchID') batchID: string): any {
19+
return this.scoresService.deleteRandomStuff(batchID);
1220
}
1321
// @Get('batchID/:batchID')
1422
// getScore(@Param('batchID') batchID: string): any {

src/app/main/scores/scores.gateway.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class ScoresGateway
2626
async handleMessage(client: Socket, payload: string): Promise<any> {
2727
const scores = await this.scoresService.getScores();
2828
this.server.emit('msgToClient', scores);
29+
2930
// this.server.emit('msgToClient', payload);
3031
}
3132

@@ -35,7 +36,7 @@ export class ScoresGateway
3536

3637
async handleConnection(client: Socket, ...args: any[]) {
3738
this.logger.log(`Client connected: ${client.id}`);
38-
const scores = await this.scoresService.getScores();
39+
const scores = await this.scoresService.getLiveScores();
3940
this.server.emit('msgToClient', scores);
4041
}
4142

src/app/main/scores/service/scores.service.ts

+43-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,32 @@ export class ScoresService {
1212
private readonly scoreModel: Model<ScoreDocument>
1313
) {}
1414

15+
async getLiveScores(): Promise<any> {
16+
try {
17+
this.scoreModel.watch().on('change', (data) => {
18+
console.log('\n ========================= \n');
19+
console.log(data);
20+
});
21+
22+
return this.scoreModel.find().sort('order').exec();
23+
// return await this.scoreModel.find().sort('order').exec();
24+
} catch (error) {
25+
throw new HttpException(
26+
{
27+
status: HttpStatus.FORBIDDEN,
28+
error: 'This is a custom exception message'
29+
},
30+
HttpStatus.FORBIDDEN
31+
);
32+
}
33+
}
1534
async getScores() {
1635
try {
1736
return this.scoreModel.find().sort('order').exec();
1837

1938
// return await this.scoreModel.find().sort('order').exec();
2039
} catch (error) {
21-
return new GraphQLError(error);
40+
throw new HttpException('Failed', HttpStatus.EXPECTATION_FAILED);
2241
}
2342
}
2443
async getScore(_id: string) {
@@ -40,7 +59,7 @@ export class ScoresService {
4059
.sort({ batchID: -1 }) // sorts by "batchID: true" to the top
4160
.exec();
4261
} catch (error) {
43-
return new GraphQLError(error);
62+
throw new HttpException('Failed', HttpStatus.EXPECTATION_FAILED);
4463
}
4564
}
4665
async getActiveScores() {
@@ -49,17 +68,35 @@ export class ScoresService {
4968
inProgress: true
5069
});
5170
} catch (error) {
52-
return new GraphQLError(error);
71+
throw new HttpException('Failed', HttpStatus.EXPECTATION_FAILED);
5372
}
5473
}
5574

75+
async addRandomStuff(tmcHostname: string) {
76+
const scoreGuid = tmcHostname;
77+
const batchID = tmcHostname;
78+
const score: Score = new Score({ tmcHostname, scoreGuid, batchID });
79+
score.startTime = new Date();
80+
try {
81+
return await new this.scoreModel(score).save();
82+
} catch (error) {
83+
throw new HttpException(error, HttpStatus.EXPECTATION_FAILED);
84+
}
85+
}
86+
async deleteRandomStuff(name: string): Promise<any> {
87+
try {
88+
return await this.scoreModel.findOneAndDelete({ tmcHostname: name });
89+
} catch (error) {
90+
throw new HttpException(error, HttpStatus.EXPECTATION_FAILED);
91+
}
92+
}
5693
async scoreStart(tmcHostname: string, scoreGuid: string, batchID: string) {
5794
const score: Score = new Score({ tmcHostname, scoreGuid, batchID });
5895
score.startTime = new Date();
5996
try {
6097
return await new this.scoreModel(score).save();
6198
} catch (error) {
62-
return new GraphQLError(error);
99+
throw new HttpException('Failed', HttpStatus.EXPECTATION_FAILED);
63100
}
64101
}
65102

@@ -109,7 +146,7 @@ export class ScoresService {
109146
return pd;
110147
});
111148
} catch (error) {
112-
return new GraphQLError(error);
149+
throw new HttpException('Failed', HttpStatus.EXPECTATION_FAILED);
113150
}
114151
}
115152

@@ -127,7 +164,7 @@ export class ScoresService {
127164
})
128165
.exec();
129166
} catch (error) {
130-
return new GraphQLError(error);
167+
throw new HttpException('Failed', HttpStatus.EXPECTATION_FAILED);
131168
}
132169
}
133170

0 commit comments

Comments
 (0)