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 (var conn = Config.DB.Open())
            {
                List<Coupon> lCoupons = new List<Coupon>();

                using (var reader = SqlHelper.GetDB().ExecuteReader("FetchCoupons"))
                {

                    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);
                    }
                    reader.Close();
                }

                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 (var conn = Config.DB.Open())
            {
                using (var reader = 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"];
                    }
                    reader.Close();
                }
            }
            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;
        }