using GanjooRazor.Models.MuseumLink; using GanjooRazor.Utils; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Newtonsoft.Json; using RMuseum.Models.GanjoorIntegration.ViewModels; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace GanjooRazor.Pages { [IgnoreAntiforgeryToken(Order = 1001)] public class PinModel : PageModel { /// /// related image suggestion model /// [BindProperty] public RelatedImageSuggestionModel RelatedImageSuggestionModel { get; set; } /// /// ارسال موفق /// public bool Succeeded { get; set; } /// /// خطا /// public string LastError { get; set; } /// /// is logged on /// public bool LoggedIn { get; set; } public async Task OnPostAsync() { Succeeded = false; LastError = ""; LoggedIn = !string.IsNullOrEmpty(Request.Cookies["Token"]); if(!LoggedIn) { LastError = $"برای پیشنهاد تصاویر مرتبط با اشعار لازم است ابتدا با نام کاربری خود وارد گنجور شوید.

ورود به گنجور"; } else if (Request.Query["final"] == "1") { using (HttpClient secureClient = new HttpClient()) { if(await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response)) { PinterestLinkViewModel model = new PinterestLinkViewModel() { GanjoorPostId = RelatedImageSuggestionModel.PoemId, GanjoorUrl = $"https://ganjoor.net{RelatedImageSuggestionModel.GanjoorUrl}", GanjoorTitle = RelatedImageSuggestionModel.GanjoorTitle, AltText = RelatedImageSuggestionModel.AltText, LinkType = RMuseum.Models.GanjoorIntegration.LinkType.Pinterest, PinterestUrl = RelatedImageSuggestionModel.PinterestUrl, PinterestImageUrl = RelatedImageSuggestionModel.PinterestImageUrl }; var stringContent = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"); var response = await secureClient.PostAsync($"{APIRoot.Url}/api/artifacts/pinterest", stringContent); if (response.StatusCode != HttpStatusCode.OK) { LastError = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); } else { Succeeded = true; } } else { LastError = "لطفاً از گنجور خارج و مجددا به آن وارد شوید."; } } } return Page(); } } }