Skip to content

Commit f61fd2c

Browse files
committed
Added example web api
1 parent ef1852f commit f61fd2c

7 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace SystemTextJsonPatch.ApiTest.Controllers
5+
{
6+
[Route("api/[controller]")]
7+
public class WeatherController : ControllerBase
8+
{
9+
private readonly WeatherForecast[] _weatherForecasts = new WeatherForecast[]
10+
{
11+
new WeatherForecast { Date = DateTime.Now, TemperatureC = 25, Summary = "Hot" },
12+
new WeatherForecast { Date = DateTime.Now.AddDays(1), TemperatureC = 20, Summary = "Warm" },
13+
new WeatherForecast { Date = DateTime.Now.AddDays(2), TemperatureC = 15, Summary = "Cool" },
14+
new WeatherForecast { Date = DateTime.Now.AddDays(3), TemperatureC = 10, Summary = "Cold" },
15+
new WeatherForecast { Date = DateTime.Now.AddDays(4), TemperatureC = 5, Summary = "Freezing" }
16+
};
17+
18+
[HttpGet]
19+
public IActionResult GetWeather()
20+
{
21+
return Ok(_weatherForecasts);
22+
}
23+
24+
[HttpPost]
25+
public ActionResult<WeatherForecast> PostWeather([FromBody, Required] WeatherForecast? weatherForecast)
26+
{
27+
if (weatherForecast == null || ModelState.IsValid == false)
28+
{
29+
return ValidationProblem(this.ModelState);
30+
}
31+
32+
return CreatedAtAction(nameof(GetWeather), weatherForecast);
33+
}
34+
35+
[HttpPatch("{id}")]
36+
public IActionResult PatchWeather(int id, [FromBody, Required] JsonPatchDocument<WeatherForecast>? patchDoc)
37+
{
38+
var weatherForecast = _weatherForecasts[id];
39+
if (weatherForecast == null)
40+
{
41+
return NotFound();
42+
}
43+
44+
patchDoc.ApplyTo(weatherForecast);
45+
TryValidateModel(weatherForecast);
46+
if (ModelState.IsValid == false)
47+
{
48+
return ValidationProblem(this.ModelState);
49+
}
50+
51+
return Ok();
52+
}
53+
}
54+
55+
public class WeatherForecast
56+
{
57+
public DateTime Date { get; set; }
58+
[Required]
59+
public int? TemperatureC { get; set; }
60+
public string Summary { get; set; }
61+
}
62+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
builder.Services.AddControllers();
6+
7+
var app = builder.Build();
8+
9+
app.MapControllers();
10+
app.UseHttpsRedirection();
11+
12+
app.Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"launchUrl": "weatherforecast",
9+
"applicationUrl": "http://localhost:5290",
10+
"environmentVariables": {
11+
"ASPNETCORE_ENVIRONMENT": "Development"
12+
}
13+
},
14+
"https": {
15+
"commandName": "Project",
16+
"dotnetRunMessages": true,
17+
"launchBrowser": true,
18+
"launchUrl": "weatherforecast",
19+
"applicationUrl": "https://localhost:7027;http://localhost:5290",
20+
"environmentVariables": {
21+
"ASPNETCORE_ENVIRONMENT": "Development"
22+
}
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\SystemTextJsonPatch\SystemTextJsonPatch.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
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+
}
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+
}

SystemTextJsonPatch.sln

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SystemTextJsonPatch.Benchma
1818
EndProject
1919
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SystemTextJsonPatch.Console", "SystemTextJsonPatch.Console\SystemTextJsonPatch.Console.csproj", "{4DA8A38B-D595-4A5E-962A-C382EA7D8906}"
2020
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SystemTextJsonPatch.ApiTest", "SystemTextJsonPatch.ApiTest\SystemTextJsonPatch.ApiTest.csproj", "{F62A2CB9-99E6-430C-ADF8-2D83D04722CA}"
22+
EndProject
2123
Global
2224
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2325
Debug|Any CPU = Debug|Any CPU
@@ -40,6 +42,10 @@ Global
4042
{4DA8A38B-D595-4A5E-962A-C382EA7D8906}.Debug|Any CPU.Build.0 = Debug|Any CPU
4143
{4DA8A38B-D595-4A5E-962A-C382EA7D8906}.Release|Any CPU.ActiveCfg = Release|Any CPU
4244
{4DA8A38B-D595-4A5E-962A-C382EA7D8906}.Release|Any CPU.Build.0 = Release|Any CPU
45+
{F62A2CB9-99E6-430C-ADF8-2D83D04722CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
46+
{F62A2CB9-99E6-430C-ADF8-2D83D04722CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
47+
{F62A2CB9-99E6-430C-ADF8-2D83D04722CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
48+
{F62A2CB9-99E6-430C-ADF8-2D83D04722CA}.Release|Any CPU.Build.0 = Release|Any CPU
4349
EndGlobalSection
4450
GlobalSection(SolutionProperties) = preSolution
4551
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)