public async Task <IActionResult> Admin()
        {
            AdminPostModel postModel = new AdminPostModel();

            postModel.Posts = await _offerDbContext.Posts.ToListAsync();

            foreach (Post item in postModel.Posts)
            {
                if (item.ExpirationDate.ToString("dd/MM/yyyy") == DateTime.Now.ToString("dd/MM/yyyy"))
                {
                    _offerDbContext.Posts.Remove(item);
                    await _offerDbContext.SaveChangesAsync();
                }
            }
            postModel.Companies = await _offerDbContext.Companies.ToListAsync();

            int specCount = SpecCount();

            if (specCount > 5)
            {
                ViewBag.Warning = "SpecDiscount Overflow";
            }

            if (_signInManager.IsSignedIn(User) && User.IsInRole("Admin"))
            {
                return(View(postModel));
            }
            else
            {
                return(RedirectToAction(nameof(AccountController.LoginAdmin), "Account"));
            }
        }
        public async Task <IActionResult> DeletePost(int Id)
        {
            AdminPostModel postModel = new AdminPostModel();

            postModel.Post = await _offerDbContext.Posts.SingleOrDefaultAsync(x => x.Id == Id);

            return(View(postModel));
        }
        public async Task <IActionResult> DeletePost(Post post, int Id)
        {
            AdminPostModel postModel = new AdminPostModel();

            postModel.Post = await _offerDbContext.Posts.SingleOrDefaultAsync(x => x.Id == Id);

            post = postModel.Post;

            _offerDbContext.Posts.Remove(post);
            await _offerDbContext.SaveChangesAsync();

            return(RedirectToAction("Admin", "Admin"));
        }
        public async Task <IActionResult> Postadd(Post post, IFormFile file, DateTime startDate, DateTime endDate, bool gifted, bool specDiscount)
        {
            if (ModelState.IsValid)
            {
                if (file == null || file.Length == 0)
                {
                    ModelState.AddModelError("", "No file selected");
                    return(View());
                }

                string mypath = Path.Combine(_env.WebRootPath, "images", Path.GetFileName(file.FileName));

                using (var stream = new FileStream(mypath, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }

                AdminPostModel postModel = new AdminPostModel();
                postModel.Companies = await _offerDbContext.Companies.ToListAsync();

                postModel.CompanyList = await _offerDbContext.Companies.Select(x => new SelectListItem()
                {
                    Value = x.Id.ToString(),
                    Text  = x.Name
                }).ToListAsync();

                if (gifted == true)
                {
                    post.Gifted = true;
                }

                if (specDiscount == true)
                {
                    post.SpecDiscount = true;
                }
                post.CreatedDate    = startDate;
                post.ExpirationDate = endDate;
                post.Image          = file.FileName;
                _offerDbContext.Posts.Add(post);
                await _offerDbContext.SaveChangesAsync();

                return(RedirectToAction("Admin", "Admin"));
            }
            else
            {
                ModelState.AddModelError("", "Couldn't create");
                return(View());
            }
        }
        public int SpecCount()
        {
            AdminPostModel postModel = new AdminPostModel();

            postModel.Posts = _offerDbContext.Posts.ToList();
            int count = 0;

            foreach (Post item in postModel.Posts)
            {
                if (item.SpecDiscount == true)
                {
                    count++;
                }
            }
            return(count);
        }
        public async Task <IActionResult> EditPost(int Id)
        {
            AdminPostModel postModel = new AdminPostModel();

            postModel.Post = await _offerDbContext.Posts.SingleOrDefaultAsync(x => x.Id == Id);

            postModel.Companies = await _offerDbContext.Companies.ToListAsync();

            postModel.CompanyList = await _offerDbContext.Companies.Select(x => new SelectListItem()
            {
                Value = x.Id.ToString(),
                Text  = x.Name
            }).ToListAsync();

            postModel.SubcategoryList = await _offerDbContext.Subcategories.Select(y => new SelectListItem()
            {
                Value = y.Id.ToString(),
                Text  = y.Name
            }).ToListAsync();

            return(View(postModel));
        }
        public async Task <IActionResult> Postadd()
        {
            AdminPostModel postModel = new AdminPostModel();

            postModel.Companies = await _offerDbContext.Companies.ToListAsync();

            postModel.Categories = await _offerDbContext.Categories.ToListAsync();

            postModel.CompanyList = await _offerDbContext.Companies.Select(x => new SelectListItem()
            {
                Value = x.Id.ToString(),
                Text  = x.Name
            }).ToListAsync();

            //ViewBag.Categories = postModel.Categories;

            postModel.CategoryList = await _offerDbContext.Categories.Select(y => new SelectListItem()
            {
                Value = y.Id.ToString(),
                Text  = y.Name
            }).ToListAsync();

            return(View(postModel));
        }
        public async Task <IActionResult> EditPost(Post post, int Id, IFormFile file, DateTime startDate, DateTime endDate, bool gifted, bool specDiscount)
        {
            if (Id != post.Id)
            {
                ModelState.AddModelError("", "Invalid Id");
            }

            if (ModelState.IsValid)
            {
                AdminPostModel postModel = new AdminPostModel();
                postModel.Post = await _offerDbContext.Posts.SingleOrDefaultAsync(x => x.Id == Id);

                if (postModel.Post != null)
                {
                    if (file == null || file.Length == 0)
                    {
                        ModelState.AddModelError("", "No file selected");
                        return(View());
                    }

                    string mypath = Path.Combine(_env.WebRootPath, "images", Path.GetFileName(file.FileName));

                    using (var stream = new FileStream(mypath, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }

                    if (gifted == true)
                    {
                        post.Gifted = true;
                    }

                    if (specDiscount == true)
                    {
                        post.SpecDiscount = true;
                    }

                    post.Image                    = file.FileName;
                    post.CreatedDate              = startDate;
                    post.ExpirationDate           = endDate;
                    postModel.Post.Title          = post.Title;
                    postModel.Post.Description    = post.Description;
                    postModel.Post.CreatedDate    = post.CreatedDate;
                    postModel.Post.ExpirationDate = post.ExpirationDate;
                    postModel.Post.Image          = file.FileName;
                    postModel.Post.URL            = post.URL;
                    postModel.Post.CompanyId      = post.CompanyId;
                    postModel.Post.SubcategoryId  = post.SubcategoryId;
                    postModel.Post.Gifted         = post.Gifted;
                    postModel.Post.SpecDiscount   = post.SpecDiscount;

                    await _offerDbContext.SaveChangesAsync();
                }
                return(RedirectToAction("Admin", "Admin"));
            }
            else
            {
                ModelState.AddModelError("", "Couldn't edit");
                return(View());
            }
        }