public async Task <ActionResult> Add([FromForm] AddProductModel model) { try { if (ModelState.IsValid) { ResJsonOutput result = await PostDataAsync("/API/Products/AddProduct", model); if (result.Status.IsSuccess) { return(Json(new AjaxResposnse { Success = true, Message = "Product added successfully.", URL = Url.Action("List", "Products") })); } else { return(Json(new AjaxResposnse { Message = result.Status.Message })); } } else { ViewBag.ErrMsg = GetModelStateErrors(ModelState); } } catch (Exception) { throw; } return(View(model)); }
public async Task <JsonResult> Delete(RequestById model) { ResJsonOutput result = new ResJsonOutput(); try { if (ModelState.IsValid) { result = await PostDataAsync("/API/Products/DeleteProduct", new RequestById { ProductId = model.ProductId }); if (result.Status.IsSuccess) { return(Json(new { Success = true, Message = "Product deleted successfully." })); } } else { ViewBag.ErrMsg = GetModelStateErrors(ModelState); } } catch (Exception) { throw; } return(Json(new AjaxResposnse { Message = result.Status.Message })); }
/// <summary> /// Get records for datatable /// </summary> /// <param name="param"></param> /// <returns></returns> //[HttpPost] public async Task <ActionResult> GetList(JqueryDatatableParam param) { ProductDatatable productDatatable = new ProductDatatable(); try { ProductListFilter model = new ProductListFilter { Start = param.Start, Length = param.Length, Search = param.Search.Value, SortColumn = Convert.ToString(param.Columns[param.Order[0].Column].Data), SortOrder = Convert.ToString(param.Order[0].Dir) }; ResJsonOutput result = await PostDataAsync("/API/Products/GetProductList", model); if (result.Status.IsSuccess) { productDatatable = Utility.ConvertJsonToObject <ProductDatatable>(result.Data); } } catch (Exception ex) { throw; } return(Json(new { draw = param.Draw, data = productDatatable.Products, recordsFiltered = productDatatable.TotalRecords, recordsTotal = productDatatable.TotalDisplayRecord })); }
public async Task <ResJsonOutput> UploadFile(IFormFile file) { ResJsonOutput result = new ResJsonOutput(); if (file == null || file.Length == 0) { result.Status.Message = "file not selected"; } else { string folderName = "Products"; string strpath = Path.Combine(_hostingEnvironment.WebRootPath, folderName); Utility.CheckDir(strpath); string FileName = Utility.EpochTime() + "_" + file.FileName; strpath = strpath + "\\" + FileName; using (var stream = new FileStream(strpath, FileMode.Create)) { file.CopyTo(stream); } result.Data = folderName + "\\" + FileName; result.Status.IsSuccess = true; } return(result); }
public async Task <ActionResult> Details(long id) { Product product = new Product(); try { ResJsonOutput result = await PostDataAsync("/API/Products/GetProductDetails", new RequestById { ProductId = id }); if (result.Status.IsSuccess) { product = Utility.ConvertJsonToObject <Product>(result.Data); } else { return(Json(new AjaxResposnse { Message = result.Status.Message })); } } catch (Exception) { throw; } return(View(product)); }
public static async Task <ResJsonOutput> PostDataAsync <T>(string ApiPath, object obj, List <KeyValue> Headers = null) { ResJsonOutput result = new ResJsonOutput(); HttpResponseMessage response = new HttpResponseMessage(); try { using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(ApiPath); client.DefaultRequestHeaders.Accept.Clear(); if (Headers != null) { foreach (var item in Headers.Where(c => c.Value.IsNullString() != string.Empty)) { client.DefaultRequestHeaders.TryAddWithoutValidation(item.Key, item.Value); } } response = await client.PostAsync(ApiPath, new StringContent(ConvertObjectToJson(obj), Encoding.UTF8, "application/json"));//new StringContent(ConvertObjectToJson(obj)) result = response.Content.ReadAsJsonAsync <ResJsonOutput>().Result; } } catch (Exception ex) { result.Status.Message = ex.Message; } return(result); }
public ActionResult <ResJsonOutput> GetProductList([FromBody] ProductListFilter param) { ResJsonOutput result = new ResJsonOutput(); try { ProductDatatable productDatatable = new ProductDatatable(); List <Product> list = product.GetAll().ToList(); if (list != null && list.Count() > 0) { productDatatable.TotalDisplayRecord = productDatatable.TotalRecords = list.Count(); if (!string.IsNullOrEmpty(param.Search)) { list = list.Where(x => x.ProductName.ToLower().Contains(param.Search.ToLower()) || x.ProductPrice.ToString().ToLower().Contains(param.Search.ToLower()) ).ToList(); productDatatable.TotalRecords = productDatatable.TotalDisplayRecord = list.Count(); } list = SortedDataByColumn(list, param.SortColumn, param.SortOrder); list = list.Skip(param.Start).Take(param.Length).ToList(); } productDatatable.Products = list; result.Data = productDatatable; result.Status.IsSuccess = true; } catch (Exception ex) { result = GetException(ex); } return(result); }
public ActionResult <ResJsonOutput> GetProductDetails([FromBody] RequestById model) { ResJsonOutput result = new ResJsonOutput(); try { result.Data = product.Get(model.ProductId); result.Status.IsSuccess = true; } catch (Exception ex) { result = GetException(ex); } return(result); }
public async Task <ResJsonOutput> PostDataAsync(string ApiPath, object obj = null) { try { return(await Utility.PostDataAsync <ResJsonOutput>(_config.GetSection("APIURL").Value + ApiPath, obj)); } catch (Exception) { ResJsonOutput result = new ResJsonOutput(); result.Status.IsSuccess = false; result.Status.StatusCode = "GNLERR"; result.Status.Message = "Error while procedding your request."; return(result); } }
public ActionResult <ResJsonOutput> DeleteProduct([FromBody] RequestById model) { ResJsonOutput result = new ResJsonOutput(); try { Product _product = product.Get(model.ProductId); product.Delete(_product); result.Status.IsSuccess = true; } catch (Exception ex) { result = GetException(ex); } return(result); }
/// <summary> /// Get Formatted Exception /// </summary> /// <param name="ex"></param> /// <returns></returns> private ResJsonOutput GetException(Exception ex) { ResJsonOutput result = new ResJsonOutput(); string ControllerName = RouteData != null ? RouteData.Values["controller"].ToString() : ""; string ActionName = RouteData != null ? RouteData.Values["action"].ToString() : ""; ErrorLog errorLog = new ErrorLog { Exception = Convert.ToString(ex), Message = ex.Message, StackTrace = ex.StackTrace, CreatedOn = DateTime.UtcNow }; log.Insert(errorLog); result.Status.StatusCode = "GNERR"; result.Status.Message = "Error while processing you request."; return(result); }
public ActionResult <ResJsonOutput> AddProduct([FromBody] AddProductModel model) { ResJsonOutput result = new ResJsonOutput(); try { Product _product = new Product { ProductName = model.ProductName, ProductDescription = model.ProductDescription, ProductPrice = model.ProductPrice, ProductImage = model.ProductImage, CreatedOn = DateTime.UtcNow }; product.Insert(_product); result.Data = _product.ProductId; result.Status.IsSuccess = true; } catch (Exception ex) { result = GetException(ex); } return(result); }