Exemplo n.º 1
0
        public async Task <IActionResult> GetInventoryObjectSystemsAndPartsPartial(int id)
        {
            string apiUrl     = $"api/inventory/systems/byTypeId/{id}";
            var    httpClient = await _facilityManagementHttpClient.GetClient();

            var response = await httpClient.GetAsync(apiUrl).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                var responseAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var compressorSystemsViewModel = new SystemsViewModel
                {
                    InventoryObjectTypeId = id,
                    Systems = JsonConvert.DeserializeObject <ICollection <InventoryObjectSystemDTO> >(responseAsString)
                };

                return(PartialView("_SystemsPartial", compressorSystemsViewModel));
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||
                     response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                return(RedirectToAction("AccessDenied", "Authorization"));
            }

            throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}");
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Index()
        {
            //await WriteOutIdentityInformation();
            var httpClient = await _facilityManagementHttpClient.GetClient();

            var response = await httpClient.GetAsync("api/inventory").ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                var responseAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var indexViewModel = new IndexViewModel()
                {
                    InventoryObjects = JsonConvert.DeserializeObject <IList <InventoryObjectDTO> >(responseAsString).ToList()
                };

                return(View(indexViewModel));
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||
                     response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                return(RedirectToAction("AccessDenied", "Authorization"));
            }

            throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}");
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddInventoryObjectPartAjaxFormAsync(PartDetailsViewModel toAddModel)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(string.Empty, "Податоците не се валидни");
                return(PartialView("FormModals/_AddPartFormPartial", toAddModel));
            }

            // the client could validate this, but allowed for testing server errors
            if (toAddModel.Name.Length < 3)
            {
                ModelState.AddModelError(string.Empty, "Name should be longer than 2 chars");
                return(PartialView("FormModals/_AddPartFormPartial", toAddModel));
            }

            var httpClient = await _facilityManagementHttpClient.GetClient();

            var           serializedUpdatedModel = JsonConvert.SerializeObject(toAddModel);
            StringContent content  = new StringContent(serializedUpdatedModel, Encoding.Unicode, "application/json");
            var           response = await httpClient.PostAsync($"api/inventory/parts", content).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                bool isAjaxRequest = Request.Headers["X-Requested-With"] == "XMLHttpRequest";

                if (isAjaxRequest)
                {
                    return(Content("success"));
                }
                else
                {
                    return(RedirectToAction("Index", "InventoryObjects"));
                }
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||
                     response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                return(RedirectToAction("AccessDenied", "Authorization"));
            }

            throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}");
        }