ajout méthodes crud

master
Tom RAMBEAU 10 months ago
parent 8a89d04fff
commit 41327233e9

@ -0,0 +1,25 @@

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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2FA20B90-3CCD-4EF2-9751-341082793745}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D44BCDFE-4237-4E0B-ACA6-B96DFB575885}
EndGlobalSection
EndGlobal

@ -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,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49491",
"sslPort": 44377
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5225",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7255;http://localhost:5225",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

@ -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": "*"
}

@ -0,0 +1,7 @@
namespace API_Rest
{
public class dbWeather
{
}
}

@ -0,0 +1,6 @@

namespace API_Rest
{
}
Loading…
Cancel
Save