diff --git a/mvc/models/BindTryParseAPI/BindTryParseAPI.csproj b/mvc/models/BindTryParseAPI/BindTryParseAPI.csproj new file mode 100644 index 000000000000..859a73ce0330 --- /dev/null +++ b/mvc/models/BindTryParseAPI/BindTryParseAPI.csproj @@ -0,0 +1,14 @@ + + + + net7.0 + enable + enable + + + + + + + + diff --git a/mvc/models/BindTryParseAPI/Controllers/WeatherForecastController.cs b/mvc/models/BindTryParseAPI/Controllers/WeatherForecastController.cs new file mode 100644 index 000000000000..a3f12a2731c7 --- /dev/null +++ b/mvc/models/BindTryParseAPI/Controllers/WeatherForecastController.cs @@ -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 _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + // + // 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); + } + // + + // + // 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); + } + // + } +} diff --git a/mvc/models/BindTryParseAPI/Models/Culture.cs b/mvc/models/BindTryParseAPI/Models/Culture.cs new file mode 100644 index 000000000000..9ac1f511c228 --- /dev/null +++ b/mvc/models/BindTryParseAPI/Models/Culture.cs @@ -0,0 +1,29 @@ +namespace BindTryParseAPI.Models +{ + // + 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; + } + } + // +} diff --git a/mvc/models/BindTryParseAPI/Models/DateRange.cs b/mvc/models/BindTryParseAPI/Models/DateRange.cs new file mode 100644 index 000000000000..f7d31596f2a5 --- /dev/null +++ b/mvc/models/BindTryParseAPI/Models/DateRange.cs @@ -0,0 +1,34 @@ +namespace BindTryParseAPI.Models +{ + // + 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; + } + } + // +} diff --git a/mvc/models/BindTryParseAPI/Models/WeatherForecast.cs b/mvc/models/BindTryParseAPI/Models/WeatherForecast.cs new file mode 100644 index 000000000000..4efa24008d7f --- /dev/null +++ b/mvc/models/BindTryParseAPI/Models/WeatherForecast.cs @@ -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; } + } +} diff --git a/mvc/models/BindTryParseAPI/Models/WeatherForecastViewModel.cs b/mvc/models/BindTryParseAPI/Models/WeatherForecastViewModel.cs new file mode 100644 index 000000000000..aa451aa44253 --- /dev/null +++ b/mvc/models/BindTryParseAPI/Models/WeatherForecastViewModel.cs @@ -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; } + } +} diff --git a/mvc/models/BindTryParseAPI/Program.cs b/mvc/models/BindTryParseAPI/Program.cs new file mode 100644 index 000000000000..0f532a000931 --- /dev/null +++ b/mvc/models/BindTryParseAPI/Program.cs @@ -0,0 +1,11 @@ +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddControllers(); + +var app = builder.Build(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/mvc/models/BindTryParseAPI/WeatherForecast.cs b/mvc/models/BindTryParseAPI/WeatherForecast.cs new file mode 100644 index 000000000000..703e779348b3 --- /dev/null +++ b/mvc/models/BindTryParseAPI/WeatherForecast.cs @@ -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; } + } +} \ No newline at end of file diff --git a/mvc/models/BindTryParseAPI/appsettings.Development.json b/mvc/models/BindTryParseAPI/appsettings.Development.json new file mode 100644 index 000000000000..0c208ae9181e --- /dev/null +++ b/mvc/models/BindTryParseAPI/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/mvc/models/BindTryParseAPI/appsettings.json b/mvc/models/BindTryParseAPI/appsettings.json new file mode 100644 index 000000000000..10f68b8c8b4f --- /dev/null +++ b/mvc/models/BindTryParseAPI/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}