Skip to content

Commit

Permalink
Tryparse (dotnet#5)
Browse files Browse the repository at this point in the history
* Add TryParse samples

* Add TryParse samples
  • Loading branch information
Rick-Anderson authored Jul 14, 2022
1 parent c3cb891 commit 25c92da
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 0 deletions.
14 changes: 14 additions & 0 deletions mvc/models/BindTryParseAPI/BindTryParseAPI.csproj
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>
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>
}
}
29 changes: 29 additions & 0 deletions mvc/models/BindTryParseAPI/Models/Culture.cs
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>
}
34 changes: 34 additions & 0 deletions mvc/models/BindTryParseAPI/Models/DateRange.cs
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>
}
13 changes: 13 additions & 0 deletions mvc/models/BindTryParseAPI/Models/WeatherForecast.cs
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 mvc/models/BindTryParseAPI/Models/WeatherForecastViewModel.cs
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; }
}
}
11 changes: 11 additions & 0 deletions mvc/models/BindTryParseAPI/Program.cs
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();
13 changes: 13 additions & 0 deletions mvc/models/BindTryParseAPI/WeatherForecast.cs
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; }
}
}
8 changes: 8 additions & 0 deletions mvc/models/BindTryParseAPI/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions mvc/models/BindTryParseAPI/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

0 comments on commit 25c92da

Please sign in to comment.