Пример #1
0
        public IHttpActionResult Create(UserCouponPackageCM cm)
        {
            try
            {
                UserCouponPackage model = AutoMapper.Mapper.Map <UserCouponPackageCM, UserCouponPackage>(cm);
                model.PurchasedDate = DateTime.Now; // Get Current time
                model.UserId        = CURRENT_USER_ID;

                CouponPackage package = _couponPackageService.GetCouponPackage(model.CouponPackageId);

                // set user package property
                model.DrinkQuantity = package.DrinkQuantity;
                model.Price         = package.Price;

                // Current hour, minute, second
                int cHour   = model.PurchasedDate.Hour;
                int cMinute = model.PurchasedDate.Minute;
                int cSecond = model.PurchasedDate.Second;

                // CREATE 30 coupon item for this user package
                model.CouponItems = new List <CouponItem>();
                for (int i = 0; i < 30; i++)
                {
                    CouponItem newCouponItem = new CouponItem()
                    {
                        IsUsed      = false,
                        DateExpired = model.PurchasedDate.AddDays(i)
                                      .AddHours(24 - cHour - 1)
                                      .AddMinutes(60 - cMinute - 1)
                                      .AddSeconds(60 - cSecond - 1),
                        OrderId = null
                    };
                    model.CouponItems.Add(newCouponItem);
                }
                // CREATE USER PACKAGE
                _userCouponPackageService.CreateUserCouponPackage(model);
                _userCouponPackageService.SaveUserCouponPackageChanges();

                UserCouponPackageVM result = AutoMapper.Mapper.Map <UserCouponPackage, UserCouponPackageVM>(model);
                return(Ok(result));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
Пример #2
0
        public IHttpActionResult Create(UserCouponPackageCM cm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                UserCouponPackage model = AutoMapper.Mapper.Map <UserCouponPackageCM, UserCouponPackage>(cm);
                model.CouponItems   = new List <CouponItem>();
                model.PurchasedDate = DateTime.Now;

                // Current hour, minute, second
                int cHour   = model.PurchasedDate.Hour;
                int cMinute = model.PurchasedDate.Minute;
                int cSecond = model.PurchasedDate.Second;

                // CREATE 30 coupon item for this user package
                for (int i = 1; i <= 30; i++)
                {
                    CouponItem newCouponItem = new CouponItem()
                    {
                        IsUsed      = false,
                        DateExpired = model.PurchasedDate.AddDays(i)
                                      .AddHours(24 - cHour - 1)
                                      .AddMinutes(60 - cMinute - 1)
                                      .AddSeconds(60 - cSecond - 1),
                        OrderId = null
                    };
                    model.CouponItems.Add(newCouponItem);
                }
                // CREATE USER PACKAGE
                _userCouponPackageService.CreateUserCouponPackage(model);
                _userCouponPackageService.SaveUserCouponPackageChanges();

                UserCouponPackageVM result = AutoMapper.Mapper.Map <UserCouponPackage, UserCouponPackageVM>(model);
                return(Ok(result));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }