<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CompetitionPlatform.Data.AzureRepositories.Project;
using CompetitionPlatform.Helpers;
using CompetitionPlatform.Models;
using CompetitionPlatform.Models.ProjectViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace CompetitionPlatform.Controllers
{
    public class ProjectController : Controller
    {
        private readonly IProjectRepository _projectRepository;
        private readonly IProjectCommentsRepository _projectCommentsRepository;
        private readonly IProjectFileRepository _projectFileRepository;
        private readonly IProjectFileInfoRepository _projectFileInfoRepository;

        public ProjectController(IProjectRepository projectRepository, IProjectCommentsRepository projectCommentsRepository,
            IProjectFileRepository projectFileRepository, IProjectFileInfoRepository projectFileInfoRepository)
        {
            _projectRepository = projectRepository;
            _projectCommentsRepository = projectCommentsRepository;
            _projectFileRepository = projectFileRepository;
            _projectFileInfoRepository = projectFileInfoRepository;
        }

        public IActionResult Create()
        {
            return View("CreateProject");
        }

        public async Task<IActionResult> Edit(string id)
        {
            var viewModel = await GetProjectViewModel(id);
            return View("EditProject", viewModel);
        }

        [HttpPost]
        public async Task<IActionResult> SaveProject(ProjectViewModel projectViewModel)
        {
            projectViewModel.Tags = TrimAndSerializeTags(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;

                projectId = await _projectRepository.SaveAsync(projectViewModel);
            }
            else
            {
                projectViewModel.LastModified = DateTime.UtcNow;

                projectId = projectViewModel.Id;

                await _projectRepository.UpdateAsync(projectViewModel);
            }

            await SaveProjectFile(projectViewModel.File, projectId);

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

        public async Task<IActionResult> ProjectDetails(string id)
        {
            var viewModel = await GetProjectViewModel(id);
            return View(viewModel);
        }

        private async Task<ProjectViewModel> GetProjectViewModel(string id)
        {
            var project = await _projectRepository.GetAsync(id);

            var comments = await _projectCommentsRepository.GetProjectCommentsAsync(id);

            comments = comments.OrderBy(c => c.Created).Reverse().ToList();

            var commentsPartial = new ProjectCommentPartialViewModel
            {
                ProjectId = project.Id,
                Comments = comments
            };

            var projectViewModel = new ProjectViewModel
            {
                Id = project.Id,
                Name = project.Name,
                Description = project.Description,
                Category = project.Category,
                Status = (Status)Enum.Parse(typeof(Status), project.ProjectStatus, true),
                BudgetFirstPlace = project.BudgetFirstPlace,
                BudgetSecondPlace = project.BudgetSecondPlace,
                VotesFor = project.VotesFor,
                VotesAgainst = project.VotesAgainst,
                Created = project.Created,
                LastModified = project.LastModified,
                CompetitionRegistrationDeadline = project.CompetitionRegistrationDeadline,
                ImplementationDeadline = project.ImplementationDeadline,
                VotingDeadline = project.VotingDeadline,
                CommentsPartial = commentsPartial,
                AuthorId = project.AuthorId,
                AuthorFullName = project.AuthorFullName
            };

            if (!string.IsNullOrEmpty(project.Tags))
            {
                projectViewModel.Categories = JsonConvert.DeserializeObject<List<string>>(project.Tags);

                StringBuilder builder = new StringBuilder();
                foreach (string tag in projectViewModel.Categories)
                {
                    builder.Append(tag).Append(", ");
                }
                projectViewModel.Tags = builder.ToString();<commit_msg>Add participants to project view model.<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CompetitionPlatform.Data.AzureRepositories.Project;
using CompetitionPlatform.Data.AzureRepositories.Users;
using CompetitionPlatform.Helpers;
using CompetitionPlatform.Models;
using CompetitionPlatform.Models.ProjectViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace CompetitionPlatform.Controllers
{
    public class ProjectController : Controller
    {
        private readonly IProjectRepository _projectRepository;
        private readonly IProjectCommentsRepository _projectCommentsRepository;
        private readonly IProjectFileRepository _projectFileRepository;
        private readonly IProjectFileInfoRepository _projectFileInfoRepository;
        private readonly IProjectParticipantsRepository _projectParticipantsRepository;

        public ProjectController(IProjectRepository projectRepository, IProjectCommentsRepository projectCommentsRepository,
            IProjectFileRepository projectFileRepository, IProjectFileInfoRepository projectFileInfoRepository,
            IProjectParticipantsRepository projectParticipantsRepository)
        {
            _projectRepository = projectRepository;
            _projectCommentsRepository = projectCommentsRepository;
            _projectFileRepository = projectFileRepository;
            _projectFileInfoRepository = projectFileInfoRepository;
            _projectParticipantsRepository = projectParticipantsRepository;
        }

        public IActionResult Create()
        {
            return View("CreateProject");
        }

        public async Task<IActionResult> Edit(string id)
        {
            var viewModel = await GetProjectViewModel(id);
            return View("EditProject", viewModel);
        }

        [HttpPost]
        public async Task<IActionResult> SaveProject(ProjectViewModel projectViewModel)
        {
            projectViewModel.Tags = TrimAndSerializeTags(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;

                projectId = await _projectRepository.SaveAsync(projectViewModel);
            }
            else
            {
                projectViewModel.LastModified = DateTime.UtcNow;

                projectId = projectViewModel.Id;

                await _projectRepository.UpdateAsync(projectViewModel);
            }

            await SaveProjectFile(projectViewModel.File, projectId);

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

        public async Task<IActionResult> ProjectDetails(string id)
        {
            var viewModel = await GetProjectViewModel(id);
            return View(viewModel);
        }

        private async Task<ProjectViewModel> GetProjectViewModel(string id)
        {
            var project = await _projectRepository.GetAsync(id);

            var comments = await _projectCommentsRepository.GetProjectCommentsAsync(id);

            var participants = await _projectParticipantsRepository.GetProjectParticipants(id);

            comments = comments.OrderBy(c => c.Created).Reverse().ToList();

            var commentsPartial = new ProjectCommentPartialViewModel
            {
                ProjectId = project.Id,
                Comments = comments
            };

            var participantsPartial = new ProjectParticipantsPartialViewModel()
            {
                Participants = participants
            };

            var projectViewModel = new ProjectViewModel
            {
                Id = project.Id,
                Name = project.Name,
                Description = project.Description,
                Category = project.Category,
                Status = (Status)Enum.Parse(typeof(Status), project.ProjectStatus, true),
                BudgetFirstPlace = project.BudgetFirstPlace,
                BudgetSecondPlace = project.BudgetSecondPlace,
                VotesFor = project.VotesFor,
                VotesAgainst = project.VotesAgainst,
                Created = project.Created,
                LastModified = project.LastModified,
                CompetitionRegistrationDeadline = project.CompetitionRegistrationDeadline,
                ImplementationDeadline = project.ImplementationDeadline,
                VotingDeadline = project.VotingDeadline,
                CommentsPartial = commentsPartial,
                ParticipantsPartial = participantsPartial,
                AuthorId = project.AuthorId,
                AuthorFullName = project.AuthorFullName
            };

            if (!string.IsNullOrEmpty(project.Tags))
            {
                projectViewModel.Categories = JsonConvert.DeserializeObject<List<string>>(project.Tags);

                StringBuilder builder = new StringBuilder();
                foreach (string tag in projectViewModel.Categories)
                {
                    builder.Append(tag).Append(", ");
                }
                projectViewModel.Tags = builder.ToString();
{'fraction_non_alphanumeric': 0.06336114884533281, 'fraction_numerical': 0.0, 'mean_word_length': 2.2887683471601785, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1560530', 'n_tokens_mistral': 2342, 'n_tokens_neox': 2145, 'n_words': 448}