internal async Task <int> CreateNewProject(CreateUserProjectViewModel viewModel)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                // creat new UserProject record
                UserProject userProject = new UserProject();
                userProject.InjectFrom(viewModel);
                userProject.CreatedAt  = DateTime.Now;
                userProject.ModifiedAt = DateTime.Now;
                db.UserProjects.Add(userProject);
                await db.SaveChangesAsync();

                // initialize blank ProjectScheduleVersion
                ProjectScheduleVersion schedule = new ProjectScheduleVersion();
                schedule.ProjectID  = userProject.ID;
                schedule.VersionNum = 1;
                schedule.CreatedAt  = userProject.CreatedAt;
                schedule.SavedAt    = userProject.CreatedAt;
                schedule.Comments   = "[initialize project schedule]";
                db.ProjectScheduleVersions.Add(schedule);
                await db.SaveChangesAsync();

                // initialize 'project' GanttTask based on user-specified ProjectStartDate
                GanttTask initProject = new GanttTask()
                {
                    ProjectID = userProject.ID,
                    ProjectScheduleVersionID = schedule.ID,
                    Text      = viewModel.Name,
                    StartDate = viewModel.ProjectStartDate,
                    Duration  = 1,
                    Progress  = 0m,
                    SortOrder = 0,
                    Type      = "project",
                    Open      = true
                };
                db.GanttTasks.Add(initProject);
                await db.SaveChangesAsync();

                // initialize 'task' GanttTask for proper rendering of start/end dates in grid
                GanttTask initTask = new GanttTask()
                {
                    ProjectID = userProject.ID,
                    ProjectScheduleVersionID = schedule.ID,
                    Text      = "New Task",
                    StartDate = viewModel.ProjectStartDate,
                    Duration  = 1,
                    Progress  = 0m,
                    SortOrder = 1,
                    Type      = "task",
                    ParentId  = initProject.GanttTaskId
                };
                db.GanttTasks.Add(initTask);
                await db.SaveChangesAsync();

                return(userProject.ID);
            }
        }
        public async Task <ActionResult> Create()
        {
            ApplicationUser user = await ReturnCurrentUserAsync();

            CreateUserProjectViewModel viewModel = new CreateUserProjectViewModel();

            viewModel.ApplicationUserID = user.Id;
            ViewBag.ProjectStatusID     = new SelectList(await db.ProjectStatuses.ToListAsync(), "ID", "Description");
            return(View(viewModel));
        }
        public async Task <ActionResult> Create([Bind(Include = "ID,ApplicationUserID,Name,Description,Comments,ProjectStartDate,ProjectStatusID")] CreateUserProjectViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                UserProjectsRepository repo = new UserProjectsRepository();
                int newProjectID            = await repo.CreateNewProject(viewModel);

                Success("New Project Created Successfully", true);
                return(RedirectToAction("Details", new { id = newProjectID }));
            }
            ViewBag.ProjectStatusID = new SelectList(await db.ProjectStatuses.ToListAsync(), "ID", "Description");
            return(View(viewModel));
        }