|
7 | 7 | using System.Linq; |
8 | 8 | using System.Threading.Tasks; |
9 | 9 |
|
10 | | -namespace Ardalis.Result.Sample.Core.Services |
| 10 | +namespace Ardalis.Result.Sample.Core.Services; |
| 11 | +public class WeatherServiceWithExceptions |
11 | 12 | { |
12 | | - public class WeatherServiceWithExceptions |
| 13 | + public WeatherServiceWithExceptions(IStringLocalizer<WeatherService> stringLocalizer) |
13 | 14 | { |
14 | | - public WeatherServiceWithExceptions(IStringLocalizer<WeatherService> stringLocalizer) |
15 | | - { |
16 | | - _stringLocalizer = stringLocalizer; |
17 | | - } |
| 15 | + _stringLocalizer = stringLocalizer; |
| 16 | + } |
18 | 17 |
|
19 | | - private static readonly string[] Summaries = new[] |
| 18 | + private static readonly string[] Summaries = new[] |
20 | 19 | { |
21 | | - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" |
22 | | - }; |
| 20 | + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" |
| 21 | + }; |
23 | 22 |
|
24 | | - private IStringLocalizer<WeatherService> _stringLocalizer; |
| 23 | + private IStringLocalizer<WeatherService> _stringLocalizer; |
25 | 24 |
|
26 | | - public Task<IEnumerable<WeatherForecast>> GetForecastAsync(ForecastRequestDto model) |
| 25 | + public Task<IEnumerable<WeatherForecast>> GetForecastAsync(ForecastRequestDto model) |
| 26 | + { |
| 27 | + return Task.FromResult(GetForecast(model)); |
| 28 | + } |
| 29 | + |
| 30 | + public IEnumerable<WeatherForecast> GetForecast(ForecastRequestDto model) |
| 31 | + { |
| 32 | + switch (model.PostalCode) |
27 | 33 | { |
28 | | - return Task.FromResult(GetForecast(model)); |
| 34 | + case "NotFound": |
| 35 | + throw new ForecastNotFoundException(); |
| 36 | + case "Conflict": |
| 37 | + throw new ForecastConflictException(); |
29 | 38 | } |
30 | 39 |
|
31 | | - public IEnumerable<WeatherForecast> GetForecast(ForecastRequestDto model) |
| 40 | + // validate model |
| 41 | + if (model.PostalCode.Length > 10) |
32 | 42 | { |
33 | | - switch (model.PostalCode) |
| 43 | + throw new ForecastRequestInvalidException(new Dictionary<string, string>() |
34 | 44 | { |
35 | | - case "NotFound": |
36 | | - throw new ForecastNotFoundException(); |
37 | | - case "Conflict": |
38 | | - throw new ForecastConflictException(); |
39 | | - } |
| 45 | + { nameof(model.PostalCode), _stringLocalizer["PostalCode cannot exceed 10 characters."].Value } |
| 46 | + }); |
| 47 | + } |
40 | 48 |
|
41 | | - // validate model |
42 | | - if (model.PostalCode.Length > 10) |
| 49 | + // test single result value |
| 50 | + if (model.PostalCode == "55555") |
| 51 | + { |
| 52 | + return new List<WeatherForecast> |
43 | 53 | { |
44 | | - throw new ForecastRequestInvalidException(new Dictionary<string, string>() |
| 54 | + new WeatherForecast |
45 | 55 | { |
46 | | - { nameof(model.PostalCode), _stringLocalizer["PostalCode cannot exceed 10 characters."].Value } |
47 | | - }); |
48 | | - } |
| 56 | + Date = DateTime.Now, |
| 57 | + TemperatureC = 0, |
| 58 | + Summary = Summaries[0] |
| 59 | + } |
| 60 | + }; |
| 61 | + } |
49 | 62 |
|
50 | | - // test single result value |
51 | | - if (model.PostalCode == "55555") |
| 63 | + var rng = new Random(); |
| 64 | + return new List<WeatherForecast>(Enumerable.Range(1, 5) |
| 65 | + .Select(index => new WeatherForecast |
52 | 66 | { |
53 | | - return new List<WeatherForecast> |
54 | | - { |
55 | | - new WeatherForecast |
56 | | - { |
57 | | - Date = DateTime.Now, |
58 | | - TemperatureC = 0, |
59 | | - Summary = Summaries[0] |
60 | | - } |
61 | | - }; |
62 | | - } |
63 | | - |
64 | | - var rng = new Random(); |
65 | | - return new List<WeatherForecast>(Enumerable.Range(1, 5) |
66 | | - .Select(index => new WeatherForecast |
67 | | - { |
68 | | - Date = DateTime.Now.AddDays(index), |
69 | | - TemperatureC = rng.Next(-20, 55), |
70 | | - Summary = Summaries[rng.Next(Summaries.Length)] |
71 | | - }) |
72 | | - .ToArray()); |
73 | | - } |
| 67 | + Date = DateTime.Now.AddDays(index), |
| 68 | + TemperatureC = rng.Next(-20, 55), |
| 69 | + Summary = Summaries[rng.Next(Summaries.Length)] |
| 70 | + }) |
| 71 | + .ToArray()); |
74 | 72 | } |
75 | 73 | } |
0 commit comments