Пример #1
0
        public async Task <IActionResult> Index(string searchField, string search, int?page)
        {
            List <Product> product = new List <Product>();

            HttpResponseMessage serviceResponse = await ClientHelper.CallApi(Common.BaseUrl, null, "", HTTPMODE.GETALL);

            //Checking the response is successful or not which is sent using HttpClient
            if (serviceResponse.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var productResponse = serviceResponse.Content.ReadAsStringAsync().Result;
                product = JsonConvert.DeserializeObject <List <Product> >(productResponse);
            }

            if (searchField == "Name")
            {
                return(View(product.Where(x => (search != null && x.Name.ToLower().Contains(search.ToLower())) || search == null)
                            .ToList().ToPagedList(page ?? 1, 5)));
            }
            else
            {
                return(View(product.Where(x => (search != null && x.Code.ToLower().Contains(search.ToLower())) || search == null)
                            .ToList().ToPagedList(page ?? 1, 5)));
            }
        }
Пример #2
0
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            HttpResponseMessage serviceResponse = await ClientHelper.CallApi(Common.BaseUrl, null, "", HTTPMODE.DELETE, id);

            //Checking the response is successful or not which is sent using HttpClient
            if (serviceResponse.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var productResponse = serviceResponse.Content.ReadAsStringAsync().Result;
            }

            return(RedirectToAction(nameof(Index)));
        }
Пример #3
0
        public async Task <ActionResult> Delete(int id)
        {
            Product             product         = new Product();
            HttpResponseMessage serviceResponse = await ClientHelper.CallApi(Common.BaseUrl, null, "", HTTPMODE.GET, id);

            //Checking the response is successful or not which is sent using HttpClient
            if (serviceResponse.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var productResponse = serviceResponse.Content.ReadAsStringAsync().Result;
                product = JsonConvert.DeserializeObject <Product>(productResponse);
            }
            return(View(product));
        }
Пример #4
0
        public async Task <ActionResult> Edit(int id, [Bind("Id", "Name", "Price", "Code", "Photo", "RowVersion")] CatalogItem catalogitem)
        {
            Product product = await MapProductObject(catalogitem);

            HttpResponseMessage serviceResponse = await ClientHelper.CallApi(Common.BaseUrl, product, "Product", HTTPMODE.PUT, id);

            //Checking the response is successful or not which is sent using HttpClient
            if (serviceResponse.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var apiResponse = serviceResponse.Content.ReadAsStringAsync().Result;
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                ModelState.AddModelError("UpdateError", "Unable to save changes. This may be due to concurrent data updates. Please go back to the list and try again.");
                return(View(product));
            }
        }
Пример #5
0
        public async Task <IActionResult> FileReport()
        {
            List <Product>      product         = new List <Product>();
            HttpResponseMessage serviceResponse = await ClientHelper.CallApi(Common.BaseUrl, null, "", HTTPMODE.GETALL);

            //Checking the response is successful or not which is sent using HttpClient
            if (serviceResponse.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var productResponse = serviceResponse.Content.ReadAsStringAsync().Result;
                product = JsonConvert.DeserializeObject <List <Product> >(productResponse);
            }

            using (var package = ExcelHelper.CreateExcelPackage <Product>(product))
            {
                package.SaveAs(new FileInfo(Path.Combine(_hostingEnvironment.WebRootPath, Common.ExportFolder, Common.FileDownLoadName)));
            }

            return(File($"~/{Common.ExportFolder}/{Common.FileDownLoadName}", Common.XlsxContentType, Common.FileDownLoadName));
        }
Пример #6
0
        public async Task <ActionResult> Create([FromForm] CatalogItem catalogItem)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Product product = await MapProductObject(catalogItem);

                    HttpResponseMessage serviceResponse = await ClientHelper.CallApi(Common.BaseUrl, product, "Product", HTTPMODE.POST);

                    if (!serviceResponse.IsSuccessStatusCode)
                    {
                        ModelState.AddModelError("InsertError", "Unable to save changes. Please check all data entries and ensure product code is not dupicate.");
                        return(View(product));
                    }
                }
            }
            catch
            {
                return(View());
            }

            return(RedirectToAction(nameof(Index)));
        }