Example #1
0
        /// <summary>
        /// Fetches all.
        /// </summary>
        /// <returns></returns>
        public static Coupon[] FetchAll()
        {
            string cacheKey = String.Format("Coupon_FetchAll");
            if (HttpContext.Current != null && HttpContext.Current.Cache[cacheKey] != null)
            {
                return HttpContext.Current.Cache[cacheKey] as Coupon[];
            }

            using (SqlConnection conn = Config.DB.Open())
            {
                SqlDataReader reader = (SqlDataReader) SqlHelper.GetDB().ExecuteReader( "FetchCoupons");

                List<Coupon> lCoupons = new List<Coupon>();

                while (reader.Read())
                {
                    Coupon coupon = new Coupon();
                    coupon.id = (int)reader["Id"];
                    coupon.code = (string)reader["Code"];
                    coupon.startDate = (DateTime)reader["StartDate"];
                    coupon.dueDate = (DateTime)reader["DueDate"];
                    coupon.maxUse = (int)reader["MaxUse"];
                    coupon.couponTypeId = (int)reader["TypeId"];
                    coupon.username = (string)reader["Username"];
                    coupon.used = (int)reader["Used"];
                    coupon.OnlyForNewUser = (bool)reader["OnlyNewUser"];
                    lCoupons.Add(coupon);
                }

                Coupon[] coupons = lCoupons.ToArray();

                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Cache.Insert(cacheKey, coupons, null, DateTime.Now.AddMinutes(30),
                            Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
                }
                return coupons;
            }
        }
Example #2
0
        public static Coupon Fetch(string code)
        {
            Coupon coupon = new Coupon();

            using (SqlConnection conn = Config.DB.Open())
            {
                SqlDataReader reader = (SqlDataReader) SqlHelper.GetDB().ExecuteReader( "FetchCoupon", code);

                if (reader.Read())
                {
                    coupon.id = (int)reader["Id"];
                    coupon.code = (string)reader["Code"];
                    coupon.startDate = (DateTime)reader["StartDate"];
                    coupon.dueDate = (DateTime)reader["DueDate"];
                    coupon.maxUse = (int)reader["MaxUse"];
                    coupon.couponTypeId = (int)reader["TypeId"];
                    coupon.used = (int)reader["Used"];
                    coupon.username = (string)reader["Username"];
                    coupon.OnlyForNewUser = (bool)reader["OnlyNewUser"];
                }
            }
            return coupon;
        }
Example #3
0
        /// <summary>
        /// Creates the specified code.
        /// </summary>
        /// <returns>Coupon with generated code</returns>
        public static Coupon Create()
        {
            Coupon[] coupons = FetchAll();
            string code = GenerateCode();
            var codes = coupons.Select(c => c.Code).ToList();

            while (codes.Contains(code))
            {
                code = GenerateCode();
            }
            Coupon coupon = new Coupon();
            coupon.Code = code;
            coupon.id = -1;
            coupon.StartDate = DateTime.Today;
            coupon.DueDate = DateTime.Today.AddMonths(1);
            coupon.OnlyForNewUser = true;
            return coupon;
        }