Skip to content

Commit 25c92da

Browse files
Tryparse (dotnet#5)
* Add TryParse samples * Add TryParse samples
1 parent c3cb891 commit 25c92da

10 files changed

+217
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-preview.5.22303.8" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Globalization;
2+
using BindTryParseAPI.Models;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace BindTryParseAPI.Controllers
6+
{
7+
[ApiController]
8+
[Route("[controller]")]
9+
public class WeatherForecastController : ControllerBase
10+
{
11+
private static readonly string[] Summaries = new[]
12+
{
13+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
14+
};
15+
16+
private readonly ILogger<WeatherForecastController> _logger;
17+
18+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
19+
{
20+
_logger = logger;
21+
}
22+
23+
// <snippet2>
24+
// GET /WeatherForecast?culture=en-GB
25+
[HttpGet]
26+
public IActionResult Get([FromQuery] Culture? culture)
27+
{
28+
var weatherForecasts = Enumerable
29+
.Range(1, 5).Select(index => new WeatherForecast
30+
{
31+
Date = DateTime.Now.AddDays(index),
32+
TemperatureC = Random.Shared.Next(-20, 55),
33+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
34+
})
35+
.Select(wf => new WeatherForecastViewModel
36+
{
37+
Date = wf.Date.ToString(new CultureInfo(culture?.DisplayName ?? "en-US")),
38+
TemperatureC = wf.TemperatureC,
39+
TemperatureF = wf.TemperatureF,
40+
Summary = wf.Summary
41+
});
42+
43+
return Ok(weatherForecasts);
44+
}
45+
// </snippet2>
46+
47+
// <snippet>
48+
// GET /WeatherForecast/GetByRange?range=07/12/2022-07/14/2022
49+
[HttpGet]
50+
[Route("GetByRange")]
51+
public IActionResult Range([FromQuery] DateRange? range)
52+
{
53+
var weatherForecasts = Enumerable
54+
.Range(1, 5).Select(index => new WeatherForecast
55+
{
56+
Date = DateTime.Now.AddDays(index),
57+
TemperatureC = Random.Shared.Next(-20, 55),
58+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
59+
})
60+
.Where(wf => DateOnly.FromDateTime(wf.Date) >= (range?.From ?? DateOnly.MinValue) && DateOnly.FromDateTime(wf.Date) <= (range?.To ?? DateOnly.MaxValue))
61+
.Select(wf => new WeatherForecastViewModel
62+
{
63+
Date = wf.Date.ToString(),
64+
TemperatureC = wf.TemperatureC,
65+
TemperatureF = wf.TemperatureF,
66+
Summary = wf.Summary
67+
});
68+
69+
return Ok(weatherForecasts);
70+
}
71+
// </snippet>
72+
}
73+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace BindTryParseAPI.Models
2+
{
3+
// <snippet>
4+
public class Culture
5+
{
6+
public string? DisplayName { get; }
7+
8+
public Culture(string displayName)
9+
{
10+
if (string.IsNullOrEmpty(displayName))
11+
throw new ArgumentNullException(nameof(displayName));
12+
13+
DisplayName = displayName;
14+
}
15+
16+
public static bool TryParse(string? value, out Culture? result)
17+
{
18+
if (value is null)
19+
{
20+
result = default;
21+
return false;
22+
}
23+
24+
result = new Culture(value);
25+
return true;
26+
}
27+
}
28+
// </snippet>
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace BindTryParseAPI.Models
2+
{
3+
// <snippet>
4+
public class DateRange
5+
{
6+
public DateOnly? From { get; }
7+
public DateOnly? To { get; }
8+
9+
public DateRange(string from, string to)
10+
{
11+
if (string.IsNullOrEmpty(from))
12+
throw new ArgumentNullException(nameof(from));
13+
if (string.IsNullOrEmpty(to))
14+
throw new ArgumentNullException(nameof(to));
15+
16+
From = DateOnly.Parse(from);
17+
To = DateOnly.Parse(to);
18+
}
19+
20+
public static bool TryParse(string? value, IFormatProvider? provider, out DateRange? result)
21+
{
22+
if (string.IsNullOrEmpty(value) || value.Split('-').Length != 2)
23+
{
24+
result = default;
25+
return false;
26+
}
27+
28+
var range = value.Split('-');
29+
result = new DateRange(range[0], range[1]);
30+
return true;
31+
}
32+
}
33+
// </snippet>
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BindTryParseAPI.Models
2+
{
3+
public class WeatherForecast
4+
{
5+
public DateTime Date { get; set; }
6+
7+
public int TemperatureC { get; set; }
8+
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
10+
11+
public string? Summary { get; set; }
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BindTryParseAPI.Models
2+
{
3+
public class WeatherForecastViewModel
4+
{
5+
public string? Date { get; set; }
6+
7+
public int TemperatureC { get; set; }
8+
9+
public int TemperatureF { get; set; }
10+
11+
public string? Summary { get; set; }
12+
}
13+
}

mvc/models/BindTryParseAPI/Program.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
builder.Services.AddControllers();
4+
5+
var app = builder.Build();
6+
7+
app.UseAuthorization();
8+
9+
app.MapControllers();
10+
11+
app.Run();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BindTryParseAPI
2+
{
3+
public class WeatherForecast
4+
{
5+
public DateTime Date { get; set; }
6+
7+
public int TemperatureC { get; set; }
8+
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
10+
11+
public string? Summary { get; set; }
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)