Example #1
0
        public async Task <IActionResult> AdminUsersAsync(int?pageNumber)
        {
            //EF core

            var CurrentUserId = _userManager.GetUserId(User as ClaimsPrincipal);

            ArtGalleryContext          context   = new ArtGalleryContext();
            IQueryable <UserViewModel> UsersData = (IQueryable <UserViewModel>)(from U in context.Account
                                                                                where U.IsUser == true
                                                                                select new UserViewModel
            {
                UserName = U.UserName,
                IsActive = U.IsActive,
            });


            AdminUsersViewModel AdminUsersVM = new AdminUsersViewModel();

            AdminUsersVM.UserPaginatedList = await PaginatedList <UserViewModel> .CreateAsunc((IQueryable <UserViewModel>) UsersData, pageNumber ?? 1, 4);

            AdminUsersVM.FullName       = context.Account.Where(A => A.Id == CurrentUserId).Select(A => A.FirstName).FirstOrDefault();
            AdminUsersVM.ProfilePicture = context.ProfileImage.Where(PI => PI.AccountId == CurrentUserId).Select(PI => PI.ImageName).FirstOrDefault();

            return(View(AdminUsersVM));
        }
Example #2
0
        public IActionResult UpdateCompetition(int id)
        {
            ArtGalleryContext          context       = new ArtGalleryContext();
            UpdateCompetitionViewModel CompetitionVM = new UpdateCompetitionViewModel();
            var Competition = context.Competition.Where(A => A.CompetitionId == id).FirstOrDefault();
            var images      = context.Image.Where(I => I.PostId == id).ToList();

            //var tags = context.PostTag.Include(T => T.Tag).Where(PT => PT.PostId == id).ToList();

            //CompetitionVM.UpdatedPost.Category = Competition.Category.Name;
            CompetitionVM.UpdatedPost.CompetitionTitle   = Competition.Title;
            CompetitionVM.UpdatedPost.CompetitionCaption = Competition.Caption;
            //CompetitionVM.UpdatedPost.SubCategory = Competition.SubCategory.Name;
            CompetitionVM.UpdatedPost.CompetitionImage = (IFormFile)context.Image.Where(I => I.PostId == id).ToList();

            //CompetitionVM.PostTags = (IEnumerable<PostTag>)tags;


            var CurrentUserId    = _userManager.GetUserId(User as ClaimsPrincipal);
            var AccountFirstName = context.Account.Where(A => A.Id == CurrentUserId).Select(A => A.FirstName).FirstOrDefault();
            var ProfileImageName = context.ProfileImage.Where(PI => PI.AccountId == CurrentUserId).Select(PI => PI.ImageName).FirstOrDefault();

            CompetitionVM.FullName       = AccountFirstName;
            CompetitionVM.ProfilePicture = ProfileImageName;

            return(View(CompetitionVM));
        }
Example #3
0
        public IActionResult SearchPost(string Title)
        {
            ArtGalleryContext context = new ArtGalleryContext();
            Post post = context.Post.Where(A => A.Title.Contains(Title)).FirstOrDefault();

            return(View(post));
        }
 [HttpPost] // must be here for it to work
 public ActionResult CreateInventory(MainViewModel productDetails)
 {
     using (ArtGalleryContext _context = new ArtGalleryContext())
     {
         if (ModelState.IsValid)
         {
             Product product = new Product
             {
                 Name          = productDetails.Name,
                 Description   = productDetails.Description,
                 Inventory     = productDetails.Inventory,
                 Image         = productDetails.Image,
                 RetailPrice   = productDetails.RetailPrice,
                 PurchasePrice = productDetails.PurchasePrice,
                 Width         = productDetails.Width,
                 Height        = productDetails.Height,
                 Depth         = productDetails.Depth,
                 Weight        = productDetails.Weight
             };
             _context.ProductDbSet.Add(product); // "saves" in context to data in working memory
             _context.SaveChanges();             // saves to the database
             return(RedirectToAction("Index"));
         }
         return(View());
     }
 }
Example #5
0
        public IActionResult AdminPost(int id)
        {
            ArtGalleryContext  context = new ArtGalleryContext();
            AdminPostViewModel PostVM  = new AdminPostViewModel();
            var post     = context.Post.Where(A => A.PostId == id).FirstOrDefault();
            var comments = context.Comment.Where(C => C.PostId == id).ToList();
            var images   = context.Image.Where(I => I.PostId == id).ToList();
            var File     = context.Files.Where(I => I.PostId == id).ToList();
            var tags     = context.PostTag.Include(T => T.Tag).Where(PT => PT.PostId == id).ToList();
            var account  = context.Account.Where(A => A.Id == post.AccountId).FirstOrDefault();

            PostVM.Post      = post;
            PostVM.ImageName = images.Where(I => I.IsPrimary == true).Select(I => I.ImageName).FirstOrDefault();
            PostVM.Comments  = comments;
            PostVM.Images    = (IEnumerable <string>)images.Select(I => I.ImageName);
            PostVM.PostTags  = (IEnumerable <PostTag>)tags;
            PostVM.FileName  = (IEnumerable <string>)File.Select(F => F.FileName);
            PostVM.Account   = account;

            var CurrentUserId    = _userManager.GetUserId(User as ClaimsPrincipal);
            var AccountFirstName = context.Account.Where(A => A.Id == CurrentUserId).Select(A => A.FirstName).FirstOrDefault();
            var ProfileImageName = context.ProfileImage.Where(PI => PI.AccountId == CurrentUserId).Select(PI => PI.ImageName).FirstOrDefault();

            PostVM.FullName       = AccountFirstName;
            PostVM.ProfilePicture = ProfileImageName;

            return(View(PostVM));
        }
Example #6
0
        public IActionResult Category()
        {
            ////EF core
            ArtGalleryContext      context   = new ArtGalleryContext();
            IEnumerable <Category> Categorys = context.Category.ToList();

            ////EF core end
            return(View(new BaseViewModel()));
        }
Example #7
0
        public async Task <IActionResult> DeleteCompetition(int id)
        {
            ArtGalleryContext Context     = new ArtGalleryContext();
            Competition       competition = await Context.Competition.Where(C => C.CompetitionId == id).FirstOrDefaultAsync();

            Context.Competition.Remove(competition);
            await Context.SaveChangesAsync();

            return(RedirectToAction("AdminCompetition"));
        }
Example #8
0
        public IActionResult AddPost()
        {
            ArtGalleryContext context = new ArtGalleryContext();
            var CurrentUserId         = _userManager.GetUserId(User as ClaimsPrincipal);
            var AccountFirstName      = context.Account.Where(A => A.Id == CurrentUserId).Select(A => A.FirstName).FirstOrDefault();
            var ProfileImageName      = context.ProfileImage.Where(PI => PI.AccountId == CurrentUserId).Select(PI => PI.ImageName).FirstOrDefault();

            return(View(new AddPostViewModel {
                FullName = AccountFirstName, ProfilePicture = ProfileImageName
            }));
        }
Example #9
0
        public UserController(IWebHostEnvironment environment, UserManager <Account> userManager, SignInManager <Account> signInManager, ILogger <Account> logger, ArtGalleryContext Context)
        {
            _environment = environment;

            _userManager = userManager;

            _signInManager = signInManager;

            _logger = logger;

            _Context = Context;
        }
Example #10
0
        public IActionResult DeleteCategory(int id)
        {
            //EF core start
            ArtGalleryContext context  = new ArtGalleryContext();
            Category          category = context.Category.FirstOrDefault(a => a.CategoryId == id);

            context.Category.Remove(category);
            context.SaveChangesAsync();
            //EF core end

            return(new RedirectResult("/Admin/Category"));
        }
        // GET: Employee
        //public ActionResult Owner()
        //{
        //    ArtGalleryContext _employeeContext = new ArtGalleryContext();

        //    var employeeAdmin = (from emp in _employeeContext.Employee
        //                         join galy in _employeeContext.Gallery
        //                         on emp.GalleryId equals galy.GalleryId
        //                         join evnt in _employeeContext.Event
        //                         on galy.GalleryId equals evnt.GalleryId
        //                         orderby evnt.EventId
        //                         select new MainViewModel
        //                         {
        //                             EventName = evnt.ExhibitName,
        //                             EmployeeId = emp.EmployeeId
        //                         }).ToList();
        //    MainViewModel employeeView = new MainViewModel
        //    {
        //        Events = employeeAdmin
        //    };

        //    return View(employeeView);

        //}


/// <summary>
/// Of the two ways I have learned to pass lists to the view I much prefer the
/// the "direct passing" method because it is less code to write, and it sends the
/// select statement variable directly to the view, which only requires that the
/// model be of the IEnumerable type. The other method assigns the select statement var
/// to another variable inside a new instance of a separate view model. I do no like
/// this approach because it bloats the ViewModels folder and requires writing more
/// code into my methods.
/// </summary>


        public ActionResult ShowInventory()
        // the return statement below where qq is passed allows this method to communicate
        // with the cshtml file of the same name and "@model" this data
        {
            ArtGalleryContext _inventoryContext = new ArtGalleryContext();

            List <MainViewModel> currentInventory = (from p in _inventoryContext.ProductDbSet
                                                     orderby p.GalleryId
                                                     where p.Inventory > 0
                                                     select new MainViewModel
            {
                Name = p.Name,
                Inventory = p.Inventory,
                RetailPrice = p.RetailPrice,
                PurchasePrice = p.PurchasePrice,
                Image = p.Image
            }).ToList();

            return(View(currentInventory));



            // The 'q' => 'qq' approach is a way of taking non-enumerable data
            // and convert it to enumerable data. It is a clunkier process, though
            // effective. David Shuman has moved away from using this since first
            // showing it to me.


            //var q = (from p in _inventoryContext.ProductDbSet
            //        orderby p.GalleryId
            //        where p.Inventory > 0
            //        select new
            //        {
            //            Name = p.Name,
            //            Inventory = p.Inventory,
            //            RetailPrice = p.RetailPrice,
            //            PurchasePrice = p.PurchasePrice,
            //            Image = p.Image
            //        }).ToList();

            //return View(q);

            //var qq = q.AsEnumerable().Select(xx => new MainViewModel
            //{
            //    Name = xx.Name,
            //    Inventory = xx.Inventory,
            //    RetailPrice = xx.RetailPrice,
            //    PurchasePrice = xx.PurchasePrice,
            //    Image = xx.Image
            //}).ToList();

            //return View(qq);
        }
Example #12
0
        public IActionResult Index()
        {
            //EF core

            ArtGalleryContext   context = new ArtGalleryContext();
            AdminIndexViewModel Index   = new AdminIndexViewModel();

            Index.Post = (IEnumerable <Post>)context.Post.Include(P => P.Images).ToList();

            //EF core end
            return(View(Index));
        }
Example #13
0
        public IActionResult DeletePost(int id)
        {
            //EF core start
            ArtGalleryContext context = new ArtGalleryContext();
            Post post = context.Post.FirstOrDefault(o => o.PostId == id);

            context.Post.Remove(post);
            context.SaveChangesAsync();
            //EF core end

            return(new RedirectResult("/Admin/Index"));
        }
Example #14
0
        public IActionResult AddCompetition()
        {
            var CurrentUserId = _userManager.GetUserId(User as ClaimsPrincipal);
            ArtGalleryContext       context = new ArtGalleryContext();
            AddCompetitionViewModel comvm   = new AddCompetitionViewModel();

            comvm.CategoryList    = context.Category.ToList();
            comvm.SubCategoryList = context.SubCategory.ToList();
            comvm.FullName        = context.Account.Where(A => A.Id == CurrentUserId).Select(A => A.FirstName).FirstOrDefault();
            comvm.ProfilePicture  = context.ProfileImage.Where(PI => PI.AccountId == CurrentUserId).Select(PI => PI.ImageName).FirstOrDefault();
            return(View(comvm));
        }
Example #15
0
        public IActionResult UpdatePost(int id)
        {
            ArtGalleryContext   GetPost = new ArtGalleryContext();
            UpdatePostViewModel EVM     = new UpdatePostViewModel();
            var TargetPost  = GetPost.Post.Include(A => A.PostTags).ThenInclude(A => A.Tag).Include(A => A.Images).Where(A => A.PostId == id).FirstOrDefault();
            var SlideImages = GetPost.Image.Where(A => A.PostId == id).Where(A => A.IsPrimary == false).ToList();
            var file        = GetPost.Files.Where(A => A.PostId == id).ToList();

            EVM.Post = TargetPost;
            EVM.ForEditSlideImages = SlideImages;
            EVM.ForEditFile        = file;
            return(View(EVM));
        }
Example #16
0
        public IActionResult UpdaCategotery(Category category)
        {
            ArtGalleryContext context        = new ArtGalleryContext();
            Category          updatecategory = context.Category.FirstOrDefault(a => a.CategoryId == category.CategoryId);

            updatecategory.Name = category.Name;
            context.Category.Update(updatecategory);
            context.SaveChanges();

            ////EF core end

            return(RedirectToAction("UpdaCategotery"));
        }
Example #17
0
        public IActionResult AddComment(string Message, int PostId)
        {
            ArtGalleryContext Context = new ArtGalleryContext();
            Comment           comment = new Comment();

            comment.PostId       = PostId;
            comment.AccountId    = _userManager.GetUserId(User as ClaimsPrincipal);
            comment.Date         = DateTime.Now;
            comment.Text         = Message;
            comment.Confirmation = "true";

            Context.Comment.Add(comment);
            Context.SaveChanges();
            return(RedirectToAction("Post", new { id = PostId }));
        }
Example #18
0
        public IActionResult AddCategory(string CategoryName)
        {
            ////EF core

            ArtGalleryContext context  = new ArtGalleryContext();
            Category          category = new Category()
            {
                Name = CategoryName
            };

            context.Category.Add(category);
            context.SaveChanges();

            ////EF core end

            return(RedirectToAction("AddCategory"));
        }
Example #19
0
        public IActionResult Index()
        {
            //EF core

            ArtGalleryContext context = new ArtGalleryContext();
            IndexViewModel    Posts   = new IndexViewModel();
            var CurrentUserId         = _userManager.GetUserId(User as ClaimsPrincipal);
            var AccountFirstName      = context.Account.Where(A => A.Id == CurrentUserId).Select(A => A.FirstName).FirstOrDefault();
            var ProfileImageName      = context.ProfileImage.Where(PI => PI.AccountId == CurrentUserId).Select(PI => PI.ImageName).FirstOrDefault();


            Posts.Post           = (IEnumerable <Post>)context.Post.Include(P => P.Images).ToList();
            Posts.FullName       = AccountFirstName;
            Posts.ProfilePicture = ProfileImageName;

            //EF core end

            return(View(Posts));
        }
 [HttpPost] //must be here for it to work
 public ActionResult CreateEvent(MainViewModel eventDetails)
 {
     using (ArtGalleryContext _context = new ArtGalleryContext())
     {
         if (ModelState.IsValid)
         {
             Event galevent = new Event
             {
                 ExhibitName = eventDetails.ExhibitName,
                 Description = eventDetails.Description,
                 PercentSale = eventDetails.PercentSale,
                 DollarSale  = eventDetails.DollarSale,
                 StartDate   = eventDetails.StartDate,
                 EndDate     = eventDetails.EndDate
             };
             _context.Event.Add(galevent);
             _context.SaveChanges();
             return(RedirectToAction("Index"));
         }
     }
     return(View(eventDetails));
 }
Example #21
0
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = "Index")
        {
            ViewData["ReturnUrl"] = returnUrl;

            if (ModelState.IsValid)
            {
                ArtGalleryContext context = new ArtGalleryContext();
                var result = await _signInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, lockoutOnFailure : false);

                Account Account = context.Account.Where(A => A.UserName == model.Username).FirstOrDefault();
                if (result.Succeeded && Account.IsUser && Account.IsActive)
                {
                    var claims = new List <Claim>
                    {
                        new Claim("User", "User")
                    };

                    var             identity        = new ClaimsIdentity(claims, "MyCookieAuth");
                    ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity);

                    _logger.LogInformation("User logged in.");
                    return(RedirectToAction(returnUrl));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    //return RedirectToLocal(nameof(Lockout));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(View(model));
                }
            }

            return(View());
        }
Example #22
0
        public async Task <IActionResult> Profile()
        {
            //EF core start

            ArtGalleryContext context = new ArtGalleryContext();
            ProfileViewModel  PVM     = new ProfileViewModel();
            Account           Account = await context.Account.FirstOrDefaultAsync(A => A.Id == _userManager.GetUserId(User as ClaimsPrincipal));

            ProfileImage ProfileImage = await context.ProfileImage.FirstOrDefaultAsync(PI => PI.AccountId == Account.Id);

            PVM.Account      = Account;
            PVM.ProfileImage = ProfileImage;

            var CurrentUserId    = _userManager.GetUserId(User as ClaimsPrincipal);
            var AccountFirstName = await context.Account.Where(A => A.Id == CurrentUserId).Select(A => A.FirstName).FirstOrDefaultAsync();

            var ProfileImageName = await context.ProfileImage.Where(PI => PI.AccountId == CurrentUserId).Select(PI => PI.ImageName).FirstOrDefaultAsync();

            PVM.FullName       = AccountFirstName;
            PVM.ProfilePicture = ProfileImageName;
            //EF core end

            return(View(PVM));
        }
Example #23
0
        public async Task <IActionResult> AdminCompetition(int?pageNumber)
        {
            ArtGalleryContext                 context         = new ArtGalleryContext();
            AdminCompetitionViewModel         CVM             = new AdminCompetitionViewModel();
            IQueryable <CompetitionViewModel> CompetitionList = (from C in context.Competition
                                                                 select new CompetitionViewModel
            {
                CompetitionName = C.Title,
                //ImageName = C.CompetitionImage.ImageName,
                StartDate = C.StartDate,
                Category = C.Category.Name,
                SubCategory = C.SubCategory.Name,
            });

            CVM.CompetitionList = await PaginatedList <CompetitionViewModel> .CreateAsunc(CompetitionList, pageNumber ?? 1, 4);

            var CurrentUserId    = _userManager.GetUserId(User as ClaimsPrincipal);
            var AccountFirstName = context.Account.Where(A => A.Id == CurrentUserId).Select(A => A.FirstName).FirstOrDefault();
            var ProfileImageName = context.ProfileImage.Where(PI => PI.AccountId == CurrentUserId).Select(PI => PI.ImageName).FirstOrDefault();

            CVM.FullName       = AccountFirstName;
            CVM.ProfilePicture = ProfileImageName;
            return(View(CVM));
        }
Example #24
0
        public async Task <IActionResult> AddPost(AddPostViewModel AddPost)
        {
            if (!ModelState.IsValid)
            {
                return(View(AddPost));
            }
            else if (ModelState.IsValid)
            {
                ArtGalleryContext PostContext = new ArtGalleryContext();

                Post post = new Post();

                post.Title       = AddPost.Title;                                   //Replace Add title to target
                post.Caption     = AddPost.Caption;                                 //Replace Add Caption to target
                post.Date        = DateTime.Now;                                    //Replace Add Caption to target
                post.AccountId   = _userManager.GetUserId(User as ClaimsPrincipal); //Replace Add AccountId to target
                post.Category    = AddPost.Category;                                //Replace Add Category to target
                post.SubCategory = AddPost.SubCategory;                             //Replace Add title to target
                post.type        = AddPost.type;                                    //Replace Add type to target
                post.Price       = AddPost.Price;                                   //Replace Add Price to target
                post.OffPrice    = AddPost.OffPrice;                                //Replace OffPrice title to target

                PostContext.Post.Add(post);
                PostContext.SaveChanges();

                //  Add Post image started
                #region PostImage

                Guid guidImage = Guid.NewGuid();

                var uploadsRootFolder = Path.Combine(_environment.WebRootPath, "img/post");
                if (!Directory.Exists(uploadsRootFolder))
                {
                    Directory.CreateDirectory(uploadsRootFolder);
                }


                if (AddPost.PostImage == null || AddPost.PostImage.Length == 0)
                {
                    await Response.WriteAsync("Error");
                }

                var filePath = Path.Combine(uploadsRootFolder, guidImage + AddPost.PostImage.FileName);

                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await AddPost.PostImage.CopyToAsync(fileStream).ConfigureAwait(false);
                }

                ArtGalleryContext PosteImageContext = new ArtGalleryContext();
                Image             PostImage         = new Image();
                PostImage.Name         = AddPost.PostImage.FileName;
                PostImage.ImageName    = guidImage + AddPost.PostImage.FileName;
                PostImage.ImageAddress = filePath;
                PostImage.PostId       = post.PostId;
                PostImage.IsPrimary    = true;
                PosteImageContext.Image.Add(PostImage);
                PosteImageContext.SaveChanges();

                #endregion
                //  Add Post image ended

                //  Add Post started
                #region Slides

                foreach (var slide in AddPost.Images)
                {
                    var SlidesRootFolder = Path.Combine(_environment.WebRootPath, "img/detail");
                    if (!Directory.Exists(SlidesRootFolder))
                    {
                        Directory.CreateDirectory(SlidesRootFolder);
                    }

                    if (slide == null || slide.Length == 0)
                    {
                        await Response.WriteAsync("Error");
                    }

                    var slidepath = Path.Combine(SlidesRootFolder, slide.FileName);

                    using (var fileStream = new FileStream(slidepath, FileMode.Create))
                    {
                        await slide.CopyToAsync(fileStream).ConfigureAwait(false);
                    }

                    Guid guidImages = new Guid();
                    ArtGalleryContext SlideContext = new ArtGalleryContext();
                    Image             newSlide     = new Image();
                    newSlide.Name         = slide.FileName;
                    newSlide.ImageName    = guidImages + slide.FileName;
                    newSlide.ImageAddress = filePath;
                    newSlide.PostId       = post.PostId;
                    newSlide.IsPrimary    = false;
                    SlideContext.Image.Add(newSlide);
                    SlideContext.SaveChanges();
                }

                #endregion
                //  Add Post ended

                //  Add file started
                #region file

                foreach (var File in AddPost.Files)
                {
                    var SlidesRootFolder = Path.Combine(_environment.WebRootPath, "file/postfile");
                    if (!Directory.Exists(SlidesRootFolder))
                    {
                        Directory.CreateDirectory(SlidesRootFolder);
                    }

                    if (File == null || File.Length == 0)
                    {
                        await Response.WriteAsync("Error");
                    }

                    var slidepath = Path.Combine(SlidesRootFolder, File.FileName);

                    using (var fileStream = new FileStream(slidepath, FileMode.Create))
                    {
                        await File.CopyToAsync(fileStream).ConfigureAwait(false);
                    }

                    Guid guid = new Guid();
                    ArtGalleryContext FileContext = new ArtGalleryContext();
                    Models.File       newFIle     = new Models.File();
                    newFIle.Date      = DateTime.Now;
                    newFIle.Name      = File.FileName;
                    newFIle.FileName  = guid + File.FileName;
                    newFIle.Address   = filePath;
                    newFIle.PostId    = post.PostId;
                    newFIle.Accountid = post.AccountId;
                    FileContext.Files.Add(newFIle);
                    FileContext.SaveChanges();
                }

                #endregion
                //  Add file ended

                // Tags strated
                #region Tags
                ArtGalleryContext GetTag       = new ArtGalleryContext();
                ArtGalleryContext TagInsertion = new ArtGalleryContext();
                foreach (var newtag in AddPost.Tags)
                {
                    if (newtag != null)
                    {
                        var CheckTag     = GetTag.Tag.FirstOrDefault(T => T.Name == newtag);
                        Tag InsertionObj = new Tag();
                        if (CheckTag == null)
                        {
                            InsertionObj.Name = newtag;
                            TagInsertion.Tag.Add(InsertionObj);
                            TagInsertion.SaveChanges();
                            TagInsertion.PostTag.Add(new PostTag {
                                PostId = post.PostId, TagId = InsertionObj.TagId
                            });
                            TagInsertion.SaveChanges();
                        }
                        else
                        {
                            TagInsertion.PostTag.Add(new PostTag {
                                PostId = post.PostId, TagId = CheckTag.TagId
                            });
                            TagInsertion.SaveChanges();
                        }
                    }
                }

                #endregion
                // Tags ended

                return(RedirectToAction("AddPost"));
            }
            else
            {
                return(View());
            }
        }
Example #25
0
        public async Task <IActionResult> UpdatePost(UpdatePostViewModel updatePost)
        {
            ////EF core start

            ArtGalleryContext GetPost = new ArtGalleryContext();
            AddPostViewModel  AVM     = new AddPostViewModel();


            var TargetPost = GetPost.Post.FirstOrDefault(O => O.PostId == updatePost.Post.PostId);                                   //Find the psot
            var PostTag    = GetPost.PostTag.Include(a => a.Tag).FirstOrDefault(O => O.PostId == updatePost.Post.PostId);            //Find the first PostTag for the target Psot
            var PostTags   = GetPost.PostTag.Where(A => A.PostId == updatePost.Post.PostId).Include(A => A.Tag).ToList();            //Get list of PostTag for the target Psot
            var PostSlides = GetPost.Image.Where(I => I.PostId == updatePost.Post.PostId).Where(I => I.IsPrimary == false).ToList(); //Get list of Images for the target psot
            var PostFile   = GetPost.Files.Where(I => I.PostId == updatePost.Post.PostId).ToList();                                  //Get list of File for the target psot
            var PostImage  = GetPost.Image.Where(I => I.PostId == updatePost.Post.PostId).Where(I => I.IsPrimary == true).FirstOrDefault();

            //Post.Body = Post.Body;
            TargetPost.Title       = updatePost.Title;                                //Replace edited title to target
            TargetPost.Caption     = updatePost.Caption;                              //Replace edited Caption to target
            TargetPost.AccountId   = _userManager.GetUserId(User as ClaimsPrincipal); //Replace edited AccountId to target
            TargetPost.Category    = updatePost.Category;                             //Replace edited Category to target
            TargetPost.SubCategory = updatePost.SubCategory;                          //Replace edited title to target
            TargetPost.type        = updatePost.type;                                 //Replace edited type to target
            TargetPost.Price       = updatePost.Price;                                //Replace edited Price to target
            TargetPost.OffPrice    = updatePost.OffPrice;                             //Replace OffPrice title to target


            GetPost.Post.Update(TargetPost);
            GetPost.SaveChanges();

            #region EditPosteImage

            if (updatePost.Images != null)
            {
                var uploadsRootFolder = Path.Combine(_environment.WebRootPath, "img/post");

                string path = Path.Combine(uploadsRootFolder, PostImage.Name);
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }

                GetPost.Image.Remove(PostImage);
                GetPost.SaveChanges();

                if (!Directory.Exists(uploadsRootFolder))
                {
                    Directory.CreateDirectory(uploadsRootFolder);
                }


                if (updatePost.image == null || updatePost.image.Length == 0)
                {
                    await Response.WriteAsync("Error");
                }

                var filePath = Path.Combine(uploadsRootFolder, updatePost.image.FileName);

                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await updatePost.image.CopyToAsync(fileStream).ConfigureAwait(false);
                }

                Guid guidImage = new Guid();
                ArtGalleryContext PosteImageContext = new ArtGalleryContext();
                Image             ImagePost         = new Image();
                ImagePost.Name      = updatePost.image.FileName;
                ImagePost.ImageName = guidImage + updatePost.image.FileName;
                ImagePost.PostId    = updatePost.Post.PostId;
                ImagePost.IsPrimary = true;
                PosteImageContext.Image.Add(ImagePost);
                PosteImageContext.SaveChanges();
            }

            #endregion

            #region EditImages

            if (updatePost.Images != null)
            {
                foreach (var slides in PostSlides)    //Break the connection of old slides
                {
                    var uploadsRootFolder = Path.Combine(_environment.WebRootPath, "img/detail");

                    string path = Path.Combine(uploadsRootFolder, slides.Name);
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }

                    GetPost.Remove(slides);
                    await GetPost.SaveChangesAsync();
                }

                foreach (var editedSlides in updatePost.Images)   //Save new slide images
                {
                    var SlidesRootFolder = Path.Combine(_environment.WebRootPath, "img/detail");
                    if (!Directory.Exists(SlidesRootFolder))
                    {
                        Directory.CreateDirectory(SlidesRootFolder);
                    }

                    if (editedSlides == null || editedSlides.Length == 0)
                    {
                        await Response.WriteAsync("Wrong image!");
                    }

                    var slidepath = Path.Combine(SlidesRootFolder, editedSlides.FileName);

                    using (var fileStream = new FileStream(slidepath, FileMode.Create))
                    {
                        await editedSlides.CopyToAsync(fileStream).ConfigureAwait(false);
                    }

                    Guid  guidImages = new Guid();
                    Image newImage   = new Image();
                    newImage.Name      = editedSlides.FileName;
                    newImage.ImageName = guidImages + editedSlides.FileName;
                    newImage.IsPrimary = false;
                    newImage.PostId    = updatePost.Post.PostId;
                    GetPost.Image.Add(newImage);
                    await GetPost.SaveChangesAsync();
                }
            }

            #endregion

            #region EditFile

            if (updatePost.File != null)
            {
                foreach (var fils in PostFile)    //Break the connection of old file
                {
                    var uploadsRootFolder = Path.Combine(_environment.WebRootPath, "file/postfile");

                    string path = Path.Combine(uploadsRootFolder, fils.Name);
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }

                    GetPost.Remove(fils);
                    await GetPost.SaveChangesAsync();
                }

                foreach (var Edlitefile in updatePost.File)   //Save new file
                {
                    var SlidesRootFolder = Path.Combine(_environment.WebRootPath, "file/postfile");
                    if (!Directory.Exists(SlidesRootFolder))
                    {
                        Directory.CreateDirectory(SlidesRootFolder);
                    }

                    if (Edlitefile == null || Edlitefile.Length == 0)
                    {
                        await Response.WriteAsync("Wrong File!");
                    }

                    var slidepath = Path.Combine(SlidesRootFolder, Edlitefile.FileName);

                    using (var fileStream = new FileStream(slidepath, FileMode.Create))
                    {
                        await Edlitefile.CopyToAsync(fileStream).ConfigureAwait(false);
                    }

                    Guid        guidFile = new Guid();
                    Models.File newfile  = new Models.File();
                    newfile.Name     = Edlitefile.FileName;
                    newfile.FileName = guidFile + Edlitefile.FileName;
                    newfile.PostId   = updatePost.Post.PostId;
                    GetPost.Files.Add(newfile);
                    await GetPost.SaveChangesAsync();
                }
            }

            #endregion

            #region EditTags
            foreach (var AT in PostTags)
            {
                bool exist = false;
                foreach (var inputtag in updatePost.Tags)
                {
                    if (inputtag == AT.Tag.Name)
                    {
                        exist = true;
                    }
                }
                if (exist == false)
                {
                    GetPost.PostTag.Remove(AT);
                    await GetPost.SaveChangesAsync();
                }
            }
            #endregion


            ////EF core end
            return(new RedirectResult("post"));
        }
Example #26
0
        public async Task <IActionResult> AddCompetition(AddCompetitionViewModel AddCompetition)
        {
            if (!ModelState.IsValid)
            {
                return(View(AddCompetition));
            }
            else if (ModelState.IsValid)
            {
                ArtGalleryContext CompetitionContext = new ArtGalleryContext();

                Competition competition = new Competition();

                var catId    = CompetitionContext.Category.Where(C => C.Name == AddCompetition.Category).Select(C => C.CategoryId).FirstOrDefault();
                var subcatId = CompetitionContext.SubCategory.Where(C => C.Name == AddCompetition.SubCategory).Select(C => C.SubCategoryId).FirstOrDefault();

                competition.Title         = AddCompetition.CompetitionTitle;   //Replace Add title to target
                competition.Caption       = AddCompetition.CompetitionCaption; //Replace Add Caption to target
                competition.StartDate     = DateTime.Now;                      //Replace Add Caption to target
                competition.CategoryId    = catId;                             //Replace Add Category to target
                competition.SubCategoryId = subcatId;                          //Replace Add title to target

                CompetitionContext.Competition.Add(competition);
                CompetitionContext.SaveChanges();

                //  Add Competition image started
                #region PostImage

                Guid guidImage = Guid.NewGuid();

                var uploadsRootFolder = Path.Combine(_environment.WebRootPath, "img/Competition");
                if (!Directory.Exists(uploadsRootFolder))
                {
                    Directory.CreateDirectory(uploadsRootFolder);
                }


                if (AddCompetition.CompetitionImage == null || AddCompetition.CompetitionImage.Length == 0)
                {
                    await Response.WriteAsync("Error");
                }

                var filePath = Path.Combine(uploadsRootFolder, guidImage + AddCompetition.CompetitionImage.FileName);

                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await AddCompetition.CompetitionImage.CopyToAsync(fileStream).ConfigureAwait(false);
                }

                ArtGalleryContext CompetitionImageContext = new ArtGalleryContext();
                Image             ImageCompetition        = new Image();
                ImageCompetition.Name         = AddCompetition.CompetitionImage.FileName;
                ImageCompetition.ImageName    = guidImage + AddCompetition.CompetitionImage.FileName;
                ImageCompetition.ImageAddress = filePath;
                //ImageCompetition.CompetitionId = competition.CompetitionId;
                ImageCompetition.PostId    = 1;
                ImageCompetition.IsPrimary = true;
                CompetitionImageContext.Image.Add(ImageCompetition);
                CompetitionImageContext.SaveChanges();

                #endregion
                //  Add Competition image ended

                // Tags strated
                #region Tags
                //ArtGalleryContext GetTag = new ArtGalleryContext();
                //ArtGalleryContext TagInsertion = new ArtGalleryContext();
                //foreach (var newtag in AddCompetition.Tags)
                //{
                //    if (newtag != null)
                //    {
                //        var CheckTag = GetTag.Tag.FirstOrDefault(T => T.Name == newtag);
                //        Tag InsertionObj = new Tag();
                //        if (CheckTag == null)
                //        {
                //            InsertionObj.Name = newtag;
                //            TagInsertion.Tag.Add(InsertionObj);
                //            TagInsertion.SaveChanges();
                //            TagInsertion.PostTag.Add(new PostTag { PostId = post.PostId, TagId = InsertionObj.TagId });
                //            TagInsertion.SaveChanges();
                //        }
                //        else
                //        {
                //            TagInsertion.PostTag.Add(new PostTag { PostId = post.PostId, TagId = CheckTag.TagId });
                //            TagInsertion.SaveChanges();
                //        }
                //    }
                //}

                #endregion
                // Tags ended

                return(RedirectToAction("AddCompetition"));
            }
            else
            {
                return(View());
            }
        }
Example #27
0
        public async Task <IActionResult> UpdateCompetition(UpdateCompetitionViewModel updateCompetition)
        {
            ////EF core start

            ArtGalleryContext       GetCompetition = new ArtGalleryContext();
            AddCompetitionViewModel AVM            = new AddCompetitionViewModel();


            var TargetCompetition = GetCompetition.Competition.FirstOrDefault(O => O.CompetitionId == updateCompetition.UpdatedPost.CompetitionId);       //Find the psot

            //var PostTag = GetCompetition.PostTag.Include(a => a.Tag).FirstOrDefault(O => O.PostId == updatePost.Post.PostId);      //Find the first PostTag for the target Psot
            //var PostTags = GetCompetition.PostTag.Where(A => A.PostId == updatePost.Post.PostId).Include(A => A.Tag).ToList();    //Get list of PostTag for the target Psot

            //var CompetitionImage = GetCompetition.Image.Where(I => I.CompetitionId == updateCompetition.UpdatedPost.CompetitionId).Where(I => I.IsPrimary == true).FirstOrDefault();

            //Post.Body = Post.Body;
            TargetCompetition.Title   = updateCompetition.UpdatedPost.CompetitionTitle;    //Replace edited title to target
            TargetCompetition.Caption = updateCompetition.UpdatedPost.CompetitionCaption;  //Replace edited Caption to target
            //TargetCompetition.Category.Name = updateCompetition.UpdatedPost.Category;     //Replace edited Category to target
            //TargetCompetition.SubCategory.Name = updateCompetition.UpdatedPost.SubCategory;      //Replace edited title to target


            GetCompetition.Competition.Update(TargetCompetition);
            GetCompetition.SaveChanges();

            #region EditPosteImage

            if (updateCompetition.UpdatedPost.CompetitionImage != null)
            {
                var uploadsRootFolder = Path.Combine(_environment.WebRootPath, "img/Competition");

                string path = Path.Combine(uploadsRootFolder, /*CompetitionImage.Name*/ "");
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }

                //GetCompetition.Image.Remove(CompetitionImage);
                GetCompetition.SaveChanges();

                if (!Directory.Exists(uploadsRootFolder))
                {
                    Directory.CreateDirectory(uploadsRootFolder);
                }


                if (updateCompetition.UpdatedPost.CompetitionImage == null || updateCompetition.UpdatedPost.CompetitionImage.Length == 0)
                {
                    await Response.WriteAsync("Error");
                }

                var filePath = Path.Combine(uploadsRootFolder, updateCompetition.UpdatedPost.CompetitionImage.FileName);

                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await updateCompetition.UpdatedPost.CompetitionImage.CopyToAsync(fileStream).ConfigureAwait(false);
                }

                Guid guidImage = new Guid();
                ArtGalleryContext CompetitionImageContext = new ArtGalleryContext();
                Image             ImageCompetition        = new Image();
                ImageCompetition.Name      = updateCompetition.UpdatedPost.CompetitionImage.FileName;
                ImageCompetition.ImageName = guidImage + updateCompetition.UpdatedPost.CompetitionImage.FileName;
                ImageCompetition.PostId    = updateCompetition.UpdatedPost.CompetitionId;
                ImageCompetition.IsPrimary = true;
                CompetitionImageContext.Image.Add(ImageCompetition);
                CompetitionImageContext.SaveChanges();
            }

            #endregion

            #region EditTags
            //foreach (var AT in PostTags)
            //{
            //    bool exist = false;
            //    foreach (var inputtag in updatePost.Tags)
            //    {

            //        if (inputtag == AT.Tag.Name)
            //        {
            //            exist = true;
            //        }

            //    }
            //    if (exist == false)
            //    {
            //        GetPost.PostTag.Remove(AT);
            //        await GetPost.SaveChangesAsync();
            //    }
            //}
            #endregion

            ////EF core end
            return(new RedirectResult("Competition"));
        }
Example #28
0
        public async Task <IActionResult> UpdateProfile(ProfileViewModel updateprofile, IFormFile UpdatedImage)
        {
            ////EF core start

            ArtGalleryContext context        = new ArtGalleryContext();
            Account           CurrentAccount = await _userManager.FindByIdAsync(_userManager.GetUserId(User as ClaimsPrincipal));

            ProfileImage profileimage = new ProfileImage();

            CurrentAccount.UserName    = updateprofile.Account.UserName;
            CurrentAccount.PhoneNumber = updateprofile.Account.PhoneNumber;
            CurrentAccount.FirstName   = updateprofile.Account.FirstName;
            CurrentAccount.LastName    = updateprofile.Account.LastName;
            CurrentAccount.Email       = updateprofile.Account.Email;
            CurrentAccount.CardNumber  = updateprofile.Account.CardNumber;
            CurrentAccount.Address     = updateprofile.Account.Address;

            await _userManager.UpdateAsync(CurrentAccount);

            #region updateImages
            profileimage = context.ProfileImage.Where(I => I.AccountId == CurrentAccount.Id).FirstOrDefault();
            if (updateprofile.ClearInput == "Clear")
            {
                if (profileimage != null)
                {
                    var uploadsRootFolder = Path.Combine(_environment.WebRootPath, "img/profile");

                    string path = Path.Combine(uploadsRootFolder, profileimage.ImageName);
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }

                    context.ProfileImage.Remove(profileimage);
                    context.SaveChanges();
                }
            }
            else if (UpdatedImage != null)
            {
                if (profileimage != null)
                {
                    if (UpdatedImage.Name != profileimage.Name)
                    {
                        Guid guidImage         = Guid.NewGuid();
                        var  uploadsRootFolder = Path.Combine(_environment.WebRootPath, "img/profile");

                        string path = Path.Combine(uploadsRootFolder, profileimage.ImageName);
                        if (System.IO.File.Exists(path))
                        {
                            System.IO.File.Delete(path);
                        }

                        context.ProfileImage.Remove(profileimage);
                        context.SaveChanges();

                        if (!Directory.Exists(uploadsRootFolder))
                        {
                            Directory.CreateDirectory(uploadsRootFolder);
                        }

                        if (UpdatedImage == null || UpdatedImage.Length == 0)
                        {
                            await Response.WriteAsync("Error");
                        }

                        var filePath = Path.Combine(uploadsRootFolder, (guidImage + UpdatedImage.FileName));

                        using (var fileStream = new FileStream(filePath, FileMode.Create))
                        {
                            await UpdatedImage.CopyToAsync(fileStream).ConfigureAwait(false);
                        }

                        ArtGalleryContext updateImageContext = new ArtGalleryContext();
                        ProfileImage      imagepro           = new ProfileImage();
                        imagepro.Name         = UpdatedImage.FileName;
                        imagepro.ImageName    = guidImage + UpdatedImage.FileName;
                        imagepro.ImageAddress = filePath;
                        imagepro.AccountId    = _userManager.GetUserId(User as ClaimsPrincipal);

                        updateImageContext.ProfileImage.Add(imagepro);
                        updateImageContext.SaveChanges();
                    }
                }
                else if (profileimage == null)
                {
                    Guid guidImage         = Guid.NewGuid();
                    var  uploadsRootFolder = Path.Combine(_environment.WebRootPath, "img/profile");

                    if (!Directory.Exists(uploadsRootFolder))
                    {
                        Directory.CreateDirectory(uploadsRootFolder);
                    }

                    if (UpdatedImage == null || UpdatedImage.Length == 0)
                    {
                        await Response.WriteAsync("Error");
                    }

                    var filePath = Path.Combine(uploadsRootFolder, (guidImage + UpdatedImage.FileName));

                    using (var fileStream = new FileStream(filePath, FileMode.Create))
                    {
                        await UpdatedImage.CopyToAsync(fileStream).ConfigureAwait(false);
                    }


                    ArtGalleryContext updateImageContext = new ArtGalleryContext();
                    ProfileImage      imagepro           = new ProfileImage();
                    imagepro.Name         = UpdatedImage.FileName;
                    imagepro.ImageName    = guidImage + UpdatedImage.FileName;
                    imagepro.ImageAddress = filePath;
                    imagepro.AccountId    = _userManager.GetUserId(User as ClaimsPrincipal);

                    updateImageContext.ProfileImage.Add(imagepro);
                    updateImageContext.SaveChanges();
                }
            }

            #endregion
            ////EF core end
            return(RedirectToAction("Profile"));
        }