From ff8483eb838846a4c38fe650ad8cc75e495390d6 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 29 Oct 2021 19:32:45 +0330 Subject: [PATCH] #233 displaying poet birth places on the map --- GanjooRazor/Areas/Admin/Pages/Poets.cshtml | 2 +- GanjooRazor/Pages/Map.cshtml | 50 ++++++++-------------- GanjooRazor/Pages/Map.cshtml.cs | 48 ++++++++++++++++++++- 3 files changed, 66 insertions(+), 34 deletions(-) diff --git a/GanjooRazor/Areas/Admin/Pages/Poets.cshtml b/GanjooRazor/Areas/Admin/Pages/Poets.cshtml index f0e97d23..6c041314 100644 --- a/GanjooRazor/Areas/Admin/Pages/Poets.cshtml +++ b/GanjooRazor/Areas/Admin/Pages/Poets.cshtml @@ -154,7 +154,7 @@ - ذخیره + ذخیرهٔ @poet.Nickname } diff --git a/GanjooRazor/Pages/Map.cshtml b/GanjooRazor/Pages/Map.cshtml index 12b8a8fe..7239324e 100644 --- a/GanjooRazor/Pages/Map.cshtml +++ b/GanjooRazor/Pages/Map.cshtml @@ -44,42 +44,28 @@ L.tileLayer.provider('Stamen.Watercolor').addTo(map); - var oms = new OverlappingMarkerSpiderfier(map, { keepSpiderfied : false}); - + var oms = new OverlappingMarkerSpiderfier(map, { keepSpiderfied: false }); - var hafezIcon = L.icon({ - iconUrl: 'https://ganjgah.ir/api/ganjoor/poet/image/hafez.gif', - iconSize: [82, 100], - iconAnchor: [82, 100], - popupAnchor: [0, -28] - }); + @{ + foreach (var poet in Model.PoetsWithBirthPlaces) + { + var url = poet.FullUrl.Substring(1); + + var @(url)Icon = L.icon({ + iconUrl: '@($"{GanjooRazor.APIRoot.InternetUrl}{poet.ImageUrl}")', + iconSize: [82, 100], + iconAnchor: [82, 100], + popupAnchor: [0, -28] + }); - var hafezMarker = L.marker([29.5926, 52.5836], { icon: hafezIcon }); + var @(url)Marker = L.marker([@poet.BirthPlaceLatitude, @poet.BirthPlaceLongitude], { icon: @(url)Icon }); - hafezMarker.addTo(map); - oms.addMarker(hafezMarker); + @(url)Marker.addTo(map); + oms.addMarker(@(url)Marker); + - var saadiIcon = L.icon({ - iconUrl: 'https://ganjgah.ir/api/ganjoor/poet/image/saadi.gif', - iconSize: [82, 100], - iconAnchor: [82, 100], - popupAnchor: [0, -28] - }); - - var saadiMarker = L.marker([29.5926, 52.5836], { icon: saadiIcon }); - saadiMarker.addTo(map); - oms.addMarker(saadiMarker); - - var ferdousiIcon = L.icon({ - iconUrl: 'https://ganjgah.ir/api/ganjoor/poet/image/ferdousi.gif', - iconSize: [82, 100], - iconAnchor: [82, 100], - popupAnchor: [0, -28] - }); - - var ferdousiMarker = L.marker([36.2605, 59.6168], { icon: ferdousiIcon }); - ferdousiMarker.addTo(map); - oms.addMarker(ferdousiMarker); + } + } }) diff --git a/GanjooRazor/Pages/Map.cshtml.cs b/GanjooRazor/Pages/Map.cshtml.cs index b52f7301..f7fd70a1 100644 --- a/GanjooRazor/Pages/Map.cshtml.cs +++ b/GanjooRazor/Pages/Map.cshtml.cs @@ -1,16 +1,62 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json.Linq; +using RMuseum.Models.Ganjoor.ViewModels; namespace GanjooRazor.Pages { public class MapModel : PageModel { - public void OnGet() + /// + /// Poets + /// + public List PoetsWithBirthPlaces { get; set; } + + private async Task preparePoets() { + var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/poets"); + response.EnsureSuccessStatusCode(); + var poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject>(); + + PoetsWithBirthPlaces = poets.Where(p => !string.IsNullOrEmpty(p.BirthPlace)).ToList(); + } + + public async Task OnGetAsync() + { + await preparePoets(); + return Page(); + } + + /// + /// HttpClient instance + /// + private readonly HttpClient _httpClient; + + /// + /// memory cache + /// + private readonly IMemoryCache _memoryCache; + + /// + /// configration file reader (appsettings.json) + /// + private readonly IConfiguration _configuration; + + public MapModel(IConfiguration configuration, + HttpClient httpClient, + IMemoryCache memoryCache + ) + { + _configuration = configuration; + _httpClient = httpClient; + _memoryCache = memoryCache; } } }