From b29d7a6a2d01efdd9dc71fa69ddede41eaa77e6d Mon Sep 17 00:00:00 2001 From: David Manuguerra Date: Sun, 5 Apr 2026 22:28:58 +0200 Subject: [PATCH] =?UTF-8?q?tableau=20ordonn=C3=A9=20par=20le=20plein=20le?= =?UTF-8?q?=20moins=20chere=20avec=20le=20trajet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/CarburantController.cs | 55 +++++--- ApiCarburant/Pompe.Detail.cs | 6 - ApiCarburant/Pompe.cs | 26 ++-- ApiCarburant/Services/PompeService.cs | 34 +++-- ApiCarburant/wwwroot/index.html | 118 +++++++++++++----- 5 files changed, 168 insertions(+), 71 deletions(-) diff --git a/ApiCarburant/Controllers/CarburantController.cs b/ApiCarburant/Controllers/CarburantController.cs index fd4ec81..548d716 100644 --- a/ApiCarburant/Controllers/CarburantController.cs +++ b/ApiCarburant/Controllers/CarburantController.cs @@ -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} }}"; + } + } /// /// 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 /// [HttpGet(Name = "GetCarburant")] - public async Task> Get([FromServices] PompeService pompeService, - [FromQuery] double? lon, [FromQuery] double? lat, [FromQuery] long dist) + public async Task> 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> 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 Refresh([FromServices] PompeService pompeService) diff --git a/ApiCarburant/Pompe.Detail.cs b/ApiCarburant/Pompe.Detail.cs index ba205bc..5d4d213 100644 --- a/ApiCarburant/Pompe.Detail.cs +++ b/ApiCarburant/Pompe.Detail.cs @@ -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, - } } diff --git a/ApiCarburant/Pompe.cs b/ApiCarburant/Pompe.cs index 8c5a798..d7df90d 100644 --- a/ApiCarburant/Pompe.cs +++ b/ApiCarburant/Pompe.cs @@ -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 double Latitude { get; set; } + public double Longitude { 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 { get; set; } = new(); + + public StationSansPrix WithoutListPrix() + { + return this; + } } \ No newline at end of file diff --git a/ApiCarburant/Services/PompeService.cs b/ApiCarburant/Services/PompeService.cs index 933a44b..c9e66c5 100644 --- a/ApiCarburant/Services/PompeService.cs +++ b/ApiCarburant/Services/PompeService.cs @@ -51,7 +51,6 @@ namespace ApiCarburant.Services while (await reader.ReadAsync()) { - if (reader.Name == "pdv") { if (reader.NodeType == XmlNodeType.Element) @@ -61,7 +60,6 @@ namespace ApiCarburant.Services Id = reader.GetAttribute("id"), Latitude = double.Parse(reader.GetAttribute("latitude")) / 100000, Longitude = double.Parse(reader.GetAttribute("longitude")) / 100000, - }; } @@ -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 { get; set; } + public List Station + { + get => field; + set => field = value; + } = []; } } diff --git a/ApiCarburant/wwwroot/index.html b/ApiCarburant/wwwroot/index.html index 6c0b8aa..8c8bb18 100644 --- a/ApiCarburant/wwwroot/index.html +++ b/ApiCarburant/wwwroot/index.html @@ -3,43 +3,99 @@ Stations carburant - + -

Stations autour de moi

-

Coucou

- -

+    

Stations autour de moi

+ + 10 km + + + + + + + + + + + + + +
NomDistanceDieselE85E5SP98
- + if (perm.state === "denied") { + alert("Veuillez autoriser la localisation dans les paramètres du navigateur"); + 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}&dist=${distance}`); + const data = await res.json(); + const tbody = document.querySelector("#stationsTable tbody"); + tbody.innerHTML = ""; + + data.forEach(s => { + const row = ` + + ${s.nom} + ${s.distance.toFixed(2)} km + ${s.diesel ?? "-"} + ${s.e85 ?? "-"} + ${s.E5 ?? "-"} + ${s.sp98 ?? "-"} + `; + 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)); + } + } \ No newline at end of file