forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add TryParse samples * Add TryParse samples
- Loading branch information
1 parent
c3cb891
commit 25c92da
Showing
10 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-preview.5.22303.8" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
73 changes: 73 additions & 0 deletions
73
mvc/models/BindTryParseAPI/Controllers/WeatherForecastController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Globalization; | ||
using BindTryParseAPI.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace BindTryParseAPI.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
private readonly ILogger<WeatherForecastController> _logger; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
// <snippet2> | ||
// GET /WeatherForecast?culture=en-GB | ||
[HttpGet] | ||
public IActionResult Get([FromQuery] Culture? culture) | ||
{ | ||
var weatherForecasts = Enumerable | ||
.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateTime.Now.AddDays(index), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
}) | ||
.Select(wf => new WeatherForecastViewModel | ||
{ | ||
Date = wf.Date.ToString(new CultureInfo(culture?.DisplayName ?? "en-US")), | ||
TemperatureC = wf.TemperatureC, | ||
TemperatureF = wf.TemperatureF, | ||
Summary = wf.Summary | ||
}); | ||
|
||
return Ok(weatherForecasts); | ||
} | ||
// </snippet2> | ||
|
||
// <snippet> | ||
// GET /WeatherForecast/GetByRange?range=07/12/2022-07/14/2022 | ||
[HttpGet] | ||
[Route("GetByRange")] | ||
public IActionResult Range([FromQuery] DateRange? range) | ||
{ | ||
var weatherForecasts = Enumerable | ||
.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateTime.Now.AddDays(index), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
}) | ||
.Where(wf => DateOnly.FromDateTime(wf.Date) >= (range?.From ?? DateOnly.MinValue) && DateOnly.FromDateTime(wf.Date) <= (range?.To ?? DateOnly.MaxValue)) | ||
.Select(wf => new WeatherForecastViewModel | ||
{ | ||
Date = wf.Date.ToString(), | ||
TemperatureC = wf.TemperatureC, | ||
TemperatureF = wf.TemperatureF, | ||
Summary = wf.Summary | ||
}); | ||
|
||
return Ok(weatherForecasts); | ||
} | ||
// </snippet> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace BindTryParseAPI.Models | ||
{ | ||
// <snippet> | ||
public class Culture | ||
{ | ||
public string? DisplayName { get; } | ||
|
||
public Culture(string displayName) | ||
{ | ||
if (string.IsNullOrEmpty(displayName)) | ||
throw new ArgumentNullException(nameof(displayName)); | ||
|
||
DisplayName = displayName; | ||
} | ||
|
||
public static bool TryParse(string? value, out Culture? result) | ||
{ | ||
if (value is null) | ||
{ | ||
result = default; | ||
return false; | ||
} | ||
|
||
result = new Culture(value); | ||
return true; | ||
} | ||
} | ||
// </snippet> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace BindTryParseAPI.Models | ||
{ | ||
// <snippet> | ||
public class DateRange | ||
{ | ||
public DateOnly? From { get; } | ||
public DateOnly? To { get; } | ||
|
||
public DateRange(string from, string to) | ||
{ | ||
if (string.IsNullOrEmpty(from)) | ||
throw new ArgumentNullException(nameof(from)); | ||
if (string.IsNullOrEmpty(to)) | ||
throw new ArgumentNullException(nameof(to)); | ||
|
||
From = DateOnly.Parse(from); | ||
To = DateOnly.Parse(to); | ||
} | ||
|
||
public static bool TryParse(string? value, IFormatProvider? provider, out DateRange? result) | ||
{ | ||
if (string.IsNullOrEmpty(value) || value.Split('-').Length != 2) | ||
{ | ||
result = default; | ||
return false; | ||
} | ||
|
||
var range = value.Split('-'); | ||
result = new DateRange(range[0], range[1]); | ||
return true; | ||
} | ||
} | ||
// </snippet> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace BindTryParseAPI.Models | ||
{ | ||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
|
||
public string? Summary { get; set; } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
mvc/models/BindTryParseAPI/Models/WeatherForecastViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace BindTryParseAPI.Models | ||
{ | ||
public class WeatherForecastViewModel | ||
{ | ||
public string? Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF { get; set; } | ||
|
||
public string? Summary { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddControllers(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace BindTryParseAPI | ||
{ | ||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
|
||
public string? Summary { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |