Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
![]() |
0e82975038 | 1 year ago |
![]() |
35061e7c24 | 1 year ago |
@ -1,103 +1,88 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace API_Rest.Controllers
|
namespace API_Rest.Controllers
|
||||||
{
|
{
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
[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/")]
|
||||||
|
[Authorize]
|
||||||
[Route("/api/WeatherForecast/getAll/")]
|
public async Task<IEnumerable<WeatherForecast>> GetAll()
|
||||||
public IEnumerable<WeatherForecast> GetAll()
|
|
||||||
{
|
{
|
||||||
return Weathers
|
WeatherForecasts = _wfs.Get();
|
||||||
.ToArray();
|
return WeatherForecasts;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("/api/WeatherForecast/getOne/{id}")]
|
[Authorize]
|
||||||
|
public async Task<IActionResult> Get(int page, int pageSize)
|
||||||
|
{
|
||||||
|
|
||||||
|
WeatherForecasts = _wfs.Get();
|
||||||
|
var pageResuts = WeatherForecasts.Skip(page).Take(pageSize);
|
||||||
|
return Ok(pageResuts);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("GetOne/{id}")]
|
||||||
|
[Authorize]
|
||||||
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}")]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<WeatherForecast>> UpdateWeatherForcast(int id, WeatherForecast weatherForcast) {
|
||||||
|
_wfs.UpdateWeatherForcast(id, weatherForcast);
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("{WeatherForecast wf}")]
|
||||||
|
[Authorize]
|
||||||
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();
|
|
||||||
Weathers.Add(wf);
|
|
||||||
return CreatedAtAction(nameof(Created), new { date = wf.Date }, 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)
|
[Authorize]
|
||||||
{
|
public async Task<ActionResult> DeleteOne(long id)
|
||||||
foreach (WeatherForecast wf in Weathers)
|
|
||||||
{
|
{
|
||||||
if (wf.Id == id)
|
if (_wfs.DeleteOne(id))
|
||||||
{
|
{
|
||||||
Weathers.Remove(wf);
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 @@
|
|||||||
|
global using Xunit;
|
@ -0,0 +1,64 @@
|
|||||||
|
using API_Rest;
|
||||||
|
using API_Rest.Controllers;
|
||||||
|
using Microsoft.AspNetCore.Http.HttpResults;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
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 async void GetOne()
|
||||||
|
{
|
||||||
|
var res = await wfc.GetOne(1) ;
|
||||||
|
Assert.NotNull(res.Result);
|
||||||
|
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public async void GetOneFail()
|
||||||
|
{
|
||||||
|
var res = await wfc.GetOne(1);
|
||||||
|
Assert.Equal(new BadRequestResult(), res);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[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);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async void Delete()
|
||||||
|
{
|
||||||
|
var res = await wfc.DeleteOne(1);
|
||||||
|
Assert.NotNull(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue