|
1 | | -namespace BestWeatherForecast.Api._2023_06_06.Controllers |
| 1 | +namespace BestWeatherForecast.Api._2023_06_06.Controllers; |
| 2 | + |
| 3 | +using Asp.Versioning; |
| 4 | +using BestWeatherForecast.Application.WeatherForcast; |
| 5 | +using BestWeatherForecast.Domain; |
| 6 | +using Mapster; |
| 7 | +using Mediator; |
| 8 | +using Microsoft.AspNetCore.Mvc; |
| 9 | +using ServiceLevelIndicators; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Weather forecast controller. |
| 13 | +/// </summary> |
| 14 | +[ApiController] |
| 15 | +[ApiVersion("2023-06-06")] |
| 16 | +[Consumes("application/json")] |
| 17 | +[Produces("application/json")] |
| 18 | +[Route("api/[controller]")] |
| 19 | +public class WeatherForecastController : ControllerBase |
2 | 20 | { |
3 | | - using Asp.Versioning; |
4 | | - using BestWeatherForecast.Application.WeatherForcast; |
5 | | - using BestWeatherForecast.Domain; |
6 | | - using Mapster; |
7 | | - using Mediator; |
8 | | - using Microsoft.AspNetCore.Mvc; |
9 | | - using ServiceLevelIndicators; |
| 21 | + private readonly ISender _sender; |
10 | 22 |
|
11 | 23 | /// <summary> |
12 | | - /// Weather forecast controller. |
| 24 | + /// Constructor |
13 | 25 | /// </summary> |
14 | | - [ApiController] |
15 | | - [ApiVersion("2023-06-06")] |
16 | | - [Consumes("application/json")] |
17 | | - [Produces("application/json")] |
18 | | - [Route("api/[controller]")] |
19 | | - public class WeatherForecastController : ControllerBase |
20 | | - { |
21 | | - private readonly ISender _sender; |
| 26 | + /// <param name="sender"></param> |
| 27 | + public WeatherForecastController(ISender sender) => _sender = sender; |
22 | 28 |
|
23 | | - /// <summary> |
24 | | - /// Constructor |
25 | | - /// </summary> |
26 | | - /// <param name="sender"></param> |
27 | | - public WeatherForecastController(ISender sender) => _sender = sender; |
28 | | - |
29 | | - /// <summary> |
30 | | - /// Get the weather forecast for Redmond,WA. |
31 | | - /// </summary> |
32 | | - /// <param name="cancellationToken">Cancellation Token.</param> |
33 | | - /// <returns></returns> |
34 | | - [HttpGet] |
35 | | - [ProducesResponseType(typeof(Models.WeatherForecast), StatusCodes.Status200OK)] |
36 | | - [ProducesResponseType(StatusCodes.Status400BadRequest)] |
37 | | - [ProducesResponseType(StatusCodes.Status404NotFound)] |
38 | | - public async ValueTask<ActionResult<Models.WeatherForecast>> GetRedmond(CancellationToken cancellationToken) |
39 | | - => await ZipCode.TryCreate("98052") |
40 | | - .Bind(static zipCode => WeatherForecastQuery.TryCreate(zipCode)) |
41 | | - .BindAsync(q => _sender.Send(q, cancellationToken)) |
42 | | - .MapAsync(r => r.Adapt<Models.WeatherForecast>()) |
43 | | - .ToActionResultAsync(this); |
| 29 | + /// <summary> |
| 30 | + /// Get the weather forecast for Redmond,WA. |
| 31 | + /// </summary> |
| 32 | + /// <param name="cancellationToken">Cancellation Token.</param> |
| 33 | + /// <returns></returns> |
| 34 | + [HttpGet] |
| 35 | + [ProducesResponseType(typeof(Models.WeatherForecast), StatusCodes.Status200OK)] |
| 36 | + [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| 37 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 38 | + public async ValueTask<ActionResult<Models.WeatherForecast>> GetRedmond(CancellationToken cancellationToken) |
| 39 | + => await ZipCode.TryCreate("98052") |
| 40 | + .Bind(static zipCode => WeatherForecastQuery.TryCreate(zipCode)) |
| 41 | + .BindAsync(q => _sender.Send(q, cancellationToken)) |
| 42 | + .MapAsync(r => r.Adapt<Models.WeatherForecast>()) |
| 43 | + .ToActionResultAsync(this); |
44 | 44 |
|
45 | | - /// <summary> |
46 | | - /// Get the weather forecast for the given zip code. |
47 | | - /// </summary> |
48 | | - /// <param name="zipCode">Zip code.</param> |
49 | | - /// <param name="cancellationToken">Cancellation Token.</param> |
50 | | - /// <returns></returns> |
51 | | - [HttpGet("{zipCode}")] |
52 | | - [ProducesResponseType(typeof(Models.WeatherForecast), StatusCodes.Status200OK)] |
53 | | - [ProducesResponseType(StatusCodes.Status400BadRequest)] |
54 | | - [ProducesResponseType(StatusCodes.Status404NotFound)] |
55 | | - public async ValueTask<ActionResult<Models.WeatherForecast>> Get([CustomerResourceId] string zipCode, CancellationToken cancellationToken) |
56 | | - => await ZipCode.TryCreate(zipCode) |
57 | | - .Bind(static zipCode => WeatherForecastQuery.TryCreate(zipCode)) |
58 | | - .BindAsync(q => _sender.Send(q, cancellationToken)) |
59 | | - .MapAsync(r => r.Adapt<Models.WeatherForecast>()) |
60 | | - .ToActionResultAsync(this); |
| 45 | + /// <summary> |
| 46 | + /// Get the weather forecast for the given zip code. |
| 47 | + /// </summary> |
| 48 | + /// <param name="zipCode">Zip code.</param> |
| 49 | + /// <param name="cancellationToken">Cancellation Token.</param> |
| 50 | + /// <returns></returns> |
| 51 | + [HttpGet("{zipCode}")] |
| 52 | + [ProducesResponseType(typeof(Models.WeatherForecast), StatusCodes.Status200OK)] |
| 53 | + [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| 54 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 55 | + public async ValueTask<ActionResult<Models.WeatherForecast>> Get([CustomerResourceId] string zipCode, CancellationToken cancellationToken) |
| 56 | + => await ZipCode.TryCreate(zipCode) |
| 57 | + .Bind(static zipCode => WeatherForecastQuery.TryCreate(zipCode)) |
| 58 | + .BindAsync(q => _sender.Send(q, cancellationToken)) |
| 59 | + .MapAsync(r => r.Adapt<Models.WeatherForecast>()) |
| 60 | + .ToActionResultAsync(this); |
61 | 61 |
|
62 | | - /// <summary> |
63 | | - /// This method throws to show the error handling middleware handles it. |
64 | | - /// </summary> |
65 | | - /// <returns></returns> |
66 | | - /// <exception cref="NotImplementedException"></exception> |
67 | | - [HttpGet("Throw")] |
68 | | - public string Throw() |
69 | | - { |
70 | | - throw new NotImplementedException("Catch me middleware."); |
71 | | - } |
| 62 | + /// <summary> |
| 63 | + /// This method throws to show the error handling middleware handles it. |
| 64 | + /// </summary> |
| 65 | + /// <returns></returns> |
| 66 | + /// <exception cref="NotImplementedException"></exception> |
| 67 | + [HttpGet("Throw")] |
| 68 | + public string Throw() |
| 69 | + { |
| 70 | + throw new NotImplementedException("Catch me middleware."); |
72 | 71 | } |
73 | 72 | } |
0 commit comments