-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayOfferController.cs
201 lines (178 loc) · 7.49 KB
/
PlayOfferController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System.Security.Claims;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PlayOfferService.Application.Commands;
using PlayOfferService.Application.Exceptions;
using PlayOfferService.Application.Queries;
using PlayOfferService.Domain.Models;
namespace PlayOfferService.Application.Controllers;
[ApiController]
[Route("api/playoffers")]
public class PlayOfferController : ControllerBase
{
private readonly IMediator _mediator;
public PlayOfferController(IMediator mediator)
{
_mediator = mediator;
}
/// <summary>
/// Retrieve all Play Offers of the logged in users club
/// </summary>
/// <returns>Play offers with a matching club id</returns>
/// <response code="200">Returns a list of Play offers matching the query params</response>
/// <response code="204">No Play offer with matching properties was found</response>
[HttpGet]
[Authorize(Roles = "MEMBER,ADMIN")]
[Route("club")]
[ProducesResponseType(typeof(IEnumerable<PlayOfferDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ActionResult), StatusCodes.Status204NoContent)]
[Consumes("application/json")]
[Produces("application/json")]
public async Task<ActionResult<IEnumerable<PlayOfferDto>>> GetByClubIdAsync()
{
var clubId = Guid.Parse(User.Claims.First(c => c.Type == "tennisClubId").Value);
var result = await _mediator.Send(new GetPlayOffersByClubIdQuery(clubId));
if (result.Count() == 0)
return NoContent();
return Ok(result);
}
///<summary>
///Retrieve all Play Offers of a logged in user
///</summary>
///<returns>List of Play offers with where given member is creator or opponent</returns>
///<response code="200">Returns a list of Play offers matching the query params</response>
///<response code="204">No Play offer with matching properties was found</response>
[HttpGet]
[Authorize(Roles = "MEMBER,ADMIN")]
[Route("participant")]
[ProducesResponseType(typeof(IEnumerable<PlayOffer>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ActionResult), StatusCodes.Status204NoContent)]
[Consumes("application/json")]
[Produces("application/json")]
public async Task<ActionResult<IEnumerable<PlayOfferDto>>> GetByParticipantIdAsync()
{
var participantId = Guid.Parse(User.FindFirst("sub")!.Value);
var result = await _mediator.Send(new GetPlayOffersByParticipantIdQuery(participantId));
if (result.Count() == 0)
return NoContent();
return Ok(result);
}
///<summary>
///Get all Play offers created by a member with a matching name
///</summary>
///<param name="creatorName">Name of the creator in the format '[FirstName] [LastName]', '[FirstName]' or '[LastName]'</param>
///<returns>A list of Play offers with a matching id</returns>
///<response code="200">Returns a List of Play offers with creator matching the query params</response>
///<response code="204">No Play offers with matching creator was found</response>
[HttpGet]
[Authorize(Roles = "MEMBER,ADMIN")]
[Route("search")]
[ProducesResponseType(typeof(IEnumerable<PlayOffer>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ActionResult), StatusCodes.Status204NoContent)]
[Consumes("application/json")]
[Produces("application/json")]
public async Task<ActionResult<IEnumerable<PlayOfferDto>>> GetByCreatorNameAsync([FromQuery] string creatorName)
{
IEnumerable<PlayOfferDto> result;
try
{
result = await _mediator.Send(new GetPlayOffersByCreatorNameQuery(creatorName));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
if (result.Count() == 0)
return NoContent();
return Ok(result);
}
///<summary>
///Create a new Play Offer for the logged in user
///</summary>
///<param name="createPlayOfferDto">The Play Offer to create</param>
///<returns>The newly created Play offer</returns>
///<response code="200">Returns the id of the created Play Offer</response>
///<response code="400">Invalid Play Offer structure</response>
///<response code="401">Only members can create Play Offers</response>
[HttpPost]
[Authorize(Roles = "MEMBER")]
[ProducesResponseType(typeof(PlayOffer), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ActionResult), StatusCodes.Status400BadRequest)]
[Consumes("application/json")]
[Produces("application/json")]
public async Task<ActionResult<PlayOffer>> Create(CreatePlayOfferDto createPlayOfferDto)
{
var creatorId = Guid.Parse(User.FindFirst("sub")!.Value);
var clubId = Guid.Parse(User.FindFirst("tennisClubId")!.Value);
Guid result;
try
{
result = await _mediator.Send(new CreatePlayOfferCommand(createPlayOfferDto, creatorId, clubId));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
return CreatedAtAction(nameof(Create), new { playOfferId = result }, result);
}
///<summary>
///Cancels a Play Offer with a matching id of the logged in user
///</summary>
///<param name="playOfferId">The id of the Play Offer to cancel</param>
///<returns>Nothing</returns>
///<response code="200">The Play Offer with the matching id was cancelled</response>
///<response code="400">No Play Offer with matching id found</response>
///<response code="401">Only creator can cancel Play Offers</response>
[HttpDelete]
[Authorize(Roles = "MEMBER")]
[ProducesResponseType(typeof(ActionResult), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ActionResult), StatusCodes.Status400BadRequest)]
[Consumes("application/json")]
[Produces("application/json")]
public async Task<ActionResult> Delete(Guid playOfferId)
{
var memberId = Guid.Parse(User.FindFirst("sub")!.Value);
try
{
await _mediator.Send(new CancelPlayOfferCommand(playOfferId, memberId));
}
catch (AuthorizationException e)
{
return Unauthorized(e.Message);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
return Ok();
}
///<summary>
///Logged in user joins a Play Offer with a matching playOfferId
///</summary>
///<param name="joinPlayOfferDto">The opponentId to add to the Play Offer with the matching playOfferId</param>
///<returns>Nothing</returns>
///<response code="200">The opponentId was added to the Play Offer with the matching playOfferId</response>
///<response code="400">No playOffer with a matching playOfferId found</response>
///<response code="401">Only members can join Play Offers</response>
[HttpPost]
[Authorize(Roles = "MEMBER")]
[Route("join")]
[ProducesResponseType(typeof(ActionResult), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ActionResult), StatusCodes.Status400BadRequest)]
[Consumes("application/json")]
[Produces("application/json")]
public async Task<ActionResult> Join(JoinPlayOfferDto joinPlayOfferDto)
{
var memberId = Guid.Parse(User.FindFirst("sub")!.Value);
try
{
await _mediator.Send(new JoinPlayOfferCommand(joinPlayOfferDto, memberId));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
return Ok();
}
}