Esempio n. 1
0
        public async Task <IActionResult> EditResource(int resourceId)
        {
            var                 requestUrl          = $"{BaseUrl}{apiUriResources}/GetResourceDetails?resourceId={resourceId}";
            ResourceDTO         resource            = new ResourceDTO();
            PopulateDropdownDTO populateDropdownDTO = new PopulateDropdownDTO();

            populateDropdownDTO = await StaticDataHandler.PopulateResourceDropdown(BaseUrl);

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(requestUrl);

                HttpResponseMessage result = await client.GetAsync(requestUrl);

                if (result.StatusCode == HttpStatusCode.OK)
                {
                    resource = await result.Content.ReadAsAsync <ResourceDTO>();
                }

                else
                {
                    resource.OutputHandler = await result.Content.ReadAsAsync <OutputHandler>();
                }
                resource.ResourceCategories = populateDropdownDTO.ResourceCategories;
                resource.ResourceTypes      = populateDropdownDTO.ResourceTypes;
            };


            resource.ResourceCategories = populateDropdownDTO.ResourceCategories;
            resource.ResourceTypes      = populateDropdownDTO.ResourceTypes;
            resource.OldImageUrl        = resource.ImageUrl;
            return(View(resource));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddResource()
        {
            PopulateDropdownDTO populateDropdownDTO = new PopulateDropdownDTO();

            populateDropdownDTO = await StaticDataHandler.PopulateResourceDropdown(BaseUrl);

            var resourceDTO = new ResourceDTO
            {
                ResourceCategories = populateDropdownDTO.ResourceCategories,
                ResourceTypes      = populateDropdownDTO.ResourceTypes
            };

            return(View(resourceDTO));
        }
Esempio n. 3
0
        public async Task <IActionResult> EditResource(ResourceDTO resource, IFormFile artwork)
        {
            var user = await StaticDataHandler.GetSessionDetails();

            PopulateDropdownDTO populateDropdownDTO = new PopulateDropdownDTO();

            populateDropdownDTO = await StaticDataHandler.PopulateResourceDropdown(BaseUrl);

            try
            {
                resource.CreatedBy   = "Denzel";
                resource.DateCreated = DateTime.Now.AddHours(2);
                if (ModelState.IsValid)
                {
                    if (artwork == null)
                    {
                    }
                    else
                    {
                        var fileUploadResult = await StaticDataHandler.fileUpload(artwork, FolderName, resource.IsFeatured);

                        if (fileUploadResult.IsErrorOccured)
                        {
                            return(View(resource.OutputHandler = new OutputHandler
                            {
                                IsErrorOccured = true,
                                Message = "Something went wrong"
                            }));
                        }
                        else
                        {
                            resource.Artwork  = (byte[])fileUploadResult.Result; //return the byte data
                            resource.Filename = artwork.FileName;
                        }
                    }
                    var requestUrl = $"{BaseUrl}{apiUriResources}/EditResource";
                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(requestUrl);
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", user.Token);

                        var result = await client.PostAsJsonAsync(client.BaseAddress, resource);

                        if (result.StatusCode == HttpStatusCode.OK)
                        {
                            return(RedirectToAction("ResourcesForAdmin"));
                        }
                        else if (result.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            resource.OutputHandler = new OutputHandler
                            {
                                IsErrorOccured = true,
                                Message        = "You're not Authorized to perfom this task"
                            };
                        }
                        else
                        {
                            populateDropdownDTO = await StaticDataHandler.PopulateResourceDropdown(BaseUrl);

                            resource.ResourceCategories = populateDropdownDTO.ResourceCategories;
                            resource.ResourceTypes      = populateDropdownDTO.ResourceTypes;
                            resource.OutputHandler      = await result.Content.ReadAsAsync <OutputHandler>();
                        }
                    };
                }
                else
                {
                    resource.ResourceCategories = await StaticDataHandler.GetResourceCategory(BaseUrl);
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException.Message != null)
                {
                    resource.OutputHandler = new OutputHandler {
                        IsErrorOccured = true, Message = ex.InnerException.Message
                    };
                }
                else
                {
                    resource.OutputHandler = new OutputHandler {
                        IsErrorOccured = true, Message = ex.Message
                    };
                }
            }


            resource.ResourceCategories = populateDropdownDTO.ResourceCategories;
            resource.ResourceTypes      = populateDropdownDTO.ResourceTypes;
            return(View(resource));
        }