Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string ImportProjects(TeisterMaskContext context, string xmlString)
- {
- var xmlSerializer = new XmlSerializer(typeof(ImportProjectsDtoXml[]), new XmlRootAttribute("Projects"));
- var projectsDto = (ImportProjectsDtoXml[])xmlSerializer.Deserialize(new StringReader(xmlString));
- var sb = new StringBuilder();
- var projects = new List<Project>();
- foreach (var projectDto in projectsDto)
- {
- if (!IsValid(projectDto))
- {
- sb.AppendLine($"{ErrorMessage}");
- continue;
- }
- var tasks = new List<Task>();
- var projectOpenDate = DateTime.ParseExact(projectDto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
- var projectDueDate = projectDto.DueDate == null || projectDto.DueDate == "" ? (DateTime?)null : DateTime.ParseExact(projectDto.DueDate, @"dd/MM/yyyy", CultureInfo.InvariantCulture);
- foreach (var task in projectDto.ProjectTasks)
- {
- if (!IsValid(task))
- {
- sb.AppendLine($"{ErrorMessage}");
- continue;
- }
- var taskOpenDate = DateTime.ParseExact(task.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
- var taskDueDate = DateTime.ParseExact(task.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
- //var correctDate = true;
- //• task open date is before project open date or task due date is after project due date)
- if (taskOpenDate < projectOpenDate || taskDueDate > projectDueDate)
- {
- sb.AppendLine($"{ErrorMessage}");
- continue;
- }
- tasks.Add(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 = Enum.Parse<ExecutionType>(task.ExecutionType),
- LabelType = Enum.Parse<LabelType>(task.LabelType)
- });
- }
- var project = new Project
- {
- Name = projectDto.Name,
- OpenDate = projectOpenDate,
- DueDate = projectDueDate,
- Tasks = tasks
- };
- projects.Add(project);
- sb.AppendLine($"Successfully imported project - {project.Name} with {project.Tasks.Count} tasks.");
- }
- context.Projects.AddRange(projects);
- context.SaveChanges();
- return sb.ToString().TrimEnd();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement