public JsonResult AddProductsToCart(Product p) { DataProvider.AddProductToCart(p, ShallvaUser.Current.Id); return Json(new { }); }
public static Product GetProduct(int productId, out List<Product> similarProducts) { Product product = null; similarProducts = null; using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING].ConnectionString)) { using (SqlCommand cmd = new SqlCommand("GetProduct", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@ProductId", productId)); conn.Open(); using (IDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { product = new Product() { Id = reader.GetInt32(0), Name = reader.GetString(3), ImageName = reader.GetString(1), SmallImageName = reader.GetString(5), CategoryId = reader.GetInt32(6), SubCategoryId = reader.GetInt32(2), CategoryName = reader.GetString(7), SubCategoryName = !reader.IsDBNull(4) ? reader.GetString(4) : string.Empty, PreviousProductId = reader.IsDBNull(12) ? null : (int?)reader.GetInt32(12), NextProductId = reader.IsDBNull(13) ? null : (int?)reader.GetInt32(13), }; } if (reader.NextResult() && reader.Read()) { product.Name = reader.GetString(0); } if (reader.NextResult()) { similarProducts = new List<Product>(); while (reader.Read()) { Product p = new Product() { Id = reader.GetInt32(0), SmallImageName = reader.GetString(1), Name = reader.GetString(2) }; similarProducts.Add(p); } } if (reader.NextResult()) { product.OrderProducts = new List<OrderProduct>(); int prodId = -1; int subProdId = -1; int prodIndex = -1; int propIndex = -1; while (reader.Read()) { int currentProdId = reader.GetInt32(6); if (prodId != currentProdId) { propIndex = -1; prodIndex++; prodId = currentProdId; OrderProduct op = new OrderProduct(); op.Id = prodId; op.Name = reader.GetString(5); op.Properties = new List<ProductProperty>(); op.SubProducts = new List<SubProduct>(); product.OrderProducts.Add(op); } int currentSubProdId = reader.GetInt32(0); if (currentSubProdId != subProdId) { subProdId = currentSubProdId; propIndex++; SubProduct sp = new SubProduct(); sp.Id = subProdId; sp.Name = reader.GetString(4); sp.PropertiesValues = new List<ProductProperty>(); sp.SKU = reader.GetString(1); product.OrderProducts[prodIndex].SubProducts.Add(sp); } int propId = reader.GetInt32(10); string propName = reader.GetString(11); var prop = new ProductProperty() { Id = propId, Name = propName }; if (!product.OrderProducts[prodIndex].Properties.Any(x => x.Id == propId)) { product.OrderProducts[prodIndex].Properties.Add(prop); } product.OrderProducts[prodIndex].SubProducts[propIndex].PropertiesValues.Add(prop); } } } conn.Close(); } } return product; }
public static List<Product> GetProductsByCriteria(ProductsCriteria criteria, out CategoryListItem nextCategory) { nextCategory = null; List<Product> products = new List<Product>(); using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING].ConnectionString)) { using (SqlCommand cmd = new SqlCommand("GetProductsByCriteria", conn)) { cmd.CommandType = CommandType.StoredProcedure; if (criteria.CategoryId != null) { cmd.Parameters.Add(new SqlParameter("@MainCat", criteria.CategoryId.Value)); } cmd.Parameters.Add(new SqlParameter("@PageNumber", criteria.PageNumber)); cmd.Parameters.Add(new SqlParameter("@PageSize", criteria.PageSize)); if (criteria.ProductId != null && criteria.ProductId.Value > 0) { cmd.Parameters.Add(new SqlParameter("@ProductId", criteria.ProductId.Value)); } conn.Open(); using (IDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { Product p = new Product() { Id = reader.GetInt32(0), Name = reader.GetString(3), ImageName = reader.GetString(1), SmallImageName = reader.GetString(5), CategoryId = reader.GetInt32(6), SubCategoryId = reader.GetInt32(2), CategoryName = reader.GetString(7), SubCategoryName = reader.IsDBNull(4) ? string.Empty : reader.GetString(4), PreviousProductId = reader.IsDBNull(12) ? null : (int?)reader.GetInt32(12), NextProductId = reader.IsDBNull(13) ? null : (int?)reader.GetInt32(13), }; products.Add(p); } if (criteria.CategoryId != null && reader.NextResult()) { if (reader.Read()) { nextCategory = new CategoryListItem() { Id = reader.GetInt32(0), Name = reader.GetString(1) }; } } } conn.Close(); } } return products; }
public static void AddProductToCart(Product product, int userId) { DateTime now = DateTime.Now; OrderDay orderDay = GetOrderDay(userId); string qTemplate = "INSERT INTO orders(userId," + "day," + "month," + "year," + "product_id,property_id," + "makat,quantity," + "full_order_id," + "full_date)" + "VALUES (" + userId + "," + now.Day.ToString() + "," + now.Month.ToString() + "," + now.Year.ToString() + "," + "{0}," + //@product_id "N'{1}'," + // @property_id "N'{2}'," + // @makat "{3}," + //@quantity "{4}," + //@full_order_id "GETDATE())\n"; string query = string.Empty; foreach (var op in product.OrderProducts) { foreach (var sp in op.SubProducts) { var props = sp.PropertiesValues.Where(x => x.Count > 0); if (props != null && props.Count() > 0) { foreach (var prop in props) { query += string.Format(qTemplate, sp.Id, prop.Name, sp.SKU, prop.Count, orderDay.Id); } } } } if (!string.IsNullOrEmpty(query)) { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING].ConnectionString)) { using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.CommandType = CommandType.Text; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } Cart.Products.ResetCart(); } }