Compare commits
No commits in common. 'tp3' and 'master' have entirely different histories.
@ -1,88 +1,103 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
|
||||
namespace API_Rest.Controllers
|
||||
{
|
||||
|
||||
[Authorize]
|
||||
[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 WeatherForecastService _wfs { get; set; }
|
||||
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 IEnumerable<WeatherForecast> WeatherForecasts { get; set; }
|
||||
public WeatherForecast wf { get; set; }
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger, WeatherForecastService wfs)
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
{
|
||||
_wfs = wfs;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet("GetAll/")]
|
||||
[Authorize]
|
||||
public async Task<IEnumerable<WeatherForecast>> GetAll()
|
||||
{
|
||||
WeatherForecasts = _wfs.Get();
|
||||
return WeatherForecasts;
|
||||
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> Get(int page, int pageSize)
|
||||
{
|
||||
|
||||
WeatherForecasts = _wfs.Get();
|
||||
var pageResuts = WeatherForecasts.Skip(page).Take(pageSize);
|
||||
return Ok(pageResuts);
|
||||
[Route("/api/WeatherForecast/getAll/")]
|
||||
public IEnumerable<WeatherForecast> GetAll()
|
||||
{
|
||||
return Weathers
|
||||
.ToArray();
|
||||
|
||||
}
|
||||
|
||||
[HttpGet("GetOne/{id}")]
|
||||
[Authorize]
|
||||
[HttpGet]
|
||||
[Route("/api/WeatherForecast/getOne/{id}")]
|
||||
|
||||
public async Task<ActionResult<WeatherForecast>> GetOne(long id)
|
||||
{
|
||||
|
||||
wf = _wfs.GetOne(id);
|
||||
if (wf != null)
|
||||
foreach (WeatherForecast wf in Weathers)
|
||||
{
|
||||
return Ok(wf);
|
||||
if (wf.Id == id)
|
||||
{
|
||||
return Ok(wf);
|
||||
}
|
||||
}
|
||||
return BadRequest();
|
||||
|
||||
}
|
||||
|
||||
[HttpPut("Update/{id}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<WeatherForecast>> UpdateWeatherForcast(int id, WeatherForecast weatherForcast) {
|
||||
_wfs.UpdateWeatherForcast(id, weatherForcast);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("{WeatherForecast wf}")]
|
||||
[Authorize]
|
||||
[HttpPost(Name = "PostWeatherForecast")]
|
||||
public async Task<ActionResult<WeatherForecast>> Post(WeatherForecast wf)
|
||||
{
|
||||
_wfs.Post(wf);
|
||||
return CreatedAtAction(nameof(Created), new { date = wf.Date }, wf);
|
||||
//_context.TodoItems.Add(todoItem);
|
||||
//await _context.SaveChangesAsync();
|
||||
Weathers.Add(wf);
|
||||
return CreatedAtAction(nameof(Created), new { date = wf.Date }, wf);
|
||||
}
|
||||
|
||||
|
||||
[HttpDelete("DeleteOne/{id}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult> DeleteOne(long id)
|
||||
[HttpDelete]
|
||||
[Route("/api/WeatherForecast/delete/{id}")]
|
||||
public async Task<ActionResult> Delete(long id)
|
||||
{
|
||||
if (_wfs.DeleteOne(id))
|
||||
foreach (WeatherForecast wf in Weathers)
|
||||
{
|
||||
return Ok();
|
||||
if (wf.Id == id)
|
||||
{
|
||||
Weathers.Remove(wf);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
return BadRequest();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
<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>
|
@ -1 +0,0 @@
|
||||
global using Xunit;
|
@ -1,64 +0,0 @@
|
||||
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