public async Task <IActionResult> Create(HomePageVM homePageVM)
        {
            if (!ModelState.IsValid)
            {
                return(View(homePageVM));
            }
            List <IFormFile> files = new List <IFormFile>();

            foreach (var item in homePageVM.ProductPhotos)
            {
                if (item == null)
                {
                    ModelState.AddModelError("ProductPhoto", "Image should be selected");
                    return(View(homePageVM));
                }

                if (!item.IsImage())
                {
                    ModelState.AddModelError("ProductPhoto", "Image type is not valid");
                    return(View(homePageVM));
                }

                if (!item.IsSmaller(1))
                {
                    ModelState.AddModelError("ProductPhoto", "Image size can be maximum 1 mb");
                    return(View(homePageVM));
                }
                files.Add(item);
            }
            await _context.Products.AddAsync(new Product
            {
                Name            = homePageVM.Product.Name,
                Price           = homePageVM.Product.Price,
                HasDiscount     = homePageVM.Product.HasDiscount,
                DiscountedPrice = homePageVM.Product.DiscountedPrice,
                Color           = homePageVM.Product.Color,
                Availability    = homePageVM.Product.Availability,
                Status          = true,
                InStock         = homePageVM.Product.InStock,
                BrandId         = homePageVM.Product.BrandId
            });

            await _context.SaveChangesAsync();

            var ProId = _context.Products.LastOrDefault().Id;

            foreach (var item in files)
            {
                var Image = await item.SaveFileAsync(_env.WebRootPath, "images");

                await _context.Images.AddAsync(new Image
                {
                    ProductImage = Image,
                    ProductId    = ProId
                });

                await _context.SaveChangesAsync();
            }
            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Create(Brand brand)
        {
            if (!ModelState.IsValid)
            {
                return(View(brand));
            }
            brand.Status = true;
            await _context.Brands.AddAsync(brand);

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 3
0
        public async Task <IActionResult> Edit(SmartFuture smartFuture)
        {
            if (!ModelState.IsValid)
            {
                return(View(smartFuture));
            }
            var smartfutureDb = await _context.SmartFutures.FindAsync(smartFuture.Id);

            if (smartFuture.Photo != null)
            {
                try
                {
                    var newPhoto = await smartFuture.Photo.SaveFileAsync(_env.WebRootPath, "images");

                    IFormFileExstensions.Delete(_env.WebRootPath, "images", smartfutureDb.Image);

                    smartfutureDb.Image = newPhoto;
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Unexpected error happened while saving image. Please, try again.");
                    return(View(smartFuture));
                }
            }

            smartfutureDb.Title    = smartFuture.Title;
            smartfutureDb.Subtitle = smartFuture.Subtitle;
            smartfutureDb.Button   = smartFuture.Button;


            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Edit(GloballyConnected globallyConnected)
        {
            if (!ModelState.IsValid)
            {
                return(View(globallyConnected));
            }
            var globallyConnectedDb = await _context.GloballyConnecteds.FindAsync(globallyConnected.Id);

            if (globallyConnected.Photo != null)
            {
                try
                {
                    var newPhoto = await globallyConnected.Photo.SaveFileAsync(_env.WebRootPath, "images");

                    IFormFileExstensions.Delete(_env.WebRootPath, "images", globallyConnectedDb.Image);

                    globallyConnectedDb.Image = newPhoto;
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Unexpected error happened while saving image. Please, try again.");
                    return(View(globallyConnected));
                }
            }

            globallyConnectedDb.Title     = globallyConnected.Title;
            globallyConnectedDb.Subtitle  = globallyConnected.Subtitle;
            globallyConnectedDb.Paragraph = globallyConnected.Paragraph;
            globallyConnectedDb.Button    = globallyConnected.Button;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Edit(BrandNewApp brandNewApp)
        {
            if (!ModelState.IsValid)
            {
                return(View(brandNewApp));
            }
            var brandNewAppDb = await _context.BrandNewApps.FindAsync(brandNewApp.Id);

            if (brandNewApp.Photo != null)
            {
                try
                {
                    var newPhoto = await brandNewApp.Photo.SaveFileAsync(_env.WebRootPath, "images");

                    IFormFileExstensions.Delete(_env.WebRootPath, "images", brandNewAppDb.Image);

                    brandNewAppDb.Image = newPhoto;
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Unexpected error happened while saving image. Please, try again.");
                    return(View(brandNewApp));
                }
            }

            if (brandNewApp.IconPhoto != null)
            {
                try
                {
                    var icoPhoto = await brandNewApp.IconPhoto.SaveFileAsync(_env.WebRootPath, "images");

                    IFormFileExstensions.Delete(_env.WebRootPath, "images", brandNewAppDb.IconImage);

                    brandNewAppDb.IconImage = icoPhoto;
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Unexpected error happened while saving image. Please, try again.");
                    return(View(brandNewApp));
                }
            }

            brandNewAppDb.SubtitleTop    = brandNewApp.SubtitleTop;
            brandNewAppDb.Title          = brandNewApp.Title;
            brandNewAppDb.SubtitleBottom = brandNewApp.SubtitleBottom;
            brandNewAppDb.Paragraph      = brandNewApp.Paragraph;
            brandNewAppDb.Button         = brandNewApp.Button;



            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Edit(Faq faq)
        {
            if (!ModelState.IsValid)
            {
                return(View(faq));
            }
            var faqDb = await _context.Faqs.FindAsync(faq.Id);

            faqDb.Question = faq.Question;
            faqDb.Answer   = faq.Answer;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Edit(SomeFeature someFeature)
        {
            if (!ModelState.IsValid)
            {
                return(View(someFeature));
            }
            var someFeatureDb = await _context.SomeFeatures.FindAsync(someFeature.Id);

            someFeatureDb.Title     = someFeature.Title;
            someFeatureDb.Paragraph = someFeature.Paragraph;
            someFeatureDb.Icon      = someFeature.Icon;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Edit(Footer footer)
        {
            if (!ModelState.IsValid)
            {
                return(View(footer));
            }
            var footerDb = await _context.Footers.FindAsync(footer.Id);

            footerDb.AddressFirstLine  = footer.AddressFirstLine;
            footerDb.AddressSecondLine = footer.AddressSecondLine;
            footerDb.PhoneNumber       = footer.PhoneNumber;
            footerDb.WorkingHours      = footer.WorkingHours;
            footerDb.Email             = footer.Email;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }