public async Task <IActionResult> Create(CreateProjectViewModel model)
        {
            // Make sure the project name is unique.
            if (await _context.Projects.AnyAsync(p => p.Name == model.Name))
            {
                ModelState.AddModelError("Name", _localizer["A project with the same name already exists."]);
            }

            // If there are image descriptions without corresponding image uploads, warn the user.
            if (model.BannerImageUpload == null && !string.IsNullOrWhiteSpace(model.BannerImageDescription))
            {
                ModelState.AddModelError("BannerImageDescription", _localizer["You can only provide a 'Banner Image Description' if you upload a 'Banner Image'."]);
            }
            if (model.DescriptiveImageUpload == null && !string.IsNullOrWhiteSpace(model.DescriptiveImageDescription))
            {
                ModelState.AddModelError("DescriptiveImageDescription", _localizer["You can only provide a 'DescriptiveImage Description' if you upload a 'DescriptiveImage'."]);
            }

            if (!ModelState.IsValid)
            {
                model.Categories = new SelectList(await _context.Categories.ToListAsync(), "Id", "Description");
                return(View(model));
            }

            var project = new Project
            {
                OwnerId         = (await _userManager.GetUserAsync(User)).Id,
                Name            = model.Name,
                Description     = model.Description,
                Goal            = model.Goal,
                Proposal        = model.Proposal,
                CreatorComments = model.CreatorComments,
                CategoryId      = model.CategoryId,
                LocationId      = model.LocationId,
                Target          = model.Target,
                Start           = model.Start,
                End             = model.End.Date.AddHours(23).AddMinutes(59).AddSeconds(59),
                BannerImage     = null
            };

            var bannerImageManager = new ImageFileManager(_context, _hostingEnvironment.WebRootPath, Path.Combine("usercontent", "bannerimages"));

            project.BannerImage = await bannerImageManager.CreateOrReplaceImageFileIfNeeded(project.BannerImage, model.BannerImageUpload, model.BannerImageDescription);

            var descriptiveImageManager = new ImageFileManager(_context, _hostingEnvironment.WebRootPath, Path.Combine("usercontent", "descriptiveimages"));

            project.DescriptiveImage = await descriptiveImageManager.CreateOrReplaceImageFileIfNeeded(project.DescriptiveImage, model.DescriptiveImageUpload, model.DescriptiveImageDescription);

            _context.Add(project);
            await _context.SaveChangesAsync();

            project.SetDescriptionVideoLink(_context, model.DescriptionVideoLink);

            // Only call this once we have a valid Project.Id
            await project.SetTags(_context, model.Hashtag?.Split(';') ?? new string[0]);

            // Notify admins and creator through e-mail
            string confirmationEmail =
                "Hi!<br>" +
                "<br>" +
                "Thanks for submitting a project on www.collaction.org!<br>" +
                "The CollAction Team will review your project as soon as possible – if it meets all the criteria we’ll publish the project on the website and will let you know, so you can start promoting it! If we have any additional questions or comments, we’ll reach out to you by email.<br>" +
                "<br>" +
                "Thanks so much for driving the CollAction / crowdacting movement!<br>" +
                "<br>" +
                "Warm regards,<br>" +
                "The CollAction team";
            string subject = $"Confirmation email - start project {project.Name}";

            ApplicationUser user = await _userManager.GetUserAsync(User);

            await _emailSender.SendEmailAsync(user.Email, subject, confirmationEmail);

            string confirmationEmailAdmin =
                "Hi!<br>" +
                "<br>" +
                $"There's a new project waiting for approval: {project.Name}<br>" +
                "Warm regards,<br>" +
                "The CollAction team";

            var administrators = await _userManager.GetUsersInRoleAsync("admin");

            foreach (var admin in administrators)
            {
                await _emailSender.SendEmailAsync(admin.Email, subject, confirmationEmailAdmin);
            }

            return(View("ThankYouCreate", new ThankYouCreateProjectViewModel()
            {
                Name = project.Name
            }));
        }
        public async Task <IActionResult> ManageProject(ManageProjectViewModel model)
        {
            Project project = await _context.Projects
                              .Where(p => p.Id == model.Id)
                              .Include(p => p.Owner)
                              .Include(p => p.Tags)
                              .ThenInclude(t => t.Tag)
                              .Include(p => p.DescriptionVideoLink)
                              .Include(p => p.BannerImage)
                              .Include(p => p.DescriptiveImage)
                              .FirstAsync();

            // If the project name changed make sure it is still unique.
            if (project.Name != model.Name && await _context.Projects.AnyAsync(p => p.Name == model.Name))
            {
                ModelState.AddModelError("Name", _localizer["A project with the same name already exists."]);
            }

            // If there are image descriptions without corresponding image uploads, warn the user.
            if (project.BannerImage == null && model.BannerImageUpload == null && !string.IsNullOrWhiteSpace(model.BannerImageDescription))
            {
                ModelState.AddModelError("BannerImageDescription", _localizer["You can only provide a 'Banner Image Description' if you upload a 'Banner Image'."]);
            }
            if (project.DescriptiveImage == null && model.DescriptiveImageUpload == null && !string.IsNullOrWhiteSpace(model.DescriptiveImageDescription))
            {
                ModelState.AddModelError("DescriptiveImageDescription", _localizer["You can only provide a 'DescriptiveImage Description' if you upload a 'DescriptiveImage'."]);
            }

            if (ModelState.IsValid)
            {
                bool approved    = model.Status == ProjectStatus.Running && project.Status == ProjectStatus.Hidden;
                bool successfull = model.Status == ProjectStatus.Successful && project.Status == ProjectStatus.Running;
                bool failed      = model.Status == ProjectStatus.Failed && project.Status == ProjectStatus.Running;

                if (approved)
                {
                    string approvalEmail =
                        "Hi!<br>" +
                        "<br>" +
                        "The CollAction Team has reviewed your project proposal and is very happy to share that your project has been approved and now live on www.collaction.org!<br>" +
                        "<br>" +
                        "So feel very welcome to start promoting it! If you have any further questions, feel free to contact the CollAction Team at [email protected]. And don’t forget to tag CollAction in your messages on social media so we can help you spread the word(FB: @collaction.org, Twitter: @collaction_org)!<br>" +
                        "<br>" +
                        "Thanks again for driving the CollAction / crowdacting movement!<br>" +
                        "<br>" +
                        "Warm regards,<br>" +
                        "The CollAction team<br>";

                    string subject = $"Approval - {project.Name}";

                    await _emailSender.SendEmailAsync(project.Owner.Email, subject, approvalEmail);
                }
                else if (successfull)
                {
                    string successEmail =
                        "Hi!<br>" +
                        "<br>" +
                        "The deadline of the project you have started on www.collaction.org has passed. We're very happy to see that the target you have set has been reached! Congratulations! Now it's time to act collectively!<br>" +
                        "<br>" +
                        "The CollAction Team might reach out to you with more specifics (this is an automated message). If you have any further questions yourself, feel free to contact the CollAction Team at [email protected]. And don’t forget to tag CollAction in your messages on social media so we can help you spread the word on your achievement (FB: @collaction.org, Twitter: @collaction_org)!<br>" +
                        "<br>" +
                        "Thanks again for driving the CollAction / crowdacting movement!<br>" +
                        "<br>" +
                        "Warm regards,<br>" +
                        "The CollAction team<br>";

                    string subject = $"Success - {project.Name}";

                    await _emailSender.SendEmailAsync(project.Owner.Email, subject, successEmail);
                }
                else if (failed)
                {
                    string failedEmail =
                        "Hi!<br>" +
                        "<br>" +
                        "The deadline of the project you have started on www.collaction.org has passed. Unfortunately the target that you have set has not been reached. Great effort though!<br>" +
                        "<br>" +
                        "The CollAction Team might reach out to you with more specifics (this is an automated message). If you have any further questions yourself, feel free to contact the CollAction Team at [email protected].<br>" +
                        "<br>" +
                        "Thanks again for driving the CollAction / crowdacting movement and better luck next time!<br>" +
                        "<br>" +
                        "Warm regards,<br>" +
                        "The CollAction team<br>";

                    string subject = $"Failed - {project.Name}";

                    await _emailSender.SendEmailAsync(project.Owner.Email, subject, failedEmail);
                }

                project.Name            = model.Name;
                project.Description     = model.Description;
                project.Goal            = model.Goal;
                project.Proposal        = model.Proposal;
                project.CreatorComments = model.CreatorComments;
                project.CategoryId      = model.CategoryId;
                project.Target          = model.Target;
                project.Start           = model.Start;
                project.End             = model.End.Date.AddHours(23).AddMinutes(59).AddSeconds(59);
                project.Status          = model.Status;
                project.OwnerId         = model.OwnerId;
                project.DisplayPriority = model.DisplayPriority;

                var bannerImageManager = new ImageFileManager(_context, _hostingEnvironment.WebRootPath, Path.Combine("usercontent", "bannerimages"));
                project.BannerImage = await bannerImageManager.CreateOrReplaceImageFileIfNeeded(project.BannerImage, model.BannerImageUpload, model.BannerImageDescription);

                var descriptiveImageManager = new ImageFileManager(_context, _hostingEnvironment.WebRootPath, Path.Combine("usercontent", "descriptiveimages"));
                project.DescriptiveImage = await descriptiveImageManager.CreateOrReplaceImageFileIfNeeded(project.DescriptiveImage, model.DescriptiveImageUpload, model.DescriptiveImageDescription);

                await project.SetTags(_context, model.Hashtag?.Split(';') ?? new string[0]);

                project.SetDescriptionVideoLink(_context, model.DescriptionVideoLink);

                await _context.SaveChangesAsync();

                return(RedirectToAction("ManageProjectsIndex"));
            }
            else
            {
                model.UserList             = new SelectList(await _context.Users.ToListAsync(), "Id", "UserName", null);
                model.CategoryList         = new SelectList(await _context.Categories.ToListAsync(), "Id", "Name", null);
                model.DisplayPriorityList  = new SelectList(Enum.GetValues(typeof(ProjectDisplayPriority)));
                model.StatusList           = new SelectList(Enum.GetValues(typeof(ProjectStatus)));
                model.BannerImageFile      = project.BannerImage;
                model.DescriptiveImageFile = project.DescriptiveImage;
                return(View(model));
            }
        }