-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
SystemTextJsonPatch.ApiTest/Controllers/WeatherController.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,62 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace SystemTextJsonPatch.ApiTest.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
public class WeatherController : ControllerBase | ||
{ | ||
private readonly WeatherForecast[] _weatherForecasts = new WeatherForecast[] | ||
{ | ||
new WeatherForecast { Date = DateTime.Now, TemperatureC = 25, Summary = "Hot" }, | ||
new WeatherForecast { Date = DateTime.Now.AddDays(1), TemperatureC = 20, Summary = "Warm" }, | ||
new WeatherForecast { Date = DateTime.Now.AddDays(2), TemperatureC = 15, Summary = "Cool" }, | ||
new WeatherForecast { Date = DateTime.Now.AddDays(3), TemperatureC = 10, Summary = "Cold" }, | ||
new WeatherForecast { Date = DateTime.Now.AddDays(4), TemperatureC = 5, Summary = "Freezing" } | ||
}; | ||
|
||
[HttpGet] | ||
public IActionResult GetWeather() | ||
{ | ||
return Ok(_weatherForecasts); | ||
} | ||
|
||
[HttpPost] | ||
public ActionResult<WeatherForecast> PostWeather([FromBody, Required] WeatherForecast? weatherForecast) | ||
{ | ||
if (weatherForecast == null || ModelState.IsValid == false) | ||
{ | ||
return ValidationProblem(this.ModelState); | ||
} | ||
|
||
return CreatedAtAction(nameof(GetWeather), weatherForecast); | ||
} | ||
|
||
[HttpPatch("{id}")] | ||
public IActionResult PatchWeather(int id, [FromBody, Required] JsonPatchDocument<WeatherForecast>? patchDoc) | ||
{ | ||
var weatherForecast = _weatherForecasts[id]; | ||
if (weatherForecast == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
patchDoc.ApplyTo(weatherForecast); | ||
TryValidateModel(weatherForecast); | ||
if (ModelState.IsValid == false) | ||
{ | ||
return ValidationProblem(this.ModelState); | ||
} | ||
|
||
return Ok(); | ||
} | ||
} | ||
|
||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
[Required] | ||
public int? TemperatureC { 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,12 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddControllers(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.MapControllers(); | ||
app.UseHttpsRedirection(); | ||
|
||
app.Run(); |
25 changes: 25 additions & 0 deletions
25
SystemTextJsonPatch.ApiTest/Properties/launchSettings.json
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,25 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "weatherforecast", | ||
"applicationUrl": "http://localhost:5290", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "weatherforecast", | ||
"applicationUrl": "https://localhost:7027;http://localhost:5290", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
SystemTextJsonPatch.ApiTest/SystemTextJsonPatch.ApiTest.csproj
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 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SystemTextJsonPatch\SystemTextJsonPatch.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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": "*" | ||
} |
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