public async Task <IActionResult> Create(AdminCouponListingServiceModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(model));
            }

            var files = HttpContext.Request.Form.Files;

            if (files.Count > 0)
            {
                byte[] pic1 = null;
                using (var fileStream1 = files[0].OpenReadStream())
                {
                    using (var memoryStream1 = new MemoryStream())
                    {
                        fileStream1.CopyTo(memoryStream1);
                        pic1 = memoryStream1.ToArray();
                    }
                }

                model.Picture = pic1;
                bool isSuccessful = await this.couponService.CreateAsync(model);

                if (isSuccessful)
                {
                    return(this.RedirectToAction(nameof(Index)));
                }

                return(this.View(model));
            }

            return(this.View(model));
        }
        public async Task <bool> CreateAsync(AdminCouponListingServiceModel model)
        {
            var couponExists = await this.context.Coupons.AnyAsync(c => c.Name == model.Name);

            var type   = ((CouponType)(int.Parse(model.CouponType)));
            var coupon = new Coupon()
            {
                Id            = model.Id,
                Name          = model.Name,
                CouponType    = model.CouponType,
                Picture       = model.Picture,
                ECouponType   = type,
                Discount      = model.Discount,
                MinimumAmount = model.MinimumAmount,
                IsActive      = model.IsActive
            };

            if (!couponExists)
            {
                await this.context.Coupons.AddAsync(coupon);

                int result = await this.context.SaveChangesAsync();

                if (result > 0)
                {
                    return(true);
                }

                return(false);
            }

            return(false);
        }
        public async Task <IActionResult> EditAsync(int id)
        {
            this.CouponVM = await this.couponService.GetByIdAsync(id);

            if (this.CouponVM == null)
            {
                return(NotFound());
            }

            return(this.View(this.CouponVM));
        }
        public async Task <bool> DeleteAsync(AdminCouponListingServiceModel model)
        {
            var couponFromDb = await this.context.Coupons.FirstAsync(x => x.Id == model.Id);

            this.context.Remove(couponFromDb);
            var success = await this.context.SaveChangesAsync();

            if (success > 0)
            {
                return(true);
            }

            return(false);
        }
        public async Task <AdminCouponListingServiceModel> GetByIdAsync(int id)
        {
            var couponFromDb = await this.context.Coupons.FirstOrDefaultAsync(x => x.Id == id);

            if (couponFromDb == null)
            {
                return(null);
            }

            var model = new AdminCouponListingServiceModel()
            {
                Id            = couponFromDb.Id,
                Name          = couponFromDb.Name,
                CouponType    = couponFromDb.CouponType,
                Picture       = couponFromDb.Picture,
                ECouponType   = couponFromDb.ECouponType,
                Discount      = couponFromDb.Discount,
                MinimumAmount = couponFromDb.MinimumAmount,
                IsActive      = couponFromDb.IsActive
            };

            return(model);
        }
        public async Task <bool> UpdateAsync(AdminCouponListingServiceModel model)
        {
            var couponFromDb = await this.context.Coupons.FirstAsync(x => x.Id == model.Id);

            var type = ((CouponType)(int.Parse(model.CouponType)));

            couponFromDb.Name          = model.Name;
            couponFromDb.CouponType    = model.CouponType;
            couponFromDb.Picture       = model.Picture;
            couponFromDb.ECouponType   = type;
            couponFromDb.Discount      = model.Discount;
            couponFromDb.MinimumAmount = model.MinimumAmount;
            couponFromDb.IsActive      = model.IsActive;

            var result  = this.context.Update(couponFromDb);
            var success = await this.context.SaveChangesAsync();

            if (success > 0)
            {
                return(true);
            }

            return(false);
        }
        private AdminCouponListingServiceModel Initialize()
        {
            var couponVM = new AdminCouponListingServiceModel();

            return(couponVM);
        }