private IHttpActionResult ValidateProductData(ProductData product, string token, bool editing) { if (product == null) { return(BadRequest("Product data is missing")); } if (!editing && !product.IsComplete()) { return(BadRequest("Product data missing some required field")); } if (product.Price != null && product.Price <= 0) { return(BadRequest("Product price must be a positive decimal number")); } if (product.Units != null && product.Units < 1) { return(BadRequest("Product units must be a positive integer")); } if (product.SellerId != null) { DataAccessSoapClient ws = new DataAccessSoapClient(); User seller = ws.FindUser(new DataAccessWS.Security { BinarySecurityToken = token }, product.SellerId.Value); if (seller == null || seller.Role != DataAccessWS.UserRole.SELLER) { return(BadRequest("Seller with id " + product.SellerId.Value + " not found in the system")); } } if (product.CategoryId != null) { DataAccessSoapClient ws = new DataAccessSoapClient(); Category category = ws.FindCategory(new DataAccessWS.Security { BinarySecurityToken = token }, product.CategoryId.Value); if (category == null) { return(BadRequest("Category with id " + product.CategoryId.Value + " not found in the system")); } } return(null); }
private void assignProperties(Product product, ProductData data, string token) { if (!string.IsNullOrEmpty(data.Name)) { product.Name = data.Name; } if (!string.IsNullOrEmpty(data.Description)) { product.Description = data.Description; } if (data.Price != null) { product.Price = data.Price.Value; } if (data.Units != null) { product.Units = data.Units.Value; } if (data.Image != null) { product.image = data.Image; } if (data.SellerId != null) { product.seller_id = data.SellerId.Value; DataAccessSoapClient ws = new DataAccessSoapClient(); dynamic user = ws.FindUser(new DataAccessWS.Security { BinarySecurityToken = token }, product.seller_id); product.seller = user; } if (data.CategoryId != null) { product.category_id = data.CategoryId.Value; DataAccessSoapClient ws = new DataAccessSoapClient(); dynamic category = ws.FindCategory(new DataAccessWS.Security { BinarySecurityToken = token }, product.category_id); product.category = category; } }