Compare commits

..

1 Commits
master ... tp2

Author SHA1 Message Date
Tom RAMBEAU 35061e7c24 tp2
1 year ago

@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
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
Global
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}.Release|Any CPU.ActiveCfg = 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
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -3,101 +3,72 @@ using System;
namespace API_Rest.Controllers
{
[ApiController]
[Route("api/[controller]")]
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
{
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 WeatherForecastService _wfs { get; set; }
public IEnumerable<WeatherForecast> WeatherForecasts { get; set; }
public WeatherForecast wf { get; set; }
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
public WeatherForecastController(ILogger<WeatherForecastController> logger, WeatherForecastService wfs)
{
_wfs = wfs;
_logger = logger;
}
[HttpGet]
[HttpGet("GetAll/")]
[Route("/api/WeatherForecast/getAll/")]
public IEnumerable<WeatherForecast> GetAll()
public async Task<IEnumerable<WeatherForecast>> GetAll()
{
return Weathers
.ToArray();
WeatherForecasts = _wfs.Get();
return WeatherForecasts;
}
[HttpGet]
[Route("/api/WeatherForecast/getOne/{id}")]
[HttpGet("GetOne/{id}")]
public async Task<ActionResult<WeatherForecast>> GetOne(long id)
{
foreach (WeatherForecast wf in Weathers)
{
if (wf.Id == id)
wf = _wfs.GetOne(id);
if (wf != null)
{
return Ok(wf);
}
}
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)
{
//_context.TodoItems.Add(todoItem);
//await _context.SaveChangesAsync();
Weathers.Add(wf);
_wfs.Post(wf);
return CreatedAtAction(nameof(Created), new { date = wf.Date }, wf);
}
[HttpDelete]
[Route("/api/WeatherForecast/delete/{id}")]
public async Task<ActionResult> Delete(long id)
{
foreach (WeatherForecast wf in Weathers)
[HttpDelete("DeleteOne/{id}")]
public async Task<ActionResult> DeleteOne(long id)
{
if (wf.Id == id)
if (_wfs.DeleteOne(id))
{
Weathers.Remove(wf);
return Ok();
}
}
return BadRequest();
}
}
}

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