Skip to content

Commit

Permalink
Added example web api
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Oct 18, 2024
1 parent ef1852f commit f61fd2c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 0 deletions.
62 changes: 62 additions & 0 deletions SystemTextJsonPatch.ApiTest/Controllers/WeatherController.cs
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; }
}
}
12 changes: 12 additions & 0 deletions SystemTextJsonPatch.ApiTest/Program.cs
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 SystemTextJsonPatch.ApiTest/Properties/launchSettings.json
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 SystemTextJsonPatch.ApiTest/SystemTextJsonPatch.ApiTest.csproj
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>
8 changes: 8 additions & 0 deletions SystemTextJsonPatch.ApiTest/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 SystemTextJsonPatch.ApiTest/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
6 changes: 6 additions & 0 deletions SystemTextJsonPatch.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SystemTextJsonPatch.Benchma
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SystemTextJsonPatch.Console", "SystemTextJsonPatch.Console\SystemTextJsonPatch.Console.csproj", "{4DA8A38B-D595-4A5E-962A-C382EA7D8906}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SystemTextJsonPatch.ApiTest", "SystemTextJsonPatch.ApiTest\SystemTextJsonPatch.ApiTest.csproj", "{F62A2CB9-99E6-430C-ADF8-2D83D04722CA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -40,6 +42,10 @@ Global
{4DA8A38B-D595-4A5E-962A-C382EA7D8906}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DA8A38B-D595-4A5E-962A-C382EA7D8906}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DA8A38B-D595-4A5E-962A-C382EA7D8906}.Release|Any CPU.Build.0 = Release|Any CPU
{F62A2CB9-99E6-430C-ADF8-2D83D04722CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F62A2CB9-99E6-430C-ADF8-2D83D04722CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F62A2CB9-99E6-430C-ADF8-2D83D04722CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F62A2CB9-99E6-430C-ADF8-2D83D04722CA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit f61fd2c

Please sign in to comment.