// Projects are not intended to be deleted by a user, so this functionality has been replaced with inactivation.

        public ActionResult InactivateProject(ProjectViewModel projectViewModel)
        {
            using (var assetRepoContext = new AssetRepoContext())
            {
                var project = assetRepoContext.Projects.SingleOrDefault(p => p.ProjectId == projectViewModel.ProjectId);

                if (project != null)
                {
                    // Represents the protected 'SYSTEM' profile.
                    project.LastUpdaterId      = 1;
                    project.LastUpdateDateTime = DateTime.Now;
                    project.IsActive           = false;
                    var assets = assetRepoContext.Assets.Where(a => a.ProjectId == project.ProjectId).ToList();
                    foreach (Asset asset in assets)
                    {
                        if (asset != null)
                        {
                            asset.ProjectId          = 1;
                            asset.LastUpdaterId      = 1;
                            asset.LastUpdateDateTime = DateTime.Now;
                        }
                    }
                    assetRepoContext.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(new HttpNotFoundResult());
        }
        public ActionResult EditContributor(ContributorViewModel contributorViewModel)
        {
            if (!ModelState.IsValid)
            {
                using (var assetRepoContext = new AssetRepoContext())
                {
                    return(View("AddEditContributor", contributorViewModel));
                }
            }

            using (var assetRepoContext = new AssetRepoContext())
            {
                var contributor = assetRepoContext.Contributors.SingleOrDefault(c => c.ContributorId == contributorViewModel.ContributorId);

                if (contributor != null)
                {
                    contributor.Name = contributorViewModel.Name;
                    assetRepoContext.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(new HttpNotFoundResult());
        }
        public ActionResult AddProject(ProjectViewModel projectViewModel)
        {
            if (!ModelState.IsValid)
            {
                using (var assetRepoContext = new AssetRepoContext())
                {
                    ViewBag.ProjectCategories = assetRepoContext.ProjectCategories
                                                .Select(pc => new SelectListItem
                    {
                        Value = pc.ProjectCategoryId.ToString(),
                        Text  = pc.Name
                    })
                                                // Miscellaneous and General Use project categories are excluded since they should have exactly one corresponding project instance.
                                                .Where(pc => pc.Value != "1" && pc.Value != "2")
                                                .ToList();

                    ViewBag.Contributors = assetRepoContext.Contributors
                                           .Where(c => c.ContributorId != 1)
                                           .Select(c => new SelectListItem
                    {
                        Value = c.ContributorId.ToString(),
                        Text  = c.Name
                    })
                                           .ToList();

                    return(View("AddEditProject", projectViewModel));
                }
            }

            using (var assetRepoContext = new AssetRepoContext())
            {
                var project = new Project
                {
                    Title             = projectViewModel.Title,
                    ProjectCategoryId = projectViewModel.Category.ProjectCategoryId.Value,
                    CreatorId         = projectViewModel.Creator.ContributorId.Value,
                    // Automatically set to current system datetime
                    CreationDateTime = DateTime.Now,
                    // Set to Creator ID value since asset is new
                    LastUpdaterId = projectViewModel.Creator.ContributorId.Value,
                    // Automatically set to current system datetime
                    LastUpdateDateTime = DateTime.Now,
                    Description        = projectViewModel.Description,
                    IsActive           = true
                };

                assetRepoContext.Projects.Add(project);
                assetRepoContext.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
        public ActionResult EditProject(ProjectViewModel projectViewModel)
        {
            if (!ModelState.IsValid)
            {
                using (var assetRepoContext = new AssetRepoContext())
                {
                    ViewBag.ProjectCategories = assetRepoContext.ProjectCategories
                                                .Select(pc => new SelectListItem
                    {
                        Value = pc.ProjectCategoryId.ToString(),
                        Text  = pc.Name
                    })
                                                // Miscellaneous and General Use project categories are excluded since they should have exactly one corresponding project instance.
                                                .Where(pc => pc.Value != "1" && pc.Value != "2")
                                                .ToList();

                    ViewBag.Contributors = assetRepoContext.Contributors
                                           .Where(c => c.ContributorId != 1)
                                           .Select(c => new SelectListItem
                    {
                        Value = c.ContributorId.ToString(),
                        Text  = c.Name
                    })
                                           .ToList();

                    return(View("AddEditProject", projectViewModel));
                }
            }

            using (var assetRepoContext = new AssetRepoContext())
            {
                var project = assetRepoContext.Projects.SingleOrDefault(p => p.ProjectId == projectViewModel.ProjectId);

                if (project != null)
                {
                    project.Title             = projectViewModel.Title;
                    project.ProjectCategoryId = projectViewModel.Category.ProjectCategoryId.Value;
                    // CreatorId and CreationDateTime omitted so as to be set only during add
                    project.LastUpdaterId = projectViewModel.LastUpdater.ContributorId.Value;
                    // Automatically set to current system datetime
                    project.LastUpdateDateTime = DateTime.Now;
                    project.Description        = projectViewModel.Description;
                    project.IsActive           = projectViewModel.IsActive;
                    assetRepoContext.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(new HttpNotFoundResult());
        }
        public ActionResult DeleteAsset(AssetViewModel assetViewModel)
        {
            using (var assetRepoContext = new AssetRepoContext())
            {
                var projectRouteId = assetViewModel.ProjectRouteId;
                var asset          = assetRepoContext.Assets.SingleOrDefault(a => a.AssetId == assetViewModel.AssetId);

                if (asset != null)
                {
                    assetRepoContext.Assets.Remove(asset);
                    assetRepoContext.SaveChanges();

                    return(RedirectToAction("Index", new { projectId = projectRouteId }));
                }
            }

            return(new HttpNotFoundResult());
        }
        public ActionResult ReactivateProject(ProjectViewModel projectViewModel)
        {
            using (var assetRepoContext = new AssetRepoContext())
            {
                var project = assetRepoContext.Projects.SingleOrDefault(p => p.ProjectId == projectViewModel.ProjectId);

                if (project != null)
                {
                    // Represents the protected 'SYSTEM' profile.
                    project.LastUpdaterId      = 1;
                    project.LastUpdateDateTime = DateTime.Now;
                    project.IsActive           = true;
                    assetRepoContext.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }

            return(new HttpNotFoundResult());
        }
        public ActionResult AddContributor(ContributorViewModel contributorViewModel)
        {
            if (!ModelState.IsValid)
            {
                using (var assetRepoContext = new AssetRepoContext())
                {
                    return(View("AddEditContributor", contributorViewModel));
                }
            }

            using (var assetRepoContext = new AssetRepoContext())
            {
                var contributor = new Contributor
                {
                    Name = contributorViewModel.Name
                };

                assetRepoContext.Contributors.Add(contributor);
                assetRepoContext.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
        public ActionResult EditAsset(AssetViewModel assetViewModel)
        {
            if (!ModelState.IsValid)
            {
                using (var assetRepoContext = new AssetRepoContext())
                {
                    ViewBag.Projects = assetRepoContext.Projects
                                       .Where(p => p.IsActive == true)
                                       .Select(p => new SelectListItem
                    {
                        Value = p.ProjectId.ToString(),
                        Text  = p.Title
                    })
                                       .ToList();

                    ViewBag.AssetTypeSubtypePairings = assetRepoContext.AssetTypeSubtypePairings.Select(atsp => new SelectListItem
                    {
                        Value = atsp.AssetTypeSubtypePairingId.ToString(),
                        Text  = atsp.AssetType.Name + ": " + atsp.AssetSubtype.Name
                    }).ToList();

                    ViewBag.Contributors = assetRepoContext.Contributors
                                           .Where(c => c.ContributorId != 1)
                                           .Select(c => new SelectListItem
                    {
                        Value = c.ContributorId.ToString(),
                        Text  = c.Name
                    })
                                           .ToList();

                    return(View("AddEditAsset", assetViewModel));
                }
            }

            using (var assetRepoContext = new AssetRepoContext())
            {
                var asset = assetRepoContext.Assets.SingleOrDefault(a => a.AssetId == assetViewModel.AssetId);

                if (asset != null)
                {
                    asset.Title     = assetViewModel.Title;
                    asset.ProjectId = assetViewModel.Project.ProjectId.Value;
                    asset.AssetTypeSubtypePairingId = assetViewModel.TypeSubtypePairing.AssetTypeSubtypePairingId.Value;
                    // CreatorId and CreationDateTime omitted so as to be set only during add
                    asset.LastUpdaterId = assetViewModel.LastUpdater.ContributorId.Value;
                    // Automatically set to current system datetime
                    asset.LastUpdateDateTime = DateTime.Now;
                    asset.Comment            = assetViewModel.Comment;
                    asset.FilePlaceholder    = assetViewModel.FilePlaceholder;

                    // Updates last updated properties for respective project
                    var project = assetRepoContext.Projects.SingleOrDefault(p => p.ProjectId == assetViewModel.Project.ProjectId);
                    if (project != null)
                    {
                        project.LastUpdaterId      = assetViewModel.LastUpdater.ContributorId.Value;
                        project.LastUpdateDateTime = DateTime.Now;
                    }

                    assetRepoContext.SaveChanges();

                    return(RedirectToAction("Index", new { projectId = assetViewModel.ProjectRouteId }));
                }
            }

            return(new HttpNotFoundResult());
        }
        public ActionResult AddAsset(AssetViewModel assetViewModel)
        {
            if (!ModelState.IsValid)
            {
                using (var assetRepoContext = new AssetRepoContext())
                {
                    ViewBag.Projects = assetRepoContext.Projects
                                       .Where(p => p.IsActive == true)
                                       .Select(p => new SelectListItem
                    {
                        Value = p.ProjectId.ToString(),
                        Text  = p.Title
                    }).ToList();

                    ViewBag.AssetTypeSubtypePairings = assetRepoContext.AssetTypeSubtypePairings.Select(atsp => new SelectListItem
                    {
                        Value = atsp.AssetTypeSubtypePairingId.ToString(),
                        Text  = atsp.AssetType.Name + ": " + atsp.AssetSubtype.Name
                    }).ToList();

                    ViewBag.Contributors = assetRepoContext.Contributors
                                           .Where(c => c.ContributorId != 1)
                                           .Select(c => new SelectListItem
                    {
                        Value = c.ContributorId.ToString(),
                        Text  = c.Name
                    })
                                           .ToList();

                    return(View("AddEditAsset", assetViewModel));
                }
            }

            using (var assetRepoContext = new AssetRepoContext())
            {
                var asset = new Asset
                {
                    Title     = assetViewModel.Title,
                    ProjectId = assetViewModel.Project.ProjectId.Value,
                    AssetTypeSubtypePairingId = assetViewModel.TypeSubtypePairing.AssetTypeSubtypePairingId.Value,
                    CreatorId = assetViewModel.Creator.ContributorId.Value,
                    // Automatically set to current system datetime
                    CreationDateTime = DateTime.Now,
                    // Set to Creator ID value since asset is new
                    LastUpdaterId = assetViewModel.Creator.ContributorId.Value,
                    // Automatically set to current system datetime
                    LastUpdateDateTime = DateTime.Now,
                    Comment            = assetViewModel.Comment,
                    FilePlaceholder    = assetViewModel.FilePlaceholder
                };

                // Updates last updated properties for respective project
                var project = assetRepoContext.Projects.SingleOrDefault(p => p.ProjectId == assetViewModel.Project.ProjectId);
                if (project != null)
                {
                    project.LastUpdaterId      = assetViewModel.Creator.ContributorId.Value;
                    project.LastUpdateDateTime = DateTime.Now;
                }

                assetRepoContext.Assets.Add(asset);
                assetRepoContext.SaveChanges();
            }

            return(RedirectToAction("Index", new { projectId = assetViewModel.ProjectRouteId }));
        }