Example #1
0
        public async Task <IActionResult> Create(CouponCreateItemViewModel couponModel)
        {
            if (couponModel.DiscountTypeText.Equals(Constants.DISCOUNT_TYPE_PERCENT))
            {
                if (couponModel.Discount < 0 || couponModel.Discount > 100)
                {
                    ModelState.AddModelError("Discount", "Out of range of possible values");
                }
            }
            if (couponModel.Image == null)
            {
                ModelState.AddModelError("Image", "Please, provide image for coupon");
            }
            if (ModelState.IsValid)
            {
                var coupon = await _couponService.CreateCouponAsync(couponModel);

                // for test only! AA
                //    var location = Helpers.CookiesHelper.GetGeolocation(HttpContext);

                //    var coupon = new Coupon
                //    {
                //        TotalCapacity = couponItem.TotalCapacity,
                //        CurrentCapacity = couponItem.CurrentCapacity,
                //        CouponName = couponItem.CouponName,
                //        Id = Guid.NewGuid()
                //        //MerchantUser = _userManager.Users.FirstOrDefault(item => item.Id == couponItem.MerchantUserId),

                //    };
                //    using (var memoryStream = new MemoryStream())
                //    {
                //        await couponItem.CouponImage.CopyToAsync(memoryStream);
                //        coupon.CouponImage = memoryStream.ToArray();
                //    }
                //    _context.Add(coupon);
                //    await _context.SaveChangesAsync();
                return(RedirectToAction(nameof(Index)));
            }
            couponModel.Products     = _couponService.GetSelectListProducts();
            couponModel.DiscountType = _couponService.GetSelectListDiscountTypes();
            couponModel.Networks     = _couponService.GetMultiSelectListNetworks();
            return(View(couponModel));
        }
Example #2
0
        public async Task <IActionResult> CreateCoupon([FromBody] CreateCouponRequestDto dto)
        {
            try
            {
                var coupon = await _couponService.CreateCouponAsync(dto);

                return(Ok(coupon));
            }
            catch (NotFoundException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (BadRequestException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                _logger.LogCritical($"CreateCoupon() error {ex}");
                return(StatusCode(500, "Internal Server Error: " + ex.Message));
            }
        }