public List <Product> RetrieveAllProducts()
        {
            List <Product>         listOfProducts = new List <Product>();
            SpecialOfferRepository offerRepo      = new SpecialOfferRepository(new SpecialOfferMssqlContext());

            try
            {
                SqlConnection cnn    = ReturnSQLConnection();
                string        query  = "SELECT * FROM dbo.[Product]";
                SqlCommand    newCmd = CreateSQLCommandText(query, cnn);
                SqlDataReader dr     = newCmd.ExecuteReader();
                while (dr.Read())
                {
                    int          productId   = Convert.ToInt32(dr["ID"]);
                    string       productName = dr["name"].ToString();
                    SpecialOffer offer       = offerRepo.RetrieveOfferByProductID(productId);

                    if (offer.RetrieveOfferId() != -1)
                    {
                        SpecialOffer product = new SpecialOffer(offer.RetrieveOfferId(), offer.RetrieveOfferPrice(), productId, productName);
                        product.ProductAmount   = Convert.ToInt32(dr["Amount"]);
                        product.ProductDesc     = dr["Description"].ToString();
                        product.ProductDiscount = Convert.ToDecimal(dr["Discount_Price"]);
                        product.ProductPrice    = Convert.ToDecimal(dr["Price"]);
                        product.ProductStatus   = dr["Status"].ToString();
                        product.ImageUrl        = dr["ImageURL"].ToString();
                        product.BtwPercentage   = Convert.ToInt32(dr["BTWPercentage"]);
                        product.ProductInStock  = Convert.ToInt32(dr["Instock"]);
                        product.StartTime       = offer.StartTime;
                        product.EndTime         = offer.EndTime;

                        listOfProducts.Add(product);
                    }
                    else
                    {
                        Product product = new Product(productId, productName);
                        product.ProductAmount   = Convert.ToInt32(dr["Amount"]);
                        product.ProductDesc     = dr["Description"].ToString();
                        product.ProductDiscount = Convert.ToDecimal(dr["Discount_Price"]);
                        product.ProductPrice    = Convert.ToDecimal(dr["Price"]);
                        product.ProductStatus   = dr["Status"].ToString();
                        product.ImageUrl        = dr["ImageURL"].ToString();
                        product.BtwPercentage   = Convert.ToInt32(dr["BTWPercentage"]);
                        product.ProductInStock  = Convert.ToInt32(dr["Instock"]);

                        listOfProducts.Add(product);
                    }
                }
                cnn.Close();
            }
            catch (NullReferenceException e)
            {
                Console.WriteLine(e.Message);
            }
            return(listOfProducts);
        }
Exemple #2
0
 public bool UpdateOfferById(SpecialOffer o)
 {
     foreach (var single in ListOfSpecialOffers)
     {
         if (single.RetrieveOfferId() == o.RetrieveOfferId())
         {
             ListOfSpecialOffers.Remove(single);
             ListOfSpecialOffers.Add(o);
             return(true);
         }
     }
     return(false);
 }
        public bool CreateNewSpecialOffer(SpecialOffer s)
        {
            string sqlUserInsert = "INSERT INTO dbo.[SpecialOffer] (Startdate, Enddate, OfferPrice, ProductID) VALUES (@Startdate, @Enddate, @OfferPrice, @ProductId)";

            using (SqlConnection connection = ReturnSQLConnection())
            {
                int affectedRows = connection.Execute(sqlUserInsert, new { Startdate = s.StartTime, Enddate = s.EndTime, OfferPrice = s.RetrieveOfferPrice(), ProductId = s.RetrieveOfferId() });
                return(true);
            }
        }