Ejemplo n.º 1
0
        public async Task <IActionResult> OnGetAsync()
        {
            if (string.IsNullOrEmpty(Request.Cookies["Token"]))
            {
                return(Redirect("/"));
            }

            LastResult = "";

            if (string.IsNullOrEmpty(Request.Query["id"]))
            {
                await ReadLocationsAsync();

                Poet = new GanjoorPoetViewModel()
                {
                    Nickname    = "",
                    Name        = "",
                    FullUrl     = "",
                    Description = "",
                    Published   = false
                };
            }
            else
            {
                await PreparePoet();
            }

            return(Page());
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> OnPostSavePoetMetaAsync(int id, int birth, int death, int pinorder, bool validbirth, bool validdeath, string birthlocation, string deathlocation)
        {
            using (HttpClient secureClient = new HttpClient())
            {
                if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
                {
                    var poet = new GanjoorPoetViewModel()
                    {
                        Id = id,
                        BirthYearInLHijri = birth,
                        DeathYearInLHijri = death,
                        PinOrder          = pinorder,
                        ValidBirthDate    = validbirth,
                        ValidDeathDate    = validdeath,
                        BirthPlace        = birthlocation,
                        DeathPlace        = deathlocation
                    };
                    var response = await secureClient.PutAsync($"{APIRoot.Url}/api/ganjoor/poet/{id}", new StringContent(JsonConvert.SerializeObject(poet), Encoding.UTF8, "application/json"));

                    if (!response.IsSuccessStatusCode)
                    {
                        return(BadRequest(JsonConvert.DeserializeObject <string>(await response.Content.ReadAsStringAsync())));
                    }

                    _memoryCache.Remove($"/api/ganjoor/poets");
                    _memoryCache.Remove($"/api/ganjoor/poet/{id}");

                    return(new OkObjectResult(true));
                }
            }

            return(new OkObjectResult(false));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> OnPostEditPoetAsync(GanjoorPoetViewModel Poet)
        {
            LastResult = "";
            using (HttpClient secureClient = new HttpClient())
            {
                await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response);

                if (Request.Query["id"].ToString() == "0")
                {
                    HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/ganjoor/poet", new StringContent(JsonConvert.SerializeObject(Poet), Encoding.UTF8, "application/json"));

                    if (!response.IsSuccessStatusCode)
                    {
                        LastResult = JsonConvert.DeserializeObject <string>(await response.Content.ReadAsStringAsync());
                        return(Page());
                    }

                    var poet = JsonConvert.DeserializeObject <GanjoorPoetCompleteViewModel>(await response.Content.ReadAsStringAsync());

                    var cacheKey1 = $"/api/ganjoor/poets";
                    if (_memoryCache.TryGetValue(cacheKey1, out List <GanjoorPoetViewModel> poets))
                    {
                        _memoryCache.Remove(cacheKey1);
                    }


                    return(Redirect($"/Admin/Poet?id={poet.Poet.Id}"));
                }
                else
                {
                    HttpResponseMessage response = await secureClient.PutAsync($"{APIRoot.Url}/api/ganjoor/poet/{Request.Query["id"]}", new StringContent(JsonConvert.SerializeObject(Poet), Encoding.UTF8, "application/json"));

                    if (!response.IsSuccessStatusCode)
                    {
                        LastResult = JsonConvert.DeserializeObject <string>(await response.Content.ReadAsStringAsync());
                        return(Page());
                    }

                    var cacheKey1 = $"/api/ganjoor/poets";
                    if (_memoryCache.TryGetValue(cacheKey1, out List <GanjoorPoetViewModel> poets))
                    {
                        _memoryCache.Remove(cacheKey1);
                    }

                    var cacheKey2 = $"/api/ganjoor/poet/{Request.Query["id"]}";
                    if (_memoryCache.TryGetValue(cacheKey2, out GanjoorPoetCompleteViewModel poet))
                    {
                        _memoryCache.Remove(cacheKey2);
                    }

                    LastResult = $"ویرایش انجام شد. <a role=\"button\" href=\"/Admin/Poet?id={Request.Query["id"]}\" class=\"actionlink\">برگشت به صفحهٔ ویرایش شاعر</a>";

                    await PreparePoet();

                    return(Page());
                }
            }
        }
Ejemplo n.º 4
0
        private async Task <bool> PreparePoet()
        {
            var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/poet/{Request.Query["id"]}");

            if (!response.IsSuccessStatusCode)
            {
                LastResult = JsonConvert.DeserializeObject <string>(await response.Content.ReadAsStringAsync());
                return(false);
            }

            var poet = JsonConvert.DeserializeObject <GanjoorPoetCompleteViewModel>(await response.Content.ReadAsStringAsync());


            Poet = poet.Poet;
            return(true);
        }