public async Task <IActionResult> SaveProject(ProjectViewModel projectViewModel, bool draft = false, bool enableVoting = false, bool enableRegistration = false)
        {
            projectViewModel.Tags = SerializeTags(projectViewModel.Tags);

            projectViewModel.ProjectStatus = projectViewModel.Status.ToString();

            projectViewModel.SkipVoting       = !enableVoting;
            projectViewModel.SkipRegistration = !enableRegistration;
            if (projectViewModel.CompetitionRegistrationDeadline == DateTime.MinValue)
            {
                projectViewModel.CompetitionRegistrationDeadline = DateTime.UtcNow.Date;
            }

            if (projectViewModel.VotingDeadline == DateTime.MinValue)
            {
                projectViewModel.VotingDeadline = DateTime.UtcNow.Date;
            }

            var idValid = Regex.IsMatch(projectViewModel.Id, @"^[a-z0-9-]+$") && !string.IsNullOrEmpty(projectViewModel.Id);

            if (!idValid)
            {
                ViewBag.ProjectCategories = _categoriesRepository.GetCategories();
                ModelState.AddModelError("Id", "Project Url can only contain lowercase letters, numbers and the dash symbol and cannot be empty!");
                return(View("CreateProject", projectViewModel));
            }

            var project = await _projectRepository.GetAsync(projectViewModel.Id);

            if (project == null)
            {
                projectViewModel.Status = draft ? Status.Draft : Status.Initiative;

                var user = GetAuthenticatedUser();

                projectViewModel.AuthorId       = user.Email;
                projectViewModel.AuthorFullName = user.GetFullName();
                projectViewModel.UserAgent      = HttpContext.Request.Headers["User-Agent"].ToString();

                projectViewModel.Created           = DateTime.UtcNow;
                projectViewModel.ParticipantsCount = 0;

                var projectId = await _projectRepository.SaveAsync(projectViewModel);

                if (_emailsQueue != null)
                {
                    var message = NotificationMessageHelper.ProjectCreatedMessage(user.Email, user.GetFullName(),
                                                                                  projectViewModel.Name);
                    await _emailsQueue.PutMessageAsync(message);
                }

                await SaveProjectFile(projectViewModel.File, projectId);

                return(RedirectToAction("ProjectDetails", "Project", new { id = projectId }));
            }
            ViewBag.ProjectCategories = _categoriesRepository.GetCategories();
            ModelState.AddModelError("Id", "Project with that Project Url already exists!");
            return(View("CreateProject", projectViewModel));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> SaveProject(ProjectViewModel projectViewModel)
        {
            projectViewModel.Tags = SerializeTags(projectViewModel.Tags);

            projectViewModel.ProjectStatus = projectViewModel.Status.ToString();

            string projectId;

            if (projectViewModel.Id == null)
            {
                var user = GetAuthenticatedUser();

                projectViewModel.AuthorId       = user.Email;
                projectViewModel.AuthorFullName = user.GetFullName();

                projectViewModel.Created           = DateTime.UtcNow;
                projectViewModel.ParticipantsCount = 0;

                projectId = await _projectRepository.SaveAsync(projectViewModel);

                if (_emailsQueue != null)
                {
                    var message = NotificationMessageHelper.ProjectCreatedMessage(user.Email, user.GetFullName(),
                                                                                  projectViewModel.Name);
                    await _emailsQueue.PutMessageAsync(message);
                }
            }
            else
            {
                var project = await _projectRepository.GetAsync(projectViewModel.Id);

                project.Status = (Status)Enum.Parse(typeof(Status), project.ProjectStatus, true);

                projectViewModel.LastModified = DateTime.UtcNow;

                projectId = projectViewModel.Id;

                await _projectRepository.UpdateAsync(projectViewModel);

                if (project.Status != Status.CompetitionRegistration && projectViewModel.Status == Status.CompetitionRegistration)
                {
                    await AddCompetitionMailToQueue(project);
                }

                if (project.Status != Status.Implementation && projectViewModel.Status == Status.Implementation)
                {
                    await AddImplementationMailToQueue(project);
                }

                if (project.Status != Status.Voting && projectViewModel.Status == Status.Voting)
                {
                    await AddVotingMailToQueue(project);
                }

                if (project.Status != Status.Archive && projectViewModel.Status == Status.Archive)
                {
                    await _winnersService.SaveWinners(projectViewModel.Id);
                }
            }

            await SaveProjectFile(projectViewModel.File, projectId);

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