1
- import { Controller , Get , Post , Body , Param } from '@nestjs/common' ;
1
+ import { Controller , Get , Post , HttpCode , HttpStatus , Body , Param } from '@nestjs/common' ;
2
2
import {
3
3
ParticipantService ,
4
4
FriendModel ,
@@ -9,26 +9,24 @@ import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
9
9
10
10
export class Friend {
11
11
constructor (
12
- public readonly id : number ,
12
+ public readonly id : number = 0 ,
13
13
public readonly from : string ,
14
14
public readonly to : string ,
15
15
) { }
16
16
}
17
17
18
18
export class Participant {
19
19
constructor (
20
- public readonly id : number ,
20
+ public readonly id : number = 0 ,
21
21
public readonly name : string ,
22
+ public readonly link : string ,
22
23
) { }
23
-
24
- accessor link = `/participant/${ this . id } ` ;
25
24
}
26
-
27
25
export class Inbound {
28
26
constructor (
29
27
public readonly from : string ,
30
28
public readonly to : string ,
31
- public readonly occurred : Date ,
29
+ public readonly occurred : string ,
32
30
public readonly subject : string ,
33
31
public readonly story : string ,
34
32
) { }
@@ -49,6 +47,7 @@ export class ParticipantApiController {
49
47
constructor ( private readonly participantApiService : ParticipantService ) { }
50
48
51
49
@Post ( ':id/friends' )
50
+ @HttpCode ( HttpStatus . OK )
52
51
@ApiOperation ( { summary : 'Create a new friendship' } )
53
52
@ApiResponse ( {
54
53
status : 200 ,
@@ -59,18 +58,21 @@ export class ParticipantApiController {
59
58
@Param ( 'id' ) id : number ,
60
59
@Body ( ) body : Friend ,
61
60
) : Promise < Friend > {
61
+ const fpm = new ParticipantModel ( parseInt ( body . from . split ( '/' ) . pop ( ) as string ) , '' ) ;
62
+ const tpm = new ParticipantModel ( parseInt ( body . to . split ( '/' ) . pop ( ) as string ) , '' ) ;
62
63
const fm = new FriendModel (
63
64
body . id ,
64
- new ParticipantModel ( parseInt ( body . from . split ( '/' ) . pop ( ) as string ) , '' ) ,
65
- new ParticipantModel ( parseInt ( body . to . split ( '/' ) . pop ( ) as string ) , '' ) ,
65
+ fpm ,
66
+ tpm ,
66
67
) ;
67
68
const saved = await this . participantApiService . addFriend ( id , fm ) ;
68
- const from = new Participant ( saved . from . id , '' ) ;
69
- const to = new Participant ( saved . to . id , '' ) ;
69
+ const from = new Participant ( saved . from . id , '' , fpm . link ) ;
70
+ const to = new Participant ( saved . to . id , '' , tpm . link ) ;
70
71
return new Friend ( saved . id , from . link , to . link ) ;
71
72
}
72
73
73
74
@Post ( ':id/outbound' )
75
+ @HttpCode ( HttpStatus . OK )
74
76
@ApiOperation ( { summary : 'Create a participant news item' } )
75
77
@ApiResponse ( {
76
78
status : 200 ,
@@ -92,6 +94,7 @@ export class ParticipantApiController {
92
94
}
93
95
94
96
@Post ( )
97
+ @HttpCode ( HttpStatus . OK )
95
98
@ApiOperation ( { summary : 'Create a new participant' } )
96
99
@ApiResponse ( {
97
100
status : 200 ,
@@ -101,7 +104,7 @@ export class ParticipantApiController {
101
104
public async addParticipant ( @Body ( ) body : Participant ) : Promise < Participant > {
102
105
const pm = new ParticipantModel ( body . id , body . name ) ;
103
106
const saved = await this . participantApiService . addParticipant ( pm ) ;
104
- return new Participant ( saved . id , saved . moniker ) ;
107
+ return new Participant ( saved . id , saved . name , saved . link ) ;
105
108
}
106
109
107
110
@Get ( ':id/friends' )
@@ -116,8 +119,8 @@ export class ParticipantApiController {
116
119
public async getFriends ( @Param ( 'id' ) id : number ) : Promise < Friend [ ] > {
117
120
const rv = await this . participantApiService . getFriends ( id ) ;
118
121
return rv . map ( ( f ) => {
119
- const from = new Participant ( f . from . id , '' ) ;
120
- const to = new Participant ( f . to . id , '' ) ;
122
+ const from = new Participant ( f . from . id , '' , f . from . link ) ;
123
+ const to = new Participant ( f . to . id , '' , f . to . link ) ;
121
124
return new Friend ( f . id , from . link , to . link ) ;
122
125
} ) ;
123
126
}
@@ -134,9 +137,9 @@ export class ParticipantApiController {
134
137
public async getInbound ( @Param ( 'id' ) id : number ) : Promise < Inbound [ ] > {
135
138
const ib = await this . participantApiService . getInbound ( id ) ;
136
139
return ib . map ( ( i ) => {
137
- const from = new Participant ( i . from . id , '' ) ;
138
- const to = new Participant ( i . to . id , '' ) ;
139
- return new Inbound ( from . link , to . link , i . occurred , i . subject , i . story ) ;
140
+ const from = new Participant ( i . from . id , '' , '/participant/' + ` ${ i . from . id } ` ) ;
141
+ const to = new Participant ( i . to . id , '' , '/participant/' + ` ${ i . to . id } ` ) ;
142
+ return new Inbound ( from . link , to . link , i . occurred . toISOString ( ) . slice ( 0 , 10 ) , i . subject , i . story ) ;
140
143
} ) ;
141
144
}
142
145
@@ -165,6 +168,6 @@ export class ParticipantApiController {
165
168
} )
166
169
public async getParticipant ( @Param ( 'id' ) id : number ) : Promise < Participant > {
167
170
const pm = await this . participantApiService . getParticipant ( id ) ;
168
- return new Participant ( pm . id , pm . moniker ) ;
171
+ return new Participant ( pm . id , pm . name , pm . link ) ;
169
172
}
170
173
}
0 commit comments