コード例 #1
0
        public static string ImportEmployees(TeisterMaskContext context, string jsonString)
        {
            StringBuilder result = new StringBuilder();

            var employees         = JsonConvert.DeserializeObject <EmployeeImportDto[]>(jsonString);
            var employeesToImport = new List <Employee>();
            var tasksIds          = context.Tasks.Select(x => x.Id).ToList();

            foreach (var currantEmployee in employees)
            {
                var tasksToImport = new List <int>();

                if (IsValid(currantEmployee))
                {
                    foreach (var task in currantEmployee.Tasks.Distinct())
                    {
                        if (tasksIds.Contains(task))
                        {
                            tasksToImport.Add(task);
                        }
                        else
                        {
                            result.AppendLine(ErrorMessage);
                        }
                    }

                    var employee = new Employee
                    {
                        Username       = currantEmployee.Username,
                        Email          = currantEmployee.Email,
                        Phone          = currantEmployee.Phone,
                        EmployeesTasks = tasksToImport.Select(x => new EmployeeTask {
                            TaskId = x
                        }).ToList()
                    };

                    employeesToImport.Add(employee);
                    result.AppendLine(string.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count));
                }
                else
                {
                    result.AppendLine(ErrorMessage);
                }
            }

            context.AddRange(employeesToImport);
            context.SaveChanges();

            return(result.ToString().Trim());
        }
コード例 #2
0
ファイル: Deserializer.cs プロジェクト: ValentinVPK/SoftUni
        public static string ImportProjects(TeisterMaskContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            XmlRootAttribute xmlRoot       = new XmlRootAttribute("Projects");
            XmlSerializer    xmlSerializer = new XmlSerializer(typeof(ImportProjectDto[]), xmlRoot);

            using StringReader stringReader = new StringReader(xmlString);

            ImportProjectDto[] projectDtos = (ImportProjectDto[])xmlSerializer.Deserialize(stringReader);

            HashSet <Project> projects = new HashSet <Project>();

            foreach (var projectDto in projectDtos)
            {
                if (!IsValid(projectDto))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                bool isOpenDateValid = DateTime.TryParseExact(projectDto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime openDate);

                if (!isOpenDateValid)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                DateTime?dueDate = null;
                if (!String.IsNullOrWhiteSpace(projectDto.DueDate))
                {
                    bool isDueDateValid = DateTime.TryParseExact(projectDto.DueDate, "dd/MM/yyyy",
                                                                 CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dueDateValue);

                    if (!isDueDateValid)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    dueDate = dueDateValue;
                }

                Project p = new Project()
                {
                    Name     = projectDto.Name,
                    OpenDate = openDate,
                    DueDate  = dueDate
                };

                HashSet <Task> projectTasks = new HashSet <Task>();

                foreach (var taskDto in projectDto.Tasks)
                {
                    if (!IsValid(taskDto))
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    bool isTaskOpenDateValid = DateTime.TryParseExact(taskDto.TaskOpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime taskOpenDate);

                    if (!isTaskOpenDateValid)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    bool isTaskDueDateValid = DateTime.TryParseExact(taskDto.TaskDueDate, "dd/MM/yyyy",
                                                                     CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime taskDueDate);

                    if (!isTaskDueDateValid)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    if (taskOpenDate < p.OpenDate)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    if (p.DueDate.HasValue && taskDueDate > p.DueDate)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    Task t = new Task()
                    {
                        Name          = taskDto.Name,
                        OpenDate      = taskOpenDate,
                        DueDate       = taskDueDate,
                        ExecutionType = (ExecutionType)taskDto.ExecutionType,
                        LabelType     = (LabelType)taskDto.LabelType,
                        //Project = p
                    };

                    projectTasks.Add(t);
                }

                p.Tasks = projectTasks;
                projects.Add(p);

                sb.AppendLine(String.Format(SuccessfullyImportedProject, p.Name, projectTasks.Count));
            }

            context.AddRange(projects);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
コード例 #3
0
        public static string ImportProjects(TeisterMaskContext context, string xmlString)
        {
            var sb       = new StringBuilder();
            var rootName = "Projects";
            var dtos     = XMLCustomSerializer.Deserialize <ProjectImportDTO[]>(xmlString, rootName);

            var validModels = new List <Project>();

            foreach (var dto in dtos)
            {
                if (!IsValid(dto))
                {
                    sb.AppendLine(string.Format(ErrorMessage));
                    continue;
                }
                var projectOpenDate = DateTime.ParseExact(dto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                var isDate          = DateTime.TryParseExact(dto.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime projectDueDate);

                var validTasks = new List <Task>();

                foreach (var task in dto.Tasks)
                {
                    if (!IsValid(task))
                    {
                        sb.AppendLine(string.Format(ErrorMessage));
                        continue;
                    }
                    var taskOpenDate = DateTime.ParseExact(task.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    var taskDueDate  = DateTime.ParseExact(task.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

                    if (projectOpenDate > taskOpenDate || (isDate && projectDueDate < taskDueDate))
                    {
                        sb.AppendLine(string.Format(ErrorMessage));
                        continue;
                    }
                    var curTask = new Task
                    {
                        Name          = task.Name,
                        OpenDate      = DateTime.ParseExact(task.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                        DueDate       = DateTime.ParseExact(task.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                        ExecutionType = (ExecutionType)task.ExecutionType,
                        LabelType     = (LabelType)task.LabelType
                    };

                    validTasks.Add(curTask);
                }

                var project = new Project
                {
                    Name     = dto.Name,
                    OpenDate = projectOpenDate,
                    DueDate  = (DateTime?)projectDueDate,
                    Tasks    = validTasks
                };

                validModels.Add(project);
                sb.AppendLine(string.Format(SuccessfullyImportedProject, project.Name, project.Tasks.Count));
            }

            context.AddRange(validModels);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
コード例 #4
0
        public static string ImportProjects(TeisterMaskContext context, string xmlString)
        {
            StringBuilder result = new StringBuilder();

            StringReader reader     = new StringReader(xmlString);
            var          serializer = new XmlSerializer(typeof(List <ProjectImportDto>), new XmlRootAttribute("Projects"));

            var projects         = (List <ProjectImportDto>)serializer.Deserialize(reader);
            var projectsToImport = new List <Project>();

            foreach (var currantProject in projects)
            {
                bool isValidOpenDate = DateTime.TryParseExact(currantProject.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime openDate);

                DateTime?dueDate;

                if (string.IsNullOrEmpty(currantProject.DueDate))
                {
                    dueDate = null;
                }
                else
                {
                    dueDate = DateTime.ParseExact(currantProject.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                }

                if (IsValid(currantProject) && isValidOpenDate)
                {
                    var tasks = new List <Task>();

                    foreach (var currantTask in currantProject.Tasks)
                    {
                        bool isValidTaskOpenDate = DateTime.TryParseExact(currantTask.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime taskOpenDate);

                        bool isValidTaskDueDate = DateTime.TryParseExact(currantTask.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime taskDueDate);

                        if (IsValid(currantTask) && isValidTaskDueDate && isValidTaskOpenDate &&
                            taskOpenDate >= openDate && (taskDueDate <= dueDate || dueDate == null))
                        {
                            var task = new Task
                            {
                                Name          = currantTask.Name,
                                OpenDate      = taskOpenDate,
                                DueDate       = taskDueDate,
                                ExecutionType = (ExecutionType)currantTask.ExecutionType,
                                LabelType     = (LabelType)currantTask.LableType
                            };

                            tasks.Add(task);
                        }
                        else
                        {
                            result.AppendLine(ErrorMessage);
                        }
                    }

                    var project = new Project
                    {
                        Name     = currantProject.Name,
                        OpenDate = openDate,
                        DueDate  = dueDate,
                        Tasks    = tasks
                    };

                    projectsToImport.Add(project);
                    result.AppendLine(string.Format(SuccessfullyImportedProject, project.Name, project.Tasks.Count));
                }
                else
                {
                    result.AppendLine(ErrorMessage);
                }
            }

            context.AddRange(projectsToImport);
            context.SaveChanges();
            return(result.ToString().Trim());
        }
コード例 #5
0
        public static string ImportProjects(TeisterMaskContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var serializer = new XmlSerializer(typeof(ImportProjectsDTO[]), new XmlRootAttribute("Projects"));

            using (StringReader stringReader = new StringReader(xmlString))
            {
                var projectDtos = (ImportProjectsDTO[])serializer.Deserialize(stringReader);

                var validProjects = new List <Project>();

                foreach (var project in projectDtos)
                {
                    if (!IsValid(project))
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    DateTime projectOpenDate;

                    var isProjectOpenDateValid = DateTime.TryParseExact(project.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out projectOpenDate);


                    if (!isProjectOpenDateValid)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    DateTime?projectDueDate;

                    if (!string.IsNullOrEmpty(project.DueDate))
                    {
                        DateTime projectDueDateValue;
                        var      isProjectDueDateValid = DateTime.TryParseExact(project.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out projectDueDateValue);

                        if (!isProjectDueDateValid)
                        {
                            sb.AppendLine(ErrorMessage);
                            continue;
                        }

                        projectDueDate = projectDueDateValue;
                    }
                    else
                    {
                        projectDueDate = null;
                    }

                    var projectToImport = new Project()
                    {
                        Name     = project.Name,
                        OpenDate = projectOpenDate,
                        DueDate  = projectDueDate,
                    };

                    foreach (var task in project.Tasks)
                    {
                        if (!IsValid(task))
                        {
                            sb.AppendLine(ErrorMessage);
                            continue;
                        }

                        DateTime taskOpenDate;

                        var isTaskOpenDateValid = DateTime.TryParseExact(task.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out taskOpenDate);

                        if (!isTaskOpenDateValid)
                        {
                            sb.AppendLine(ErrorMessage);
                            continue;
                        }

                        DateTime taskDueDate;

                        var isTaskDueDateValid = DateTime.TryParseExact(task.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out taskDueDate);

                        if (!isTaskDueDateValid)
                        {
                            sb.AppendLine(ErrorMessage);
                            continue;
                        }

                        if (taskOpenDate < projectOpenDate)
                        {
                            sb.AppendLine(ErrorMessage);
                            continue;
                        }

                        if (projectDueDate.HasValue)
                        {
                            if (taskDueDate > projectDueDate.Value)
                            {
                                sb.AppendLine(ErrorMessage);
                                continue;
                            }
                        }

                        var taskToImport = new Task()
                        {
                            Name          = task.Name,
                            OpenDate      = taskOpenDate,
                            DueDate       = taskDueDate,
                            ExecutionType = (ExecutionType)task.ExecutionType,
                            LabelType     = (LabelType)task.LabelType
                        };

                        projectToImport.Tasks.Add(taskToImport);
                    }
                    validProjects.Add(projectToImport);

                    sb.AppendLine(String.Format(SuccessfullyImportedProject, projectToImport.Name, projectToImport.Tasks.Count));
                }

                context.AddRange(validProjects);
                context.SaveChanges();
            }

            return(sb.ToString().TrimEnd());
        }
コード例 #6
0
        public static string ImportProjects(TeisterMaskContext context, string xmlString)
        {
            var sb = new StringBuilder();
            var projectsFromXml = XmlConverter
                                  .Deserializer <ProjectInputXml>(xmlString, "Projects");
            var projects = new List <Project>();

            foreach (var projectDto in projectsFromXml)
            {
                if (IsValid(projectDto) == false)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }
                var validDueDate = DateTime.TryParseExact(projectDto.DueDate, "dd/MM/yyyy",
                                                          CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dueDate);
                var openProjectDate = DateTime.ParseExact(projectDto.OpenDate.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                var project         = new Project
                {
                    Name     = projectDto.Name,
                    OpenDate = openProjectDate,
                    DueDate  = validDueDate ? (DateTime?)dueDate : null
                };
                foreach (var taskDto in projectDto.Tasks)
                {
                    if (IsValid(taskDto) == false)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }
                    var openTaskDate = DateTime.ParseExact(taskDto.OpenDate.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    var dueTaskDate  = DateTime.ParseExact(taskDto.DueDate.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    if (openTaskDate < openProjectDate)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }
                    if (validDueDate && dueTaskDate > dueDate)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }
                    var task = new Task
                    {
                        Name          = taskDto.Name,
                        OpenDate      = openTaskDate,
                        DueDate       = dueTaskDate,
                        ExecutionType = Enum.Parse <ExecutionType>(taskDto.ExecutionType),
                        LabelType     = Enum.Parse <LabelType>(taskDto.LabelType)
                    };
                    project.Tasks.Add(task);
                }
                sb.AppendLine($"Successfully imported project - {project.Name} with {project.Tasks.Count} tasks.");
                projects.Add(project);
            }
            context.AddRange(projects);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
            // throw new NotImplementedException();
        }