public void Add(ImportResultModel model)
 {
     Rows       += model.Rows;
     Added      += model.Added;
     Updated    += model.Updated;
     ErrorCount += model.ErrorCount;
     Errors.AddRange(model.Errors);
 }
Exemplo n.º 2
0
        public async Task <object> Import(CsvReader projectsCsvReader, CsvReader contestantsCsvReader, bool debug)
        {
            var projectsResult        = new ImportResultModel();
            var participantsResult    = new ImportResultModel();
            var participantsIdsToFind = new List <string>();
            var opResponse            = await GetOldPlatformApiResponse();

            var cats = await _catsRepo.GetAll();

            while (await projectsCsvReader.ReadAsync())
            {
                ExpandoObject recordEo   = projectsCsvReader.GetRecord <dynamic>();
                var           recordDict = recordEo.ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToString());
                var           opApiObj   = opResponse.FirstOrDefault(p => p.Id == recordDict["Id"]);
                if (opApiObj == null)
                {
                    continue;
                }

                var project         = ProjectFrom(recordDict, opApiObj, cats);
                var existingProject = GetProjectCaching(project.OldPlatformId);
                if (existingProject == null)
                {
                    if (!debug)
                    {
                        project = await AddProject(project);
                    }
                    else
                    {
                        _projectsCache.Add(project);
                    }

                    projectsResult.Added++;
                }
                else
                {
                    project.IsInOpen = existingProject.IsInOpen;
                    var patchDiff = JsonPatchUtils.CreatePatch(existingProject, project);
                    SanitizePatchDocument(patchDiff);
                    if (!patchDiff.IsEmpty())
                    {
                        if (!debug)
                        {
                            patchDiff.ApplyTo(existingProject);
                        }

                        projectsResult.Updated++;
                    }
                    else
                    {
                        projectsResult.NotTouched++;
                    }
                }

                var contestantsIds = recordDict["Id [Contestants]"];
                participantsIdsToFind.AddRange(contestantsIds.Split(",", StringSplitOptions.RemoveEmptyEntries)
                                               .Select(id => id.Trim()));
            }

            participantsIdsToFind = participantsIdsToFind.Distinct().ToList();

            while (await contestantsCsvReader.ReadAsync())
            {
                ExpandoObject recordEo   = contestantsCsvReader.GetRecord <dynamic>();
                var           recordDict = recordEo.ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToString());
                var           opId       = recordDict["Id"];
                if (!participantsIdsToFind.Contains(opId))
                {
                    continue;
                }

                var participant         = ParticipantFrom(recordDict);
                var projectIds          = recordDict["Id [Projects]"].Split(",", StringSplitOptions.RemoveEmptyEntries);
                var existingParticipant = GetParticipantCaching(participant.OldPlatformId);
                if (existingParticipant == null)
                {
                    if (!debug)
                    {
                        participant.User = await AddUserAsync(participant.User);

                        participant = await AddParticipant(participant);
                    }

                    participantsResult.Added++;
                }
                else
                {
                    participant.ActivationEmailSent = existingParticipant.ActivationEmailSent;
                    var patchDiff = JsonPatchUtils.CreatePatch(existingParticipant, participant);
                    SanitizePatchDocument(patchDiff);
                    if (!patchDiff.IsEmpty())
                    {
                        if (!debug)
                        {
                            patchDiff.ApplyTo(existingParticipant);
                        }

                        participantsResult.Updated++;
                    }
                    else
                    {
                        participantsResult.NotTouched++;
                    }

                    participant = existingParticipant;
                }

                foreach (var projectId in projectIds)
                {
                    participant.Projects ??= new();
                    if (participant.Projects.All(proj => proj.OldPlatformId != projectId))
                    {
                        var project = GetProjectCaching(projectId);
                        if (project == null)
                        {
                            participantsResult.Errors.Add(
                                $"required project with id {projectId} not found (for contestant with id: " + opId +
                                ")");
                        }
                        else
                        {
                            participant.Projects.Add(project);
                        }
                    }
                }
            }


            return(new { projectsResult, participantsResult });
        }