1
1
import { Injectable } from '@nestjs/common' ;
2
2
import { Repository } from 'typeorm' ;
3
3
import { WorldEntity } from '../models/world.entity' ;
4
+ import { FortuneEntity } from '../models/fortune.entity' ;
4
5
import { InjectRepository } from '@nestjs/typeorm' ;
5
6
6
7
@Injectable ( )
7
8
export class BenchService {
8
9
constructor (
9
10
@InjectRepository ( WorldEntity )
10
- private worldRepository : Repository < WorldEntity > ) { }
11
+ private worldRepository : Repository < WorldEntity > ,
12
+ @InjectRepository ( FortuneEntity )
13
+ private fortuneRepository : Repository < FortuneEntity > ,
14
+ ) { }
11
15
12
16
getOne ( ) {
13
17
@@ -16,28 +20,33 @@ export class BenchService {
16
20
17
21
async getMultiple ( totalQueries : string ) {
18
22
19
- return new Promise ( async ( resolve , reject ) => {
20
- const worldArr = [ ] ;
21
- const total = this . sanitizeQueries ( totalQueries ) ;
22
- for ( let i = 0 ; i < total ; i ++ ) {
23
- worldArr . push ( await this . getOne ( ) ) ;
24
- }
25
- resolve ( worldArr ) ;
26
- } ) ;
23
+ const worldArr = [ ] ;
24
+ const total = this . sanitizeQueries ( totalQueries ) ;
25
+ for ( let i = 0 ; i < total ; i ++ ) {
26
+ worldArr . push ( await this . getOne ( ) ) ;
27
+ }
28
+ return worldArr ;
27
29
}
28
30
29
31
async updateMultiple ( totalQueries : string ) {
30
32
31
- return new Promise ( async ( resolve , reject ) => {
32
- const worldArr = [ ] ;
33
- const total = this . sanitizeQueries ( totalQueries ) ;
34
- for ( let i = 0 ; i < total ; i ++ ) {
35
- let worldToUpdate = await this . getOne ( ) ;
36
- worldToUpdate . randomnumber = Math . floor ( Math . random ( ) * 10000 ) + 1 ;
37
- worldToUpdate = await this . worldRepository . save ( worldToUpdate ) ;
38
- worldArr . push ( worldToUpdate ) ;
39
- }
40
- resolve ( worldArr ) ;
33
+ const worldArr = [ ] ;
34
+ const total = this . sanitizeQueries ( totalQueries ) ;
35
+ for ( let i = 0 ; i < total ; i ++ ) {
36
+ let worldToUpdate = await this . getOne ( ) ;
37
+ worldToUpdate . randomnumber = Math . floor ( Math . random ( ) * 10000 ) + 1 ;
38
+ worldArr . push ( worldToUpdate ) ;
39
+ await this . worldRepository . update ( worldToUpdate . id , worldToUpdate ) ;
40
+ }
41
+ return worldArr ;
42
+ }
43
+
44
+ async getFortunes ( ) {
45
+ return this . fortuneRepository . find ( ) . then ( ( fortunes ) => {
46
+ const newFortune = { id : 0 , message : "Additional fortune added at request time." } ;
47
+ fortunes . push ( newFortune ) ;
48
+ fortunes . sort ( ( a , b ) => ( a . message < b . message ) ? - 1 : 1 ) ;
49
+ return fortunes ;
41
50
} ) ;
42
51
}
43
52
0 commit comments