parent
8a89d04fff
commit
41327233e9
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,6 @@
|
||||
@API_Rest_HostAddress = http://localhost:5225
|
||||
|
||||
GET {{API_Rest_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
@ -0,0 +1,103 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
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)]
|
||||
}
|
||||
};
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
|
||||
[Route("/api/WeatherForecast/getAll/")]
|
||||
public IEnumerable<WeatherForecast> GetAll()
|
||||
{
|
||||
return Weathers
|
||||
.ToArray();
|
||||
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("/api/WeatherForecast/getOne/{id}")]
|
||||
|
||||
public async Task<ActionResult<WeatherForecast>> GetOne(long id)
|
||||
{
|
||||
|
||||
foreach (WeatherForecast wf in Weathers)
|
||||
{
|
||||
if (wf.Id == id)
|
||||
{
|
||||
return Ok(wf);
|
||||
}
|
||||
}
|
||||
return BadRequest();
|
||||
|
||||
}
|
||||
|
||||
[HttpPost(Name = "PostWeatherForecast")]
|
||||
public async Task<ActionResult<WeatherForecast>> Post(WeatherForecast wf)
|
||||
{
|
||||
//_context.TodoItems.Add(todoItem);
|
||||
//await _context.SaveChangesAsync();
|
||||
Weathers.Add(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)
|
||||
{
|
||||
if (wf.Id == id)
|
||||
{
|
||||
Weathers.Remove(wf);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
return BadRequest();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
using API_Rest.Controllers;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
@ -0,0 +1,14 @@
|
||||
namespace API_Rest
|
||||
{
|
||||
public class WeatherForecast
|
||||
{
|
||||
public int Id {get;set;}
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
Loading…
Reference in new issue