tableau ordonné par le plein le moins chere avec le trajet
This commit is contained in:
@@ -3,6 +3,7 @@ using ApiCarburant.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualBasic;
|
||||
|
||||
namespace ApiCarburant.Controllers;
|
||||
|
||||
@@ -10,6 +11,19 @@ namespace ApiCarburant.Controllers;
|
||||
[Route("[controller]")]
|
||||
public class CarburantController : ControllerBase
|
||||
{
|
||||
public record ResultatStation(
|
||||
string nom,
|
||||
double distance,
|
||||
decimal? diesel,
|
||||
decimal? e85,
|
||||
decimal? e5,
|
||||
decimal? sp98)
|
||||
{
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{{ nom = {nom}, distance = {distance}, diesel = {diesel}, e85 = {e85} }}";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a collection of fuel stations located near the specified geographic coordinates.
|
||||
@@ -24,29 +38,34 @@ public class CarburantController : ControllerBase
|
||||
/// ?lon=4.816992463720462&lat=43.9291477523281
|
||||
/// </example>
|
||||
[HttpGet(Name = "GetCarburant")]
|
||||
public async Task<IEnumerable<Station>> Get([FromServices] PompeService pompeService,
|
||||
[FromQuery] double? lon, [FromQuery] double? lat, [FromQuery] long dist)
|
||||
public async Task<IEnumerable<ResultatStation>> Get([FromServices] PompeService pompeService,
|
||||
[FromQuery] double? lon, [FromQuery] double? lat, [FromQuery] long? dist, [FromQuery] string? carburant = "E85")
|
||||
|
||||
{
|
||||
dist = dist == 0 ? 10000 : dist;
|
||||
dist = dist ?? 25;
|
||||
double lon1 = lon ?? 4.816992463720462;
|
||||
double lat1 = lat ?? 43.9291477523281;
|
||||
if (pompeService.Station == null)
|
||||
{ await pompeService.RefreshAsync(); }
|
||||
return pompeService.Station.Select(s => new { distance = 110574 * Math.Sqrt((s.Latitude - lat1) * (s.Latitude - lat1) + (s.Longitude - lon1) * (s.Longitude - lon1)), station = s })
|
||||
.Where(x => x.distance < dist)
|
||||
.OrderBy(x => x.distance)
|
||||
.Select(x => x.station)
|
||||
if (!pompeService.Station.Any())
|
||||
{
|
||||
await pompeService.RefreshAsync();
|
||||
}
|
||||
return pompeService.Station.Select(s => new { distance = 110.574 * Math.Sqrt((s.Latitude - lat1) * (s.Latitude - lat1) + (s.Longitude - lon1) * (s.Longitude - lon1)), station = s })
|
||||
.Where(x => x.distance < dist && x.station.Prix.Any(p => p.nom == carburant))
|
||||
.OrderBy(x => x.station.E85 * 50m + (decimal)x.distance * (2m * 0.2m * x.station.E85.Value + 2m))
|
||||
.Select(x =>
|
||||
new ResultatStation(x.station.Ville + x.station.Adresse, x.distance, x.station.Gazole, x.station.E85, x.station.E5, x.station.SP98)
|
||||
|
||||
)
|
||||
.ToList();
|
||||
// .OrderBy(x => x.Prix.OrderBy(p => p.Valeur).Select(p => p.Valeur).FirstOrDefault()??15).ToList();
|
||||
//throw new Exception("Test exception");
|
||||
//return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
//{
|
||||
// Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
// TemperatureC = Random.Shared.Next(-20, 55),
|
||||
// Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
//})
|
||||
//.ToArray();
|
||||
}
|
||||
[HttpGet("nom", Name = "GetNomCarburant")]
|
||||
public async Task<IEnumerable<String>> GetNomCarburant([FromServices] PompeService pompeService)
|
||||
{
|
||||
if (!pompeService.Station.Any())
|
||||
{
|
||||
await pompeService.RefreshAsync();
|
||||
}
|
||||
return pompeService.Station.SelectMany(x => x.Prix).Select(p => p.nom).Distinct();
|
||||
}
|
||||
[HttpGet("refresh", Name = "GetRefreshCarburant")]
|
||||
public async Task<IActionResult> Refresh([FromServices] PompeService pompeService)
|
||||
|
||||
@@ -2,16 +2,10 @@ namespace ApiCarburant
|
||||
{
|
||||
public class Prix
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string? nom { get; set; }
|
||||
public decimal Valeur { get; set; }
|
||||
public DateTime Maj { get; set; }
|
||||
}
|
||||
|
||||
public enum TypeCarburant
|
||||
{
|
||||
E85,
|
||||
SP95,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,27 @@
|
||||
namespace ApiCarburant;
|
||||
|
||||
public class Station
|
||||
public class StationSansPrix
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public double Latitude { get; set; }
|
||||
public double Longitude { get; set; }
|
||||
|
||||
public string cp { get; set; }
|
||||
|
||||
|
||||
public string Ville { get; set; }
|
||||
public string Adresse { get; set; }
|
||||
|
||||
public decimal? E85 { get; set; }
|
||||
public decimal? E5 { get; set; }
|
||||
public decimal? E10 { get; set; }
|
||||
public decimal? SP98 { get; set; }
|
||||
public decimal? Gazole { get; set; }
|
||||
}
|
||||
public class Station : StationSansPrix
|
||||
{
|
||||
|
||||
public List<Prix> Prix { get; set; } = new();
|
||||
|
||||
public StationSansPrix WithoutListPrix()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,6 @@ namespace ApiCarburant.Services
|
||||
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
|
||||
if (reader.Name == "pdv")
|
||||
{
|
||||
if (reader.NodeType == XmlNodeType.Element)
|
||||
@@ -62,7 +61,6 @@ namespace ApiCarburant.Services
|
||||
Latitude = double.Parse(reader.GetAttribute("latitude")) / 100000,
|
||||
Longitude = double.Parse(reader.GetAttribute("longitude")) / 100000,
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
else if (reader.NodeType == XmlNodeType.EndElement)
|
||||
@@ -95,14 +93,30 @@ namespace ApiCarburant.Services
|
||||
Maj = DateTime.Parse(reader.GetAttribute("maj"))
|
||||
};
|
||||
currentStation.Prix.Add(prix);
|
||||
}
|
||||
else
|
||||
if (prix.nom == "Gazole")
|
||||
{
|
||||
currentStation.Gazole = prix.Valeur;
|
||||
}
|
||||
else if (prix.nom == "E5")
|
||||
{
|
||||
currentStation.E5 = prix.Valeur;
|
||||
}
|
||||
else if (prix.nom == "E10")
|
||||
{
|
||||
currentStation.E10 = prix.Valeur;
|
||||
}
|
||||
else if (prix.nom == "SP98")
|
||||
{
|
||||
currentStation.SP98 = prix.Valeur;
|
||||
}
|
||||
else if (prix.nom == "E85")
|
||||
{
|
||||
currentStation.E85 = prix.Valeur;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
this.Station = stations;
|
||||
@@ -129,6 +143,10 @@ namespace ApiCarburant.Services
|
||||
//};
|
||||
}
|
||||
|
||||
public List<Station> Station { get; set; }
|
||||
public List<Station> Station
|
||||
{
|
||||
get => field;
|
||||
set => field = value;
|
||||
} = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,34 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>Stations autour de moi</h1>
|
||||
<p>Coucou</p>
|
||||
<input type="range" min="5" max="150" value="10" id="distanceSlider">
|
||||
<span id="distanceValue">10 km</span>
|
||||
<button onclick="getLocation()">Localiser</button>
|
||||
<table id="stationsTable" border="1">
|
||||
<thead>
|
||||
<tr>
|
||||
<th onclick="sortTable(0)">Nom</th>
|
||||
<th onclick="sortTable(1)">Distance</th>
|
||||
<th onclick="sortTable(2)">Diesel</th>
|
||||
<th onclick="sortTable(3)">E85</th>
|
||||
<th onclick="sortTable(4)">E5</th>
|
||||
<th onclick="sortTable(3)">SP98</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody></tbody>
|
||||
</table>
|
||||
|
||||
<pre id="result"></pre>
|
||||
|
||||
<script>async function getLocation() {
|
||||
<script>
|
||||
const slider = document.getElementById("distanceSlider");
|
||||
const value = document.getElementById("distanceValue");
|
||||
|
||||
slider.oninput = () => {
|
||||
value.innerText = slider.value + " km";
|
||||
};
|
||||
|
||||
async function getLocation() {
|
||||
if (!navigator.geolocation) {
|
||||
alert("Géolocalisation non supportée");
|
||||
return;
|
||||
@@ -23,23 +46,56 @@
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
navigator.geolocation.getCurrentPosition(async (pos) => {
|
||||
const lat = pos.coords.latitude;
|
||||
const lon = pos.coords.longitude;
|
||||
const distance = document.getElementById("distanceSlider").value;
|
||||
|
||||
try {
|
||||
const res = await fetch(`/carburant?lat=${lat}&lon=${lon}`);
|
||||
const res = await fetch(`/carburant?lat=${lat}&lon=${lon}&dist=${distance}`);
|
||||
const data = await res.json();
|
||||
document.getElementById("result").textContent =
|
||||
JSON.stringify(data, null, 2);
|
||||
const tbody = document.querySelector("#stationsTable tbody");
|
||||
tbody.innerHTML = "";
|
||||
|
||||
data.forEach(s => {
|
||||
const row = `
|
||||
<tr>
|
||||
<td>${s.nom}</td>
|
||||
<td>${s.distance.toFixed(2)} km</td>
|
||||
<td>${s.diesel ?? "-"}</td>
|
||||
<td>${s.e85 ?? "-"}</td>
|
||||
<td>${s.E5 ?? "-"}</td>
|
||||
<td>${s.sp98 ?? "-"}</td>
|
||||
</tr>`;
|
||||
tbody.innerHTML += row;
|
||||
});
|
||||
|
||||
//document.getElementById("result").textContent =
|
||||
// JSON.stringify(data, null, 2);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}, (err) => {
|
||||
alert("Erreur géolocalisation: " + err.message);
|
||||
});
|
||||
function sortTable(colIndex) {
|
||||
const table = document.getElementById("stationsTable");
|
||||
const rows = Array.from(table.rows).slice(1);
|
||||
|
||||
const asc = table.getAttribute("data-sort") !== "asc";
|
||||
table.setAttribute("data-sort", asc ? "asc" : "desc");
|
||||
|
||||
rows.sort((a, b) => {
|
||||
const A = a.cells[colIndex].innerText;
|
||||
const B = b.cells[colIndex].innerText;
|
||||
|
||||
return asc
|
||||
? A.localeCompare(B, undefined, { numeric: true })
|
||||
: B.localeCompare(A, undefined, { numeric: true });
|
||||
});
|
||||
|
||||
rows.forEach(r => table.appendChild(r));
|
||||
}
|
||||
}</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user