Пример #1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Value")] SingleUseCoupon singleUseCoupon)
        {
            if (id != singleUseCoupon.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(singleUseCoupon);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SingleUseCouponExists(singleUseCoupon.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(singleUseCoupon));
        }
Пример #2
0
        public async Task <IActionResult> Create([Bind("LastSpent")] SingleUseCoupon singleUseCoupon, IFormFile file)
        {
            if (ModelState.IsValid)
            {
                var    userId          = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
                var    shopOwner       = _context.ShopOwners.Where(s => s.ApplicationUserId == userId).FirstOrDefault();
                var    filename        = shopOwner.Id + Path.GetFileName(file.FileName);
                string contentrootpath = _host.ContentRootPath + "/App_Data/uploads";
                var    path            = Path.Combine(contentrootpath, filename);
                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    await file.CopyToAsync(fileStream);
                }

                var neighborIds           = _context.Neighbors.Select(n => n.Id).ToList();
                var singleUseCouponValues = _context.SingleUseCoupons.Select(s => s.Value).ToList();

                using (RasterCodecs codecs = new RasterCodecs())
                {
                    RasterImage newImage = codecs.Load(filename, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1);

                    //Dispose of the image
                    if (newImage != null)
                    {
                        theImage.Dispose();
                    }

                    theImage      = newImage;
                    imageFileName = filename;
                }

                BarcodeData[] dataArray = barcodeEngineInstance.Reader.ReadBarcodes(theImage, LeadRect.Empty, 0, null);

                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("{0} barcode(s) found", dataArray.Length);
                sb.AppendLine();

                for (int i = 0; i < dataArray.Length; i++)
                {
                    BarcodeData data = dataArray[i];

                    sb.AppendFormat("Symbology: {0}, Location: {1}, Data: {2}", data.Symbology.ToString(), data.Bounds.ToString(), data.Value);
                    sb.AppendLine();

                    if (neighborIds.Contains(sb[2]))
                    {
                        var neighborId   = sb[2];
                        var subscription = _context.Subscriptions.Where(s => s.NeighborId == neighborId).FirstOrDefault();
                        subscription.UsageCount++;
                        subscription.TotalSpent += singleUseCoupon.LastSpent;
                    }
                    if (singleUseCouponValues.Contains(sb[2]))
                    {
                        throw new Exception();
                    }
                    else
                    {
                        _context.Add(singleUseCoupon);
                        await _context.SaveChangesAsync();
                    };
                }
                //if (neighborIds.Contains(singleUseCoupon.Value))
                //{
                //    var neighborId = singleUseCoupon.Value;
                //    var subscription = _context.Subscriptions.Where(s => s.NeighborId == neighborId).FirstOrDefault();
                //    subscription.UsageCount++;
                //    subscription.TotalSpent += singleUseCoupon.LastSpent;
                //}
                //else
                //{
                //    throw new Exception();
                //}
                //var singleUseCouponValues = _context.SingleUseCoupons.Select(s => s.Value).ToList();
                //if (singleUseCouponValues.Contains(singleUseCoupon.Value))
                //{
                //    throw new Exception();
                //}
                //else
                //{
                //    _context.Add(singleUseCoupon);
                //    await _context.SaveChangesAsync();
                //}
                //return RedirectToAction(nameof(Index));
            }
            return(View());
        }