diff --git a/API_Rest/API_Rest.sln b/API_Rest/API_Rest.sln index a53e739..f2f0c75 100644 --- a/API_Rest/API_Rest.sln +++ b/API_Rest/API_Rest.sln @@ -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 diff --git a/API_Rest/API_Rest/Controllers/WeatherForecastController.cs b/API_Rest/API_Rest/Controllers/WeatherForecastController.cs index 6ec987c..332f1fa 100644 --- a/API_Rest/API_Rest/Controllers/WeatherForecastController.cs +++ b/API_Rest/API_Rest/Controllers/WeatherForecastController.cs @@ -3,101 +3,72 @@ using System; namespace API_Rest.Controllers { + + [ApiController] [Route("api/[controller]")] public class WeatherForecastController : ControllerBase { - private static List Summaries = new List - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - public static List Weathers = new List - { - 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 WeatherForecasts { get; set; } + public WeatherForecast wf { get; set; } private readonly ILogger _logger; - public WeatherForecastController(ILogger logger) + public WeatherForecastController(ILogger logger, WeatherForecastService wfs) { + _wfs = wfs; _logger = logger; } - [HttpGet] + [HttpGet("GetAll/")] - [Route("/api/WeatherForecast/getAll/")] - public IEnumerable GetAll() + public async Task> GetAll() { - return Weathers - .ToArray(); + WeatherForecasts = _wfs.Get(); + return WeatherForecasts; } - [HttpGet] - [Route("/api/WeatherForecast/getOne/{id}")] - + [HttpGet("GetOne/{id}")] + public async Task> 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(); } - [HttpPost(Name = "PostWeatherForecast")] + [HttpPut("Update/{id}")] + + public async Task> UpdateWeatherForcast(int id, WeatherForecast weatherForcast) { + _wfs.UpdateWeatherForcast(id, weatherForcast); + return Ok(); + } + + [HttpPost("{WeatherForecast wf}")] public async Task> Post(WeatherForecast wf) { - //_context.TodoItems.Add(todoItem); - //await _context.SaveChangesAsync(); - Weathers.Add(wf); - return CreatedAtAction(nameof(Created), new { date = wf.Date }, wf); + _wfs.Post(wf); + return CreatedAtAction(nameof(Created), new { date = wf.Date }, wf); } - [HttpDelete] - [Route("/api/WeatherForecast/delete/{id}")] - public async Task Delete(long id) + + [HttpDelete("DeleteOne/{id}")] + public async Task DeleteOne(long id) { - foreach (WeatherForecast wf in Weathers) + if (_wfs.DeleteOne(id)) { - if (wf.Id == id) - { - Weathers.Remove(wf); - return Ok(); - } + return Ok(); } return BadRequest(); - - } } } diff --git a/API_Rest/API_Rest/Program.cs b/API_Rest/API_Rest/Program.cs index cc47a7b..d693ba5 100644 --- a/API_Rest/API_Rest/Program.cs +++ b/API_Rest/API_Rest/Program.cs @@ -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(); var app = builder.Build(); diff --git a/API_Rest/API_Rest/WeatherForecastService.cs b/API_Rest/API_Rest/WeatherForecastService.cs new file mode 100644 index 0000000..f978612 --- /dev/null +++ b/API_Rest/API_Rest/WeatherForecastService.cs @@ -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 Summaries = new List + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + public List Weathers { get; set; } = new List + { + + 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 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; + } + } +} diff --git a/API_Rest/API_UnitTest/API_UnitTest.csproj b/API_Rest/API_UnitTest/API_UnitTest.csproj new file mode 100644 index 0000000..3a6ec62 --- /dev/null +++ b/API_Rest/API_UnitTest/API_UnitTest.csproj @@ -0,0 +1,29 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/API_Rest/API_UnitTest/GlobalUsings.cs b/API_Rest/API_UnitTest/GlobalUsings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/API_Rest/API_UnitTest/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/API_Rest/API_UnitTest/UnitTest.cs b/API_Rest/API_UnitTest/UnitTest.cs new file mode 100644 index 0000000..95c1414 --- /dev/null +++ b/API_Rest/API_UnitTest/UnitTest.cs @@ -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 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); + } + } +} \ No newline at end of file