Пример #1
0
        public IActionResult CreateBanner(IFormCollection formData, IFormFile bannerImage, IFormFile mobileBannerImage)
        {
            var bannerName            = $"{formData["BannerName"]}";
            var expiryDate            = DateTime.Parse($"{formData["ExpiryDate"]}");
            var bannerImagePath       = string.Empty;
            var mobileBannerImagePath = string.Empty;

            var acceptedMimes = new List <string> {
                ".jpg", ".jpeg", ".png", ".gif"
            };

            try
            {
                var mobileBannerImageExtension = $".{mobileBannerImage.FileName.Split('.')[1]}";
                var bannerImageExtension       = $".{bannerImage.FileName.Split('.')[1]}";

                var mobileMimeMatch = acceptedMimes.FirstOrDefault(check => check.Contains(mobileBannerImageExtension));
                var bannerMimeMatch = acceptedMimes.FirstOrDefault(check => check.Contains(bannerImageExtension));

                if (mobileMimeMatch == null || bannerMimeMatch == null)
                {
                    throw new Exception("Apenas imagens no formato *.jpg, *.jpeg, *.gif ou *.png podem ser cadastradas!");
                }

                if (bannerImage != null)
                {
                    var fileName = $"{Guid.NewGuid() + bannerImageExtension}";

                    var newFileName = Path.Combine(_environment.WebRootPath, "img\\banners") + $@"\{fileName}";

                    bannerImagePath = $"http://www.pro-mobile.ml/img/banners/{fileName}";

                    using (FileStream fs = System.IO.File.Create(newFileName))
                    {
                        bannerImage.CopyTo(fs);
                        fs.Flush();
                    }
                }

                if (mobileBannerImage != null)
                {
                    var fileName = $"{Guid.NewGuid() + mobileBannerImageExtension}";

                    var newFileName = Path.Combine(_environment.WebRootPath, "img\\banners") + $@"\{fileName}";

                    mobileBannerImagePath = $"http://www.pro-mobile.ml/img/banners/{fileName}";

                    using (FileStream fs = System.IO.File.Create(newFileName))
                    {
                        mobileBannerImage.CopyTo(fs);
                        fs.Flush();
                    }
                }

                var newBanner = new Banner
                {
                    Status          = Status.Active,
                    Name            = bannerName,
                    RegionDivision  = (RegionDivision)Enum.Parse(typeof(RegionDivision), $"{User.Claims.Where(c => c.Type == "RegionDivision").Select(c => c.Value).FirstOrDefault()}"),
                    CreatedAt       = DateTime.Now,
                    ExpiryDate      = expiryDate,
                    ImagePath       = bannerImagePath,
                    MobileImagePath = mobileBannerImagePath
                };

                _db.Banners.Add(newBanner);
                _db.SaveChanges();
            }
            catch (Exception e)
            {
                return(RedirectToAction("Create", "Banners", new { errorMessage = $"Houve um erro ao cadastrar e salvar o novo banner! Erro: {e.Message}" }));
            }

            return(RedirectToAction("Create", "Banners", new { successMessage = $"Novo banner cadastrado com sucesso!" }));
        }
Пример #2
0
        public IActionResult CreateProduct(IFormCollection formData, IFormFile productImage)
        {
            var acceptedMimes = new List <string> {
                ".jpg", ".jpeg", ".png", ".gif"
            };

            using (var dbTransaction = _db.Database.BeginTransaction())
            {
                try
                {
                    var productName          = $"{formData["ProductName"]}";
                    var productHash          = $"{Guid.NewGuid()}";
                    var featuredText         = $"{formData["FeaturedText"]}";
                    var productCategory      = Convert.ToInt32(formData["ProductCategory"]);
                    var inCashPrice          = Convert.ToDecimal(Convert.ToString(formData["InCashPrice"]).Replace("R$ ", ""));
                    var parcelValue          = Convert.ToDecimal(Convert.ToString(formData["ParcelValue"]).Replace("R$ ", ""));
                    var forwardedPrice       = Convert.ToDecimal(Convert.ToString(formData["ForwardedPrice"]).Replace("R$ ", ""));
                    var yearlyTax            = Convert.ToDecimal(Convert.ToString(formData["YearlyTax"]).Replace("%", ""));
                    var monthlyTax           = Convert.ToDecimal(Convert.ToString(formData["MonthlyTax"]).Replace("%", ""));
                    var inputParcelsQuantity = Convert.ToInt32(formData["InputParcelsQuantity"]);
                    var parcelsQuantity      = Convert.ToInt32(formData["ParcelsQuantity"]);
                    var expiryDate           = DateTime.Parse($"{formData["ExpiryDate"]}");
                    var imagePath            = string.Empty;

                    var qrGenerator  = new QRCodeGenerator();
                    var qrCodeData   = qrGenerator.CreateQrCode(productHash, QRCodeGenerator.ECCLevel.Q);
                    var qrCode       = new Base64QRCode(qrCodeData);
                    var qrCodeString = qrCode.GetGraphic(10);

                    if (productImage != null)
                    {
                        var imageExtension = $".{productImage.FileName.Split('.')[1]}";

                        var bannerMimeMatch = acceptedMimes.FirstOrDefault(check => check.Contains(imageExtension));

                        if (bannerMimeMatch == null)
                        {
                            throw new Exception("Apenas imagens no formato *.jpg, *.jpeg, *.gif ou *.png podem ser cadastradas!");
                        }

                        var fileName = $"{Guid.NewGuid() + imageExtension}";

                        var newFileName = Path.Combine(_environment.WebRootPath, "img\\products") + $@"\{fileName}";

                        imagePath = $"http://www.pro-mobile.ml/img/products/{fileName}";

                        using (var fs = System.IO.File.Create(newFileName))
                        {
                            productImage.CopyTo(fs);
                            fs.Flush();
                        }
                    }

                    var newProduct = new PromotionalProduct
                    {
                        ProductCategory      = _db.ProductCategories.FirstOrDefault(c => c.Id == productCategory),
                        ProductHash          = productHash,
                        Name                 = productName,
                        FeaturedOptionalText = featuredText,
                        CreatedAt            = DateTime.Now,
                        ExpiryDate           = expiryDate,
                        ForwardedPrice       = forwardedPrice,
                        InCashPrice          = inCashPrice,
                        ParcelValue          = parcelValue,
                        ImagePath            = imagePath,
                        InputParcelsQuantity = inputParcelsQuantity,
                        ParcelsQuantity      = parcelsQuantity,
                        MonthlyTax           = monthlyTax,
                        YearlyTax            = yearlyTax,
                        Status               = Status.Active,
                        RegionDivision       = (RegionDivision)Enum.Parse(typeof(RegionDivision), $"{User.Claims.Where(c => c.Type == "RegionDivision").Select(c => c.Value).FirstOrDefault()}"),
                        QrCodeString         = qrCodeString
                    };

                    _db.PromotionalProducts.Add(newProduct);
                    _db.SaveChanges();
                    dbTransaction.Commit();
                }
                catch (Exception e)
                {
                    dbTransaction.Rollback();
                    return(RedirectToAction("Create", "Products", new { errorMessage = $"Houve um erro ao cadastrar e salvar o novo produto! Erro: {e.Message}" }));
                }
            }

            return(RedirectToAction("Create", "Products", new { successMessage = $"Novo produto cadastrado com sucesso!" }));
        }