1
+ using System . Security . Claims ;
1
2
using MediatR ;
3
+ using Microsoft . AspNetCore . Authorization ;
2
4
using Microsoft . AspNetCore . Mvc ;
3
5
using PlayOfferService . Application . Commands ;
6
+ using PlayOfferService . Application . Exceptions ;
4
7
using PlayOfferService . Application . Queries ;
5
8
using PlayOfferService . Domain . Models ;
6
9
@@ -18,22 +21,22 @@ public PlayOfferController(IMediator mediator)
18
21
_mediator = mediator ;
19
22
}
20
23
21
- ///<summary>
22
- ///Retrieve all Play Offers of a club with a matching id
23
- ///</summary>
24
- ///<param name="clubId">The id of the club of the play offer</param>
25
- ///<returns>Play offers with a matching club id</returns>
26
- ///<response code="200">Returns a list of Play offers matching the query params</response>
27
- ///<response code="204">No Play offer with matching properties was found</response>
24
+ /// <summary>
25
+ /// Retrieve all Play Offers of the logged in users club
26
+ /// </summary>
27
+ /// <returns>Play offers with a matching club id</returns>
28
+ /// <response code="200">Returns a list of Play offers matching the query params</response>
29
+ /// <response code="204">No Play offer with matching properties was found</response>
28
30
[ HttpGet ]
31
+ [ Authorize ]
29
32
[ Route ( "club" ) ]
30
33
[ ProducesResponseType ( typeof ( IEnumerable < PlayOfferDto > ) , StatusCodes . Status200OK ) ]
31
34
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status204NoContent ) ]
32
35
[ Consumes ( "application/json" ) ]
33
36
[ Produces ( "application/json" ) ]
34
- public async Task < ActionResult < IEnumerable < PlayOfferDto > > > GetByClubIdAsync ( [ FromQuery ] Guid clubId )
37
+ public async Task < ActionResult < IEnumerable < PlayOfferDto > > > GetByClubIdAsync ( )
35
38
{
36
- //TODO: refactor after jwt implementation to get clubId from token
39
+ var clubId = Guid . Parse ( User . Claims . First ( c => c . Type == "tennisClubId" ) . Value ) ;
37
40
var result = await _mediator . Send ( new GetPlayOffersByClubIdQuery ( clubId ) ) ;
38
41
39
42
if ( result . Count ( ) == 0 )
@@ -43,21 +46,21 @@ public async Task<ActionResult<IEnumerable<PlayOfferDto>>> GetByClubIdAsync([Fro
43
46
}
44
47
45
48
///<summary>
46
- ///Retrieve all Play Offers of a participating member
49
+ ///Retrieve all Play Offers of a logged in user
47
50
///</summary>
48
- ///<param name="participantId">The id of the member participating in the play offer</param>
49
51
///<returns>List of Play offers with where given member is creator or opponent</returns>
50
52
///<response code="200">Returns a list of Play offers matching the query params</response>
51
53
///<response code="204">No Play offer with matching properties was found</response>
52
54
[ HttpGet ]
55
+ [ Authorize ]
53
56
[ Route ( "participant" ) ]
54
57
[ ProducesResponseType ( typeof ( IEnumerable < PlayOffer > ) , StatusCodes . Status200OK ) ]
55
58
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status204NoContent ) ]
56
59
[ Consumes ( "application/json" ) ]
57
60
[ Produces ( "application/json" ) ]
58
- public async Task < ActionResult < IEnumerable < PlayOfferDto > > > GetByParticipantIdAsync ( [ FromQuery ] Guid participantId )
61
+ public async Task < ActionResult < IEnumerable < PlayOfferDto > > > GetByParticipantIdAsync ( )
59
62
{
60
- //TODO: refactor after jwt implementation to get participantId from token
63
+ var participantId = Guid . Parse ( User . FindFirst ( ClaimTypes . NameIdentifier ) . Value ) ;
61
64
var result = await _mediator . Send ( new GetPlayOffersByParticipantIdQuery ( participantId ) ) ;
62
65
63
66
if ( result . Count ( ) == 0 )
@@ -74,6 +77,7 @@ public async Task<ActionResult<IEnumerable<PlayOfferDto>>> GetByParticipantIdAsy
74
77
///<response code="200">Returns a List of Play offers with creator matching the query params</response>
75
78
///<response code="204">No Play offers with matching creator was found</response>
76
79
[ HttpGet ]
80
+ [ Authorize ]
77
81
[ Route ( "search" ) ]
78
82
[ ProducesResponseType ( typeof ( IEnumerable < PlayOffer > ) , StatusCodes . Status200OK ) ]
79
83
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status204NoContent ) ]
@@ -100,23 +104,31 @@ public async Task<ActionResult<IEnumerable<PlayOfferDto>>> GetByCreatorNameAsync
100
104
101
105
102
106
///<summary>
103
- ///Create a new Play Offer
107
+ ///Create a new Play Offer for the logged in user
104
108
///</summary>
105
109
///<param name="createPlayOfferDto">The Play Offer to create</param>
106
110
///<returns>The newly created Play offer</returns>
107
111
///<response code="200">Returns the id of the created Play Offer</response>
108
112
///<response code="400">Invalid Play Offer structure</response>
113
+ ///<response code="401">Only members can create Play Offers</response>
109
114
[ HttpPost ]
115
+ [ Authorize ]
110
116
[ ProducesResponseType ( typeof ( PlayOffer ) , StatusCodes . Status201Created ) ]
111
117
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status400BadRequest ) ]
112
118
[ Consumes ( "application/json" ) ]
113
119
[ Produces ( "application/json" ) ]
114
120
public async Task < ActionResult < PlayOffer > > Create ( CreatePlayOfferDto createPlayOfferDto )
115
121
{
122
+ if ( User . Claims . First ( c => c . Type == "groups" ) . Value != "MEMBER" )
123
+ return Unauthorized ( "Only members can create Play Offers!" ) ;
124
+
125
+ var creatorId = Guid . Parse ( User . FindFirst ( ClaimTypes . NameIdentifier ) ! . Value ) ;
126
+ var clubId = Guid . Parse ( User . FindFirst ( "tennisClubId" ) ! . Value ) ;
127
+
116
128
Guid result ;
117
129
try
118
130
{
119
- result = await _mediator . Send ( new CreatePlayOfferCommand ( createPlayOfferDto ) ) ;
131
+ result = await _mediator . Send ( new CreatePlayOfferCommand ( createPlayOfferDto , creatorId , clubId ) ) ;
120
132
}
121
133
catch ( Exception e )
122
134
{
@@ -127,22 +139,33 @@ public async Task<ActionResult<PlayOffer>> Create(CreatePlayOfferDto createPlayO
127
139
}
128
140
129
141
///<summary>
130
- ///Cancels a Play Offer with a matching id
142
+ ///Cancels a Play Offer with a matching id of the logged in user
131
143
///</summary>
132
144
///<param name="playOfferId">The id of the Play Offer to cancel</param>
133
145
///<returns>Nothing</returns>
134
146
///<response code="200">The Play Offer with the matching id was cancelled</response>
135
147
///<response code="400">No Play Offer with matching id found</response>
148
+ ///<response code="401">Only creator can cancel Play Offers</response>
136
149
[ HttpDelete ]
150
+ [ Authorize ]
137
151
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status200OK ) ]
138
152
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status400BadRequest ) ]
139
153
[ Consumes ( "application/json" ) ]
140
154
[ Produces ( "application/json" ) ]
141
155
public async Task < ActionResult > Delete ( Guid playOfferId )
142
156
{
157
+ if ( User . Claims . First ( c => c . Type == "groups" ) . Value != "MEMBER" )
158
+ return Unauthorized ( "Only members can cancel Play Offers!" ) ;
159
+
160
+ var memberId = Guid . Parse ( User . FindFirst ( ClaimTypes . NameIdentifier ) ! . Value ) ;
161
+
143
162
try
144
163
{
145
- await _mediator . Send ( new CancelPlayOfferCommand ( playOfferId ) ) ;
164
+ await _mediator . Send ( new CancelPlayOfferCommand ( playOfferId , memberId ) ) ;
165
+ }
166
+ catch ( AuthorizationException e )
167
+ {
168
+ return Unauthorized ( e . Message ) ;
146
169
}
147
170
catch ( Exception e )
148
171
{
@@ -153,23 +176,29 @@ public async Task<ActionResult> Delete(Guid playOfferId)
153
176
}
154
177
155
178
///<summary>
156
- ///Adds a given opponentId to a Play Offer and creates a reservation
179
+ ///Logged in user joins a Play Offer with a matching playOfferId
157
180
///</summary>
158
181
///<param name="joinPlayOfferDto">The opponentId to add to the Play Offer with the matching playOfferId</param>
159
182
///<returns>Nothing</returns>
160
183
///<response code="200">The opponentId was added to the Play Offer with the matching playOfferId</response>
161
184
///<response code="400">No playOffer with a matching playOfferId found</response>
185
+ ///<response code="401">Only members can join Play Offers</response>
162
186
[ HttpPost ]
187
+ [ Authorize ]
163
188
[ Route ( "join" ) ]
164
189
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status200OK ) ]
165
190
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status400BadRequest ) ]
166
191
[ Consumes ( "application/json" ) ]
167
192
[ Produces ( "application/json" ) ]
168
193
public async Task < ActionResult > Join ( JoinPlayOfferDto joinPlayOfferDto )
169
194
{
195
+ if ( User . Claims . First ( c => c . Type == "groups" ) . Value != "MEMBER" )
196
+ return Unauthorized ( "Only members can join Play Offers!" ) ;
197
+
198
+ var memberId = Guid . Parse ( User . FindFirst ( ClaimTypes . NameIdentifier ) ! . Value ) ;
170
199
try
171
200
{
172
- await _mediator . Send ( new JoinPlayOfferCommand ( joinPlayOfferDto ) ) ;
201
+ await _mediator . Send ( new JoinPlayOfferCommand ( joinPlayOfferDto , memberId ) ) ;
173
202
}
174
203
catch ( Exception e )
175
204
{
0 commit comments