Tom RAMBEAU 1 year ago
parent 41327233e9
commit 35061e7c24

@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188 VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_Rest", "API_Rest\API_Rest.csproj", "{2FA20B90-3CCD-4EF2-9751-341082793745}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API_Rest", "API_Rest\API_Rest.csproj", "{2FA20B90-3CCD-4EF2-9751-341082793745}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_UnitTest", "API_UnitTest\API_UnitTest.csproj", "{D8815A04-DE74-46F6-BD39-C946F3EE33CA}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -15,6 +17,10 @@ Global
{2FA20B90-3CCD-4EF2-9751-341082793745}.Debug|Any CPU.Build.0 = Debug|Any CPU {2FA20B90-3CCD-4EF2-9751-341082793745}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FA20B90-3CCD-4EF2-9751-341082793745}.Release|Any CPU.ActiveCfg = Release|Any CPU {2FA20B90-3CCD-4EF2-9751-341082793745}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FA20B90-3CCD-4EF2-9751-341082793745}.Release|Any CPU.Build.0 = Release|Any CPU {2FA20B90-3CCD-4EF2-9751-341082793745}.Release|Any CPU.Build.0 = Release|Any CPU
{D8815A04-DE74-46F6-BD39-C946F3EE33CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8815A04-DE74-46F6-BD39-C946F3EE33CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8815A04-DE74-46F6-BD39-C946F3EE33CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8815A04-DE74-46F6-BD39-C946F3EE33CA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -3,101 +3,72 @@ using System;
namespace API_Rest.Controllers namespace API_Rest.Controllers
{ {
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/[controller]")]
public class WeatherForecastController : ControllerBase public class WeatherForecastController : ControllerBase
{ {
private static List<String> Summaries = new List<String>
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
public static List<WeatherForecast> Weathers = new List<WeatherForecast>
{
new WeatherForecast public WeatherForecastService _wfs { get; set; }
{
Id = 1,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
},
new WeatherForecast
{
Id = 2,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
},
new WeatherForecast
{
Id = 3,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
}
};
public IEnumerable<WeatherForecast> WeatherForecasts { get; set; }
public WeatherForecast wf { get; set; }
private readonly ILogger<WeatherForecastController> _logger; private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger) public WeatherForecastController(ILogger<WeatherForecastController> logger, WeatherForecastService wfs)
{ {
_wfs = wfs;
_logger = logger; _logger = logger;
} }
[HttpGet] [HttpGet("GetAll/")]
[Route("/api/WeatherForecast/getAll/")] public async Task<IEnumerable<WeatherForecast>> GetAll()
public IEnumerable<WeatherForecast> GetAll()
{ {
return Weathers WeatherForecasts = _wfs.Get();
.ToArray(); return WeatherForecasts;
} }
[HttpGet] [HttpGet("GetOne/{id}")]
[Route("/api/WeatherForecast/getOne/{id}")]
public async Task<ActionResult<WeatherForecast>> GetOne(long id) public async Task<ActionResult<WeatherForecast>> GetOne(long id)
{ {
foreach (WeatherForecast wf in Weathers) wf = _wfs.GetOne(id);
if (wf != null)
{ {
if (wf.Id == id) return Ok(wf);
{
return Ok(wf);
}
} }
return BadRequest(); return BadRequest();
} }
[HttpPost(Name = "PostWeatherForecast")] [HttpPut("Update/{id}")]
public async Task<ActionResult<WeatherForecast>> UpdateWeatherForcast(int id, WeatherForecast weatherForcast) {
_wfs.UpdateWeatherForcast(id, weatherForcast);
return Ok();
}
[HttpPost("{WeatherForecast wf}")]
public async Task<ActionResult<WeatherForecast>> Post(WeatherForecast wf) public async Task<ActionResult<WeatherForecast>> Post(WeatherForecast wf)
{ {
//_context.TodoItems.Add(todoItem); _wfs.Post(wf);
//await _context.SaveChangesAsync(); return CreatedAtAction(nameof(Created), new { date = wf.Date }, wf);
Weathers.Add(wf);
return CreatedAtAction(nameof(Created), new { date = wf.Date }, wf);
} }
[HttpDelete]
[Route("/api/WeatherForecast/delete/{id}")] [HttpDelete("DeleteOne/{id}")]
public async Task<ActionResult> Delete(long id) public async Task<ActionResult> DeleteOne(long id)
{ {
foreach (WeatherForecast wf in Weathers) if (_wfs.DeleteOne(id))
{ {
if (wf.Id == id) return Ok();
{
Weathers.Remove(wf);
return Ok();
}
} }
return BadRequest(); return BadRequest();
} }
} }
} }

@ -1,3 +1,4 @@
using API_Rest;
using API_Rest.Controllers; using API_Rest.Controllers;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -9,6 +10,7 @@ builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<WeatherForecastService, WeatherForecastService>();
var app = builder.Build(); var app = builder.Build();

@ -0,0 +1,91 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using System.Runtime.CompilerServices;
namespace API_Rest
{
public class WeatherForecastService
{
private static List<String> Summaries = new List<String>
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
public List<WeatherForecast> Weathers { get; set; } = new List<WeatherForecast>
{
new WeatherForecast
{
Id = 1,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
},
new WeatherForecast
{
Id = 2,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
},
new WeatherForecast
{
Id = 3,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
}
};
public List<WeatherForecast> Get()
{
return Weathers;
}
public WeatherForecast GetOne(long id)
{
if (Weathers != null)
{
foreach (WeatherForecast wf in Weathers)
{
if (wf.Id == id)
{
return wf;
}
}
return null;
}
return null;
}
public void UpdateWeatherForcast(int id, WeatherForecast weatherForcast)
{
weatherForcast.Id = id;
}
public void Post(WeatherForecast wf)
{
Weathers.Add(wf);
}
public bool DeleteOne(long id)
{
foreach (WeatherForecast wf in Weathers)
{
if (wf.Id == id)
{
Weathers.Remove(wf);
return true;
}
}
return false;
}
}
}

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\API_Rest\API_Rest.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,43 @@
using API_Rest;
using API_Rest.Controllers;
using Microsoft.Extensions.Logging;
using System.Security.Cryptography.X509Certificates;
namespace API_UnitTest
{
public class UnitTest
{
public ILogger<WeatherForecastController> logger { get; set; }
public WeatherForecastService wfs = new WeatherForecastService();
public WeatherForecastController wfc;
public UnitTest() {
wfc = new WeatherForecastController(logger, wfs);
}
[Fact]
public void GetAll()
{
var res = wfc.GetAll();
Assert.NotNull(res);
}
[Fact]
public void GetOne()
{
var res = wfc.GetOne(1);
Assert.NotNull(res.Result);
}
[Fact]
public async void Post()
{
WeatherForecast wf = new WeatherForecast();
wf.Id = 4;
wf.Date = DateOnly.FromDateTime(DateTime.Today);
wf.TemperatureC = 5;
await wfc.Post(wf);
Assert.Contains(wf,wfs.Weathers);
}
}
}
Loading…
Cancel
Save