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
7
10
namespace PlayOfferService . Application . Controllers ;
8
11
9
12
[ ApiController ]
10
- [ Route ( "api" ) ]
13
+ [ Route ( "api/playoffers " ) ]
11
14
public class PlayOfferController : ControllerBase
12
15
{
13
16
@@ -18,49 +21,114 @@ public PlayOfferController(IMediator mediator)
18
21
_mediator = mediator ;
19
22
}
20
23
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>
30
+ [ HttpGet ]
31
+ [ Authorize ]
32
+ [ Route ( "club" ) ]
33
+ [ ProducesResponseType ( typeof ( IEnumerable < PlayOfferDto > ) , StatusCodes . Status200OK ) ]
34
+ [ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status204NoContent ) ]
35
+ [ Consumes ( "application/json" ) ]
36
+ [ Produces ( "application/json" ) ]
37
+ public async Task < ActionResult < IEnumerable < PlayOfferDto > > > GetByClubIdAsync ( )
38
+ {
39
+ var clubId = Guid . Parse ( User . Claims . First ( c => c . Type == "tennisClubId" ) . Value ) ;
40
+ var result = await _mediator . Send ( new GetPlayOffersByClubIdQuery ( clubId ) ) ;
41
+
42
+ if ( result . Count ( ) == 0 )
43
+ return NoContent ( ) ;
44
+
45
+ return Ok ( result ) ;
46
+ }
47
+
21
48
///<summary>
22
- ///Retrieve all Play Offers matching the query params
49
+ ///Retrieve all Play Offers of a logged in user
23
50
///</summary>
24
- ///<param name="playOfferId">The id of the play offer</param>
25
- ///<param name="creatorId">The id of the creator of the play offer</param>
26
- ///<param name="clubId">The id of the club of the play offer</param>
27
- ///<returns>Play offer with a matching id</returns>
28
- ///<response code="200">Returns a Play offer matching the query params</response>
51
+ ///<returns>List of Play offers with where given member is creator or opponent</returns>
52
+ ///<response code="200">Returns a list of Play offers matching the query params</response>
29
53
///<response code="204">No Play offer with matching properties was found</response>
30
54
[ HttpGet ]
55
+ [ Authorize ]
56
+ [ Route ( "participant" ) ]
57
+ [ ProducesResponseType ( typeof ( IEnumerable < PlayOffer > ) , StatusCodes . Status200OK ) ]
58
+ [ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status204NoContent ) ]
59
+ [ Consumes ( "application/json" ) ]
60
+ [ Produces ( "application/json" ) ]
61
+ public async Task < ActionResult < IEnumerable < PlayOfferDto > > > GetByParticipantIdAsync ( )
62
+ {
63
+ var participantId = Guid . Parse ( User . FindFirst ( ClaimTypes . NameIdentifier ) . Value ) ;
64
+ var result = await _mediator . Send ( new GetPlayOffersByParticipantIdQuery ( participantId ) ) ;
65
+
66
+ if ( result . Count ( ) == 0 )
67
+ return NoContent ( ) ;
68
+
69
+ return Ok ( result ) ;
70
+ }
71
+
72
+ ///<summary>
73
+ ///Get all Play offers created by a member with a matching name
74
+ ///</summary>
75
+ ///<param name="creatorName">Name of the creator in the format '[FirstName] [LastName]', '[FirstName]' or '[LastName]'</param>
76
+ ///<returns>A list of Play offers with a matching id</returns>
77
+ ///<response code="200">Returns a List of Play offers with creator matching the query params</response>
78
+ ///<response code="204">No Play offers with matching creator was found</response>
79
+ [ HttpGet ]
80
+ [ Authorize ]
81
+ [ Route ( "search" ) ]
31
82
[ ProducesResponseType ( typeof ( IEnumerable < PlayOffer > ) , StatusCodes . Status200OK ) ]
32
83
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status204NoContent ) ]
33
84
[ Consumes ( "application/json" ) ]
34
85
[ Produces ( "application/json" ) ]
35
- public async Task < ActionResult < IEnumerable < PlayOffer > > > GetByIdAsync ( [ FromQuery ] Guid ? playOfferId , [ FromQuery ] Guid ? creatorId , [ FromQuery ] Guid ? clubId )
86
+ public async Task < ActionResult < IEnumerable < PlayOfferDto > > > GetByCreatorNameAsync ( [ FromQuery ] string creatorName )
36
87
{
37
- var result = await _mediator . Send ( new GetPlayOffersByIdQuery ( playOfferId , creatorId , clubId ) ) ;
88
+ IEnumerable < PlayOfferDto > result ;
89
+ try
90
+ {
91
+ result = await _mediator . Send ( new GetPlayOffersByCreatorNameQuery ( creatorName ) ) ;
92
+ }
93
+ catch ( Exception e )
94
+ {
95
+ return BadRequest ( e . Message ) ;
96
+ }
38
97
39
98
if ( result . Count ( ) == 0 )
40
99
return NoContent ( ) ;
41
100
42
101
return Ok ( result ) ;
43
102
}
103
+
44
104
45
105
46
106
///<summary>
47
- ///Create a new Play Offer
107
+ ///Create a new Play Offer for the logged in user
48
108
///</summary>
49
- ///<param name="playOfferDto ">The Play Offer to create</param>
109
+ ///<param name="createPlayOfferDto ">The Play Offer to create</param>
50
110
///<returns>The newly created Play offer</returns>
51
111
///<response code="200">Returns the id of the created Play Offer</response>
52
112
///<response code="400">Invalid Play Offer structure</response>
113
+ ///<response code="401">Only members can create Play Offers</response>
53
114
[ HttpPost ]
115
+ [ Authorize ]
54
116
[ ProducesResponseType ( typeof ( PlayOffer ) , StatusCodes . Status201Created ) ]
55
117
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status400BadRequest ) ]
56
118
[ Consumes ( "application/json" ) ]
57
119
[ Produces ( "application/json" ) ]
58
- public async Task < ActionResult < PlayOffer > > Create ( PlayOfferDto playOfferDto )
120
+ public async Task < ActionResult < PlayOffer > > Create ( CreatePlayOfferDto createPlayOfferDto )
59
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
+
60
128
Guid result ;
61
129
try
62
130
{
63
- result = await _mediator . Send ( new CreatePlayOfferCommand ( playOfferDto ) ) ;
131
+ result = await _mediator . Send ( new CreatePlayOfferCommand ( createPlayOfferDto , creatorId , clubId ) ) ;
64
132
}
65
133
catch ( Exception e )
66
134
{
@@ -71,22 +139,33 @@ public async Task<ActionResult<PlayOffer>> Create(PlayOfferDto playOfferDto)
71
139
}
72
140
73
141
///<summary>
74
- ///Cancels a Play Offer with a matching id
142
+ ///Cancels a Play Offer with a matching id of the logged in user
75
143
///</summary>
76
144
///<param name="playOfferId">The id of the Play Offer to cancel</param>
77
145
///<returns>Nothing</returns>
78
146
///<response code="200">The Play Offer with the matching id was cancelled</response>
79
147
///<response code="400">No Play Offer with matching id found</response>
148
+ ///<response code="401">Only creator can cancel Play Offers</response>
80
149
[ HttpDelete ]
150
+ [ Authorize ]
81
151
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status200OK ) ]
82
152
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status400BadRequest ) ]
83
153
[ Consumes ( "application/json" ) ]
84
154
[ Produces ( "application/json" ) ]
85
155
public async Task < ActionResult > Delete ( Guid playOfferId )
86
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
+
87
162
try
88
163
{
89
- 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 ) ;
90
169
}
91
170
catch ( Exception e )
92
171
{
@@ -97,23 +176,29 @@ public async Task<ActionResult> Delete(Guid playOfferId)
97
176
}
98
177
99
178
///<summary>
100
- ///Adds a given opponentId to a Play Offer and creates a reservation
179
+ ///Logged in user joins a Play Offer with a matching playOfferId
101
180
///</summary>
102
181
///<param name="joinPlayOfferDto">The opponentId to add to the Play Offer with the matching playOfferId</param>
103
182
///<returns>Nothing</returns>
104
183
///<response code="200">The opponentId was added to the Play Offer with the matching playOfferId</response>
105
184
///<response code="400">No playOffer with a matching playOfferId found</response>
185
+ ///<response code="401">Only members can join Play Offers</response>
106
186
[ HttpPost ]
107
- [ Route ( "/join" ) ]
187
+ [ Authorize ]
188
+ [ Route ( "join" ) ]
108
189
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status200OK ) ]
109
190
[ ProducesResponseType ( typeof ( ActionResult ) , StatusCodes . Status400BadRequest ) ]
110
191
[ Consumes ( "application/json" ) ]
111
192
[ Produces ( "application/json" ) ]
112
193
public async Task < ActionResult > Join ( JoinPlayOfferDto joinPlayOfferDto )
113
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 ) ;
114
199
try
115
200
{
116
- await _mediator . Send ( new JoinPlayOfferCommand ( joinPlayOfferDto ) ) ;
201
+ await _mediator . Send ( new JoinPlayOfferCommand ( joinPlayOfferDto , memberId ) ) ;
117
202
}
118
203
catch ( Exception e )
119
204
{
0 commit comments