public async Task <IActionResult> EditPost(Brands brand)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Xaiş olunur düzgün doldurun.");
                ViewBag.Floor = _context.Floor;
                ViewBag.User  = _userManager.Users;
                ViewBag.Tag   = _context.Tags.Where(t => t.CategoryId == 3);

                return(View(brand));
            }

            Brands newBrand = await _context.Brands.FindAsync(brand.Id);

            if (newBrand == null)
            {
                return(View("Error"));
            }

            if (brand.Photo != null)
            {
                string computerPhoto = Path.Combine(_env.WebRootPath, "img", newBrand.PhotoURL);

                if (System.IO.File.Exists(computerPhoto))
                {
                    System.IO.File.Delete(computerPhoto);
                }

                string filename = await brand.Photo.SaveAsync(_env.WebRootPath);

                brand.PhotoURL    = filename;
                newBrand.PhotoURL = brand.PhotoURL;
            }

            if (brand.OnePagePhoto != null)
            {
                string computerPhoto = Path.Combine(_env.WebRootPath, "img", newBrand.OnePagePhotoURL);

                if (System.IO.File.Exists(computerPhoto))
                {
                    System.IO.File.Delete(computerPhoto);
                }

                string filename = await brand.OnePagePhoto.SaveAsync(_env.WebRootPath);

                brand.OnePagePhotoURL    = filename;
                newBrand.OnePagePhotoURL = brand.OnePagePhotoURL;
                newBrand.OnePageInfo     = brand.OnePageInfo;
            }

            IEnumerable <BrandTags> oldTags = _context.BrandTags.Where(p => p.BrandId == newBrand.Id);

            if (brand.TagsId != null)
            {
                _context.BrandTags.RemoveRange(oldTags);

                foreach (var c in brand.TagsId)
                {
                    BrandTags brandTags = new BrandTags()
                    {
                        BrandId = newBrand.Id,
                        TagsId  = c
                    };

                    await _context.BrandTags.AddAsync(brandTags);
                }
            }

            newBrand.Name          = brand.Name;
            newBrand.Phone         = brand.Phone;
            newBrand.InstagramLink = brand.InstagramLink;
            newBrand.FacebookLink  = brand.FacebookLink;
            newBrand.FloorId       = brand.FloorId;
            newBrand.Website       = brand.Website;
            newBrand.UserId        = brand.UserId;
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Example #2
0
        public async Task <IActionResult> Create(Brands brand)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }

            if (!User.IsInRole("Admin"))
            {
                return(RedirectToAction("Login", "Account"));
            }
            if (!ModelState.IsValid)
            {
                ViewBag.Active = "Home";
                ViewBag.Floor  = _context.Floor;
                ViewBag.User   = _userManager.Users;
                ViewBag.Tag    = _context.Tags.Where(t => t.CategoryId == 2);

                return(View(brand));
            }

            if (ModelState["Photo"].ValidationState == ModelValidationState.Invalid)
            {
                ViewBag.Active = "Home";
                ViewBag.Floor  = _context.Floor;
                ViewBag.User   = _userManager.Users;
                ViewBag.Tag    = _context.Tags.Where(t => t.CategoryId == 2);

                return(View(brand));
            }

            if (!brand.Photo.IsImage())
            {
                ViewBag.Active = "Home";
                ViewBag.Floor  = _context.Floor;
                ViewBag.User   = _userManager.Users;
                ViewBag.Tag    = _context.Tags.Where(t => t.CategoryId == 2);

                ModelState.AddModelError("Photo", "File type should be image");
                return(View(brand));
            }

            string filename = await brand.Photo.SaveAsync(_env.WebRootPath);

            brand.PhotoURL = filename;

            Brands newBrand = new Brands()
            {
                Name          = brand.Name,
                Phone         = brand.Phone,
                Website       = brand.Website,
                FacebookLink  = brand.FacebookLink,
                InstagramLink = brand.InstagramLink,
                CategoryId    = 2,
                FloorId       = brand.FloorId,
                PhotoURL      = filename,
                UserId        = brand.UserId,
                IsActive      = true
            };

            await _context.Brands.AddAsync(newBrand);

            foreach (var c in brand.TagsId)
            {
                BrandTags tags = new BrandTags()
                {
                    BrandId = newBrand.Id,
                    TagsId  = c
                };

                await _context.BrandTags.AddAsync(tags);
            }

            await _context.SaveChangesAsync();

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