public bool AddProduct(Admin admin, Product product) { if (product == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (short)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var productExist = GetProductById(admin, product.productid); if (productExist != null) throw new Exception(ErrorConstants.PRODUCT_WITH_GIVEN_ID_ALREADY_EXIST); var elasticClient = GetElasticClient(); var response = elasticClient.Index<Product>(product, i => i .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_PRODUCT) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public bool UpdateProduct(Admin admin, Product product) { if (product == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (short)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var productElasticId = GetProductId(product.productid); if(String.IsNullOrWhiteSpace(productElasticId)) { throw new Exception(ErrorConstants.PRODUCT_NOT_FOUND); } var productUpdate = new Dictionary<string, object>(); if (!String.IsNullOrWhiteSpace(product.productname)) productUpdate[ConstProduct.PRODUCT_NAME] = product.productname; if (product.price>0) productUpdate[ConstProduct.PRICE] = product.price; if (product.quantity>0) productUpdate[ConstProduct.QUANTITY] = product.quantity; if (product.unit>0) productUpdate[ConstProduct.UNIT] = product.unit; var elasticClient = GetElasticClient(); var response = elasticClient.Update<Product, object>(u => u .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_PRODUCT) .Id(productElasticId) .Doc(productUpdate) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public JsonResult AddProduct() { var id = Request.Params.Get(AppConstants.PRODUCT_ID); var name = Request.Params.Get(AppConstants.PRODUCT_NAME); var price = Request.Params.Get(AppConstants.PRICE); var quantity = Request.Params.Get(AppConstants.QUANTITY); var unit = Request.Params.Get(AppConstants.UNIT); var response = new ServiceResponse(); var admin = CookieHelper.GetLoggedInAdmin(HttpContext); if(admin == null) { response.result = ErrorConstants.ADMIN_NOT_LOGGED_IN; return Json(response); } if(String.IsNullOrWhiteSpace(id) || String.IsNullOrWhiteSpace(name) || String.IsNullOrWhiteSpace(price) || String.IsNullOrWhiteSpace(quantity) || String.IsNullOrWhiteSpace(unit)) { response.result = ErrorConstants.REQUIRED_FIELD_EMPTY; return Json(response); } double priceDouble; float quantityFloat; Int16 unitInt; if(!Double.TryParse(price, out priceDouble) || !float.TryParse(quantity, out quantityFloat) || !Int16.TryParse(unit, out unitInt)) { response.result = ErrorConstants.INVALID_DATA; return Json(response); } try { var product = new Product() { productid = id, productname = name, price = priceDouble, quantity = quantityFloat, unit = unitInt, create_at = DateTime.UtcNow }; if(productManager.AddProduct(admin, product)) { response.result = SuccessConstants.PRODUCT_ADDED; response.status = true; } else response.result = ErrorConstants.PROBLEM_ADDING_PRODUCT; return Json(response); } catch (Exception e) { Console.Error.WriteLine(e.GetBaseException().Message); response.result = e.GetBaseException().Message; } return Json(response); }