public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var employeesDTOs = JsonConvert .DeserializeObject <ICollection <EmployeeInputModel> >(jsonString); var result = new StringBuilder(); var employeesAndTasksIds = new Dictionary <Employee, HashSet <int> >(); foreach (var employeeDTO in employeesDTOs) { if (!IsValid(employeeDTO)) { result.AppendLine(ErrorMessage); continue; } Employee employee = new Employee { Username = employeeDTO.Username, Email = employeeDTO.Email, Phone = employeeDTO.Phone, }; employeesAndTasksIds.Add(employee, employeeDTO.Tasks); } context.Employees.AddRange(employeesAndTasksIds.Keys); context.SaveChanges(); var tasksIds = context.Tasks.Select(x => x.Id).ToHashSet(); foreach (var kvp in employeesAndTasksIds) { foreach (var taskId in kvp.Value) { if (!tasksIds.Contains(taskId)) { result.AppendLine(ErrorMessage); continue; } kvp.Key.EmployeesTasks.Add(new EmployeeTask { TaskId = taskId, EmployeeId = kvp.Key.Id }); } result.AppendLine( string.Format(SuccessfullyImportedEmployee, kvp.Key.Username, kvp.Key.EmployeesTasks.Count)); } context.SaveChanges(); return(result.ToString().Trim()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var dtos = JsonConvert.DeserializeObject <ImportEmployeesDto[]>(jsonString); StringBuilder sb = new StringBuilder(); List <Employee> employees = new List <Employee>(); foreach (var dto in dtos) { if (!IsValid(dto)) { sb.AppendLine(ErrorMessage); continue; } Employee employee = new Employee { Username = dto.Username, Phone = dto.Phone, Email = dto.Email }; var dtoTasksDistincted = dto.Tasks.Distinct(); foreach (var currentTask in dtoTasksDistincted) { bool isExist = context.Tasks.Any(x => x.Id == currentTask); if (!isExist) { sb.AppendLine(ErrorMessage); continue; } EmployeeTask employeeTask = new EmployeeTask { TaskId = currentTask }; employee.EmployeesTasks.Add(employeeTask); } context.SaveChanges(); employees.Add(employee); sb.AppendLine(string.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count)); } context.Employees.AddRange(employees); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
private static string AddTasks(TeisterMaskContext context, Project project, TaskImportDTO[] tasks) { StringBuilder result = new StringBuilder(); foreach (var taskDto in tasks) { if (IsValid(taskDto) && IsValidOpenDate(taskDto.OpenDate, project.OpenDate) && IsValidDueDate(taskDto.DueDate, project.DueDate)) { Task task = new Task { Name = taskDto.Name, OpenDate = DateTime.ParseExact(taskDto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), DueDate = DateTime.ParseExact(taskDto.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), ExecutionType = (ExecutionType)taskDto.ExcecutionType, LabelType = (LabelType)taskDto.LabelType, Project = project }; context.Add(task); } else { result.AppendLine(ErrorMessage); } } context.SaveChanges(); return(result.ToString().TrimEnd()); }
public static string ImportProjects(TeisterMaskContext context, string xmlString) { var sb = new StringBuilder(); var projectsDto = XmlConvert.Deserialize <XmlProjectImportModel>(xmlString, "Projects"); foreach (var projectDto in projectsDto) { if (IsValid(projectDto) == false || DateTime.TryParseExact(projectDto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime projectOpenDate) == false) { sb.AppendLine(ErrorMessage); continue; } bool hasDueDate = DateTime.TryParseExact(projectDto.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime projectDueDate); var project = new Project { Name = projectDto.Name, DueDate = hasDueDate ? (DateTime?)projectDueDate : null, OpenDate = projectOpenDate, }; foreach (var taskDto in projectDto.Tasks) { if (IsValid(taskDto) == false || DateTime.TryParseExact(taskDto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime taskOpenDate) == false || DateTime.TryParseExact(taskDto.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime taskDueDate) == false || taskOpenDate.Date < projectOpenDate.Date) { sb.AppendLine(ErrorMessage); continue; } if (hasDueDate) { if (taskDueDate.Date > projectDueDate.Date) { sb.AppendLine(ErrorMessage); continue; } } project.Tasks.Add(new Task { Name = taskDto.Name, DueDate = taskDueDate, LabelType = (LabelType)taskDto.LabelType, OpenDate = taskOpenDate, ExecutionType = (ExecutionType)taskDto.ExecutionType, }); } context.Projects.Add(project); sb.AppendLine(string.Format(SuccessfullyImportedProject, project.Name, project.Tasks.Count)); } context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var sb = new StringBuilder(); var employeeDtos = JsonConvert.DeserializeObject <List <ImportEmployeeDto> >(jsonString); foreach (var employee in employeeDtos) { if (!IsValid(employee)) { sb.AppendLine(ErrorMessage); continue; } if (employee.Username.Any(ch => !char.IsLetterOrDigit(ch))) { sb.AppendLine(ErrorMessage); continue; } var employeeToAdd = new Employee() { Username = employee.Username, Email = employee.Email, Phone = employee.Phone }; foreach (var taskId in employee.Tasks.Distinct()) { var task = context.Tasks .FirstOrDefault(t => t.Id == taskId); if (task == null) { sb.AppendLine(ErrorMessage); continue; } var employeeTask = new EmployeeTask() { Employee = employeeToAdd, Task = task }; employeeToAdd.EmployeesTasks.Add(employeeTask); } context.Employees.Add(employeeToAdd); context.SaveChanges(); sb.AppendLine(string.Format(SuccessfullyImportedEmployee, employeeToAdd.Username, employeeToAdd.EmployeesTasks.Count)); } return(sb.ToString().Trim()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var employeesDtos = JsonConvert.DeserializeObject <EmployeeImportDto[]>(jsonString); var sb = new StringBuilder(); foreach (var dto in employeesDtos) { if (IsValid(dto)) { var employee = new Employee { Username = dto.Username, Email = dto.Email, Phone = dto.Phone, }; context.Employees.Add(employee); AddAllEmployeeTasks(context, dto.Tasks, employee.Id, ref sb); } else { sb.AppendLine(ErrorMessage); } } context.SaveChanges(); return(sb.ToString()); }
public static string ImportProjects(TeisterMaskContext context, string xmlString) { var serializer = new XmlSerializer(typeof(ProjectImportDto[]), new XmlRootAttribute(elementName: "Projects")); var objects = (ProjectImportDto[])serializer.Deserialize(new StringReader(xmlString)); var sb = new StringBuilder(); foreach (var dto in objects) { if (IsValid(dto)) { var project = new Project { Name = dto.Name, OpenDate = DateTime.ParseExact(dto.OpenDate, @"dd/MM/yyyy", CultureInfo.InvariantCulture) }; if (!(dto.DueDate == null) && dto.DueDate != string.Empty) { project.DueDate = DateTime.ParseExact(dto.DueDate, @"dd/MM/yyyy", CultureInfo.InvariantCulture); } //TODO Random new line added context.Projects.Add(project); AddAllTasks(context, project, dto.Tasks, ref sb); sb.AppendLine(string.Format(SuccessfullyImportedProject, project.Name, project.Tasks.Count)); } else { sb.AppendLine(ErrorMessage); } } context.SaveChanges(); return(sb.ToString().Trim()); }
private static void AddAllTasks(TeisterMaskContext context, Project project, TaskImportDto[] tasks, ref StringBuilder sb) { var projectId = project.Id; var projectOpenDate = project.OpenDate; var projectDueDate = project.DueDate; foreach (var dto in tasks) { if (IsValid(dto) && IsOpenDateValid(dto.OpenDate, projectOpenDate) && IsDueDateValid(dto.DueDate, projectDueDate)) { var task = new Task { Name = dto.Name, OpenDate = DateTime.ParseExact(dto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), DueDate = DateTime.ParseExact(dto.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), ExecutionType = (ExecutionType)dto.ExecutionType, LabelType = (LabelType)dto.LabelType, ProjectId = projectId }; context.Tasks.Add(task); } else { sb.AppendLine(ErrorMessage); } } context.SaveChanges(); }
private static string AddEmployeeTasks(TeisterMaskContext context, Employee employee, int[] tasks) { StringBuilder result = new StringBuilder(); foreach (var task in tasks) { if (IsTaskValid(context, task)) { EmployeeTask employeeTask = new EmployeeTask { Employee = employee, TaskId = task }; context.EmployeesTasks.Add(employeeTask); } else { result.AppendLine(ErrorMessage); } } context.SaveChanges(); return(result.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var employeeDTOs = JsonConvert .DeserializeObject <List <EmployeesImportModel> >(jsonString); var result = new StringBuilder(); foreach (var employeeDTO in employeeDTOs) { if (!IsValid(employeeDTO)) { result.AppendLine(ErrorMessage); continue; } //unique tasks var tasks = new List <Task>(); var employeeTasks = new List <EmployeeTask>(); var employee = new Employee() { Username = employeeDTO.Username, Email = employeeDTO.Email, Phone = employeeDTO.Phone }; foreach (var taskId in employeeDTO.Tasks) { var task = context.Tasks.FirstOrDefault(t => t.Id == taskId); if (task == null) { result.AppendLine(ErrorMessage); continue; //TODO: what if all tasks are null? } if (!tasks.Contains(task)) { tasks.Add(task); var newEmployeeTask = new EmployeeTask() { Employee = employee, Task = task }; employeeTasks.Add(newEmployeeTask); } } employee.EmployeesTasks = employeeTasks; result.AppendLine(String.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count)); context.Employees.Add(employee); context.SaveChanges(); } return(result.ToString().Trim()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var sb = new StringBuilder(); var employeesDtos = JsonConvert.DeserializeObject <ImportEmployeesDto[]>(jsonString); foreach (var emplDto in employeesDtos) { if (!IsValid(emplDto)) { sb.AppendLine(ErrorMessage); continue; } var validEmployee = new Employee { Username = emplDto.Username, Email = emplDto.Email, Phone = emplDto.Phone }; var validTasksIds = new List <int>(); var invalidTasksIds = new List <int>(); foreach (var taskId in emplDto.Tasks) { var currentTask = context.Tasks.FirstOrDefault(t => t.Id == taskId); if (currentTask == null) { if (!invalidTasksIds.Contains(taskId)) { sb.AppendLine(ErrorMessage); invalidTasksIds.Add(taskId); } continue; } if (validTasksIds.Contains(taskId)) { continue; } validTasksIds.Add(taskId); var employeeTask = new EmployeeTask { Task = currentTask, Employee = validEmployee }; validEmployee.EmployeesTasks.Add(employeeTask); } context.Employees.Add(validEmployee); sb.AppendLine($"Successfully imported employee - {validEmployee.Username} with {validEmployee.EmployeesTasks.Count} tasks."); } context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var employeesImport = JsonConvert.DeserializeObject <List <ImportEmployeesDto> >(jsonString); var allTasks = context.Tasks.Select(t => t.Id).ToList(); var employeeTasks = new List <EmployeeTask>(); var employees = new List <Employee>(); var sb = new StringBuilder(); foreach (var dto in employeesImport) { var isEmployeeValid = IsValid(dto); if (!isEmployeeValid) { sb.AppendLine(ErrorMessage); continue; } var employee = new Employee() { Username = dto.Username, Phone = dto.Phone, Email = dto.Email, }; var tasksToLoop = dto.Tasks.Distinct(); foreach (var taskDto in tasksToLoop) { if (!allTasks.Contains(taskDto)) { sb.AppendLine(ErrorMessage); continue; } var employeeTask = new EmployeeTask() { Employee = employee, TaskId = taskDto }; employeeTasks.Add(employeeTask); employee.EmployeesTasks.Add(employeeTask); } employees.Add(employee); var result = string.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count); sb.AppendLine(result); } context.Employees.AddRange(employees); context.EmployeesTasks.AddRange(employeeTasks); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var sb = new StringBuilder(); var employeesResultsDto = JsonConvert .DeserializeObject <ImportEmplyeesDto[]>(jsonString); var epmployeesList = new List <Employee>(); foreach (var employeeDto in employeesResultsDto) { if (!IsValid(employeeDto)) { sb.AppendLine(ErrorMessage); continue; } bool hasCorrectMail = epmployeesList.FirstOrDefault(e => e.Email == employeeDto.Email) != null; bool hasCorrectPhone = epmployeesList.FirstOrDefault(e => e.Phone == employeeDto.Phone) != null; if (hasCorrectMail && hasCorrectPhone) { sb.AppendLine(ErrorMessage); continue; } var employee = new Employee { Username = employeeDto.Username, Email = employeeDto.Email, Phone = employeeDto.Phone, }; foreach (var taskId in employeeDto.Tasks.Distinct()) { if (context.Tasks.Find(taskId) == null) { sb.AppendLine(ErrorMessage); continue; } employee.EmployeesTasks.Add(new EmployeeTask { Employee = employee, TaskId = taskId }); } epmployeesList.Add(employee); sb.AppendLine(string.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count)); } context.Employees.AddRange(epmployeesList); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportProjects(TeisterMaskContext context, string xmlString) { var serializer = new XmlSerializer(typeof(ProjectImportDto[]), new XmlRootAttribute("Projects")); ProjectImportDto[] projectDtos; using (var stream = new StringReader(xmlString)) { projectDtos = (ProjectImportDto[])serializer.Deserialize(stream); } var resultBuilder = new StringBuilder(); foreach (var projectDto in projectDtos) { var project = new Project { Name = projectDto.Name, OpenDate = DateTime.ParseExact(projectDto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), DueDate = ParseDateTime(projectDto.DueDate) }; if (!IsValid(project)) { resultBuilder.AppendLine(ErrorMessage); continue; } foreach (var taskDto in projectDto.Tasks) { var task = new Task(); task.Name = taskDto.Name; task.OpenDate = DateTime.ParseExact(taskDto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); task.DueDate = DateTime.ParseExact(taskDto.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); task.ExecutionType = (ExecutionType)Enum.ToObject(typeof(ExecutionType), taskDto.ExecutionType); task.LabelType = (LabelType)Enum.ToObject(typeof(LabelType), taskDto.LabelType); if (!IsValid(task) || task.OpenDate > project.OpenDate || task.DueDate < project.DueDate) { resultBuilder.AppendLine(ErrorMessage); continue; } project.Tasks.Add(task); } context.Projects.Add(project); resultBuilder.AppendLine(string.Format(SuccessfullyImportedProject, project.Name, project.Tasks.Count)); } context.SaveChanges(); var result = resultBuilder.ToString().TrimEnd(); return(result); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var employeeDtos = JsonConvert.DeserializeObject <ImportEmployeeDto[]>(jsonString); var sb = new StringBuilder(); var employees = new List <Employee>(); foreach (var employeeDto in employeeDtos) { if (IsValid(employeeDto) == false) { sb.AppendLine(ErrorMessage); continue; } var employee = new Employee { Username = employeeDto.Username, Email = employeeDto.Email, Phone = employeeDto.Phone }; foreach (var taskIdDto in employeeDto.Tasks) { var task = context.Tasks.Find(taskIdDto); if (task == null) { sb.AppendLine(ErrorMessage); continue; } var employeeTask = new EmployeeTask { EmployeeId = employee.Id, TaskId = task.Id, Task = task }; employee.EmployeesTasks.Add(employeeTask); } sb.AppendLine(String.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count)); employees.Add(employee); } context.Employees.AddRange(employees); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var employeesDto = JsonConvert.DeserializeObject <ImportEmployeesDto[]>(jsonString); StringBuilder sb = new StringBuilder(); var employees = new List <Employee>(); foreach (var employeeDto in employeesDto) { var currentTasks = new List <Task>(); if (!IsValid(employeeDto)) { sb.AppendLine(ErrorMessage); continue; } foreach (var taskDto in employeeDto.Tasks) { if (context.Tasks.Find(int.Parse(taskDto)) == null) { sb.AppendLine(ErrorMessage); } else { if (currentTasks.FirstOrDefault(x => x.Id == int.Parse(taskDto)) == null) { Task task = new Task { Id = int.Parse(taskDto) }; currentTasks.Add(task); } } } Employee employee = new Employee { Username = employeeDto.Username, Email = employeeDto.Email, Phone = employeeDto.Phone, EmployeesTasks = currentTasks.Select(et => new EmployeeTask { Task = et }).ToList() }; employees.Add(employee); sb.AppendLine(string.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count)); } context.Employees.AddRange(employees); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { StringBuilder sb = new StringBuilder(); ImportEmployeeDto[] employeeDtos = JsonConvert.DeserializeObject <ImportEmployeeDto[]>(jsonString); HashSet <Employee> validEmployees = new HashSet <Employee>(); foreach (ImportEmployeeDto employeeDto in employeeDtos) { if (!IsValid(employeeDtos)) { sb.AppendLine(ErrorMessage); continue; } Employee e = new Employee() { Username = employeeDto.Username, Email = employeeDto.Email, Phone = employeeDto.Phone }; HashSet <EmployeeTask> employeeTasks = new HashSet <EmployeeTask>(); foreach (int taskId in employeeDto.Tasks.Distinct()) { Task task = context .Tasks .Find(taskId); if (task == null) { sb.AppendLine(ErrorMessage); continue; } EmployeeTask employeeTask = new EmployeeTask() { Employee = e, TaskId = taskId }; employeeTasks.Add(employeeTask); } e.EmployeesTasks = employeeTasks; validEmployees.Add(e); sb.AppendLine(String.Format(SuccessfullyImportedEmployee, e.Username, employeeTasks.Count)); } context.Employees.AddRange(validEmployees); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var sb = new StringBuilder(); var employees = new List <Employee>(); var employeesTasksForImport = new List <EmployeeTask>(); var employeesDto = JsonConvert.DeserializeObject <EmployeeDto[]>(jsonString); var tasksId = context.Tasks.Select(i => i.Id).ToList(); foreach (var dto in employeesDto) { if (!IsValid(dto)) { sb.AppendLine(ErrorMessage); continue; } if (!IsUsernameValid(dto.Username)) { sb.AppendLine(ErrorMessage); } var employee = new Employee() { Username = dto.Username, Email = dto.Email, Phone = dto.Phone }; var curentTasks = new List <EmployeeTask>(); foreach (var t in dto.Tasks.Distinct()) { if (!tasksId.Contains(t)) { sb.AppendLine(ErrorMessage); continue; } var newEmployeeTask = new EmployeeTask { Employee = employee, TaskId = t }; curentTasks.Add(newEmployeeTask); } employeesTasksForImport.AddRange(curentTasks); sb.AppendLine(string.Format(SuccessfullyImportedEmployee, employee.Username, curentTasks.Count)); } context.Employees.AddRange(employees); context.EmployeesTasks.AddRange(employeesTasksForImport); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportProjects(TeisterMaskContext context, string xmlString) { StringBuilder output = new StringBuilder(); XmlSerializer serializer = new XmlSerializer(typeof(ProjectXmlDto[]), new XmlRootAttribute("Projects")); StringReader reader = new StringReader(xmlString); var projectDtos = (ProjectXmlDto[])serializer.Deserialize(reader); foreach (var proj in projectDtos) { if (!IsValid(proj)) { output.AppendLine(ErrorMessage); continue; } DateTime?dueDate = null; var parsedDueDate = DateTime.TryParseExact(proj.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var date); if (parsedDueDate) { dueDate = date; } var project = new Project { Name = proj.Name, OpenDate = DateTime.ParseExact(proj.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None), DueDate = dueDate, }; foreach (var projtask in proj.Tasks) { if (!IsValid(projtask)) { output.AppendLine(ErrorMessage); continue; } DateTime taskOpenDate = DateTime.ParseExact(projtask.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None); DateTime taskDueDate = DateTime.ParseExact(projtask.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None); if (taskOpenDate < project.OpenDate || (project.DueDate.HasValue && taskDueDate > project.DueDate.Value)) { output.AppendLine(ErrorMessage); continue; } var task = new Task { Name = projtask.Name, OpenDate = taskOpenDate, DueDate = taskDueDate, ExecutionType = (ExecutionType)projtask.ExecutionType, LabelType = (LabelType)projtask.LabelType, }; project.Tasks.Add(task); } context.Projects.Add(project); context.SaveChanges(); output.AppendLine(string.Format(SuccessfullyImportedProject, project.Name, project.Tasks.Count())); } return(output.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { StringBuilder sb = new StringBuilder(); EmployeeImportDto[] employeesDto = JsonConvert.DeserializeObject <EmployeeImportDto[]>(jsonString); List <Employee> employeesToAdd = new List <Employee>(); foreach (var employeeImport in employeesDto) { if (!IsValid(employeeImport)) { sb.AppendLine(ErrorMessage); continue; } if (!UserValidation(employeeImport.Username)) { sb.AppendLine(ErrorMessage); continue; } Employee employee = new Employee() { Username = employeeImport.Username, Email = employeeImport.Email, Phone = employeeImport.Phone, }; foreach (int taskId in employeeImport.Tasks.Distinct()) { Task task = context.Tasks .FirstOrDefault(t => t.Id == taskId); if (task == null) { sb.AppendLine(ErrorMessage); continue; } employee.EmployeesTasks.Add(new EmployeeTask() { Employee = employee, Task = task }); } employeesToAdd.Add(employee); sb.AppendLine(string.Format($"SuccessfullyImportedEmployee, {employee.Username}, {employee.EmployeesTasks.Count}")); } context.Employees.AddRange(employeesToAdd); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportProjects(TeisterMaskContext context, string xmlString) { var sb = new StringBuilder(); XmlSerializer serializer = new XmlSerializer(typeof(ProjectsXmlDto[]), new XmlRootAttribute("Projects")); var xml = (ProjectsXmlDto[])serializer.Deserialize(new StringReader(xmlString)); foreach (var projectDto in xml) { if (!IsValid(projectDto)) { sb.AppendLine(ErrorMessage); continue; } var IsprojectDueDate = DateTime.TryParseExact(projectDto.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime parsedDueDate); var project = new Project { Name = projectDto.Name, OpenDate = DateTime.ParseExact(projectDto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), DueDate = IsprojectDueDate? (DateTime?)parsedDueDate : null //isReleasedDateValid ? (DateTime?)releasedTime : null }; foreach (var taskDto in projectDto.Tasks) { if (!IsValid(taskDto)) { sb.AppendLine(ErrorMessage); continue; } var taskOpenDate = DateTime.ParseExact(taskDto.OpenDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); var taskDueDate = DateTime.ParseExact(taskDto.DueDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); if (taskOpenDate <= project.OpenDate || taskDueDate > project.DueDate) { sb.AppendLine(ErrorMessage); continue; } var labelType = (LabelType)taskDto.LabelType; var executionType = (ExecutionType)taskDto.ExecutionType; var task = context.Tasks.FirstOrDefault(x => x.Name == taskDto.Name) ?? new Task { Name = taskDto.Name, OpenDate = taskOpenDate, DueDate = taskDueDate, LabelType = labelType, ExecutionType = executionType }; project.Tasks.Add(task); } context.Projects.Add(project); context.SaveChanges(); sb.AppendLine($"Successfully imported project - {project.Name} with {project.Tasks.Count()} tasks."); } return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { StringBuilder sb = new StringBuilder(); ImportEmployeeDto[] importEmployeeDtos = JsonConvert.DeserializeObject <ImportEmployeeDto[]>(jsonString); List <Employee> employees = new List <Employee>(); foreach (var employeeDto in importEmployeeDtos) { if (!IsValid(employeeDto)) { sb.AppendLine(ErrorMessage); continue; } if (!IsUsernameValid(employeeDto.Username)) { sb.AppendLine(ErrorMessage); continue; } Employee employee = new Employee() { Username = employeeDto.Username, Email = employeeDto.Email, Phone = employeeDto.Phone, }; foreach (var taskId in employeeDto.Tasks.Distinct()) { Task task = context.Tasks.FirstOrDefault(t => t.Id == taskId); if (task == null) { sb.AppendLine(ErrorMessage); continue; } employee.EmployeesTasks.Add(new EmployeeTask() { Employee = employee, Task = task }); } employees.Add(employee); sb.AppendLine(string.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count)); } context.Employees.AddRange(employees); //auto add composite keys context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var allEmployees = JsonConvert.DeserializeObject <ImportEmployeeDto[]>(jsonString); var validEmployees = new List <Employee>(); var sb = new StringBuilder(); var addedTask = new List <Task>(); foreach (var employeeDto in allEmployees) { if (!IsValid(employeeDto)) { sb.AppendLine(ErrorMessage); continue; } var employee = new Employee { Username = employeeDto.Username, Email = employeeDto.Email, Phone = employeeDto.Phone, }; foreach (var task in employeeDto.Tasks) { var findTask = context.Tasks.FirstOrDefault(t => t.Id == task); if (findTask == null) { sb.AppendLine(ErrorMessage); continue; } if (addedTask.Contains(findTask)) { continue; } var employeeTask = new EmployeeTask { Employee = employee, Task = findTask }; addedTask.Add(findTask); employee.EmployeesTasks.Add(employeeTask); } validEmployees.Add(employee); sb.AppendLine(string.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count)); } context.Employees.AddRange(validEmployees); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var employeesDto = JsonConvert.DeserializeObject <EmployeesImportDto[]>(jsonString); var sb = new StringBuilder(); var employees = new List <Employee>(); foreach (var dto in employeesDto) { if (!IsValid(dto)) // not a valid employee { sb.AppendLine(ErrorMessage); // ErrorMessage } else { var employee = new Employee() // create employee { Username = dto.Username, Email = dto.Email, Phone = dto.Phone }; foreach (var task in dto.Tasks.Distinct()) // check each task { if (!context.Tasks.Any(t => t.Id == task)) // not a valid task ??? { sb.AppendLine(ErrorMessage); //ErrorMessage } else { var employeeTask = new EmployeeTask() { TaskId = task, Employee = employee }; employee.EmployeesTasks.Add(employeeTask); // add this task to the employee } } employees.Add(employee); // add the employee to the list of valid employees sb.AppendLine(string.Format(SuccessfullyImportedEmployee, dto.Username, employee.EmployeesTasks.Count)); } } context.Employees.AddRange(employees); context.SaveChanges(); var result = sb.ToString().TrimEnd(); return(result); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var sb = new StringBuilder(); var employeesDto = JsonConvert.DeserializeObject <IEnumerable <EmployeeImportModel> >(jsonString); var employees = new List <Employee>(); foreach (var employeeDto in employeesDto) { if (!IsValid(employeeDto)) { sb.AppendLine(ErrorMessage); continue; } var employee = new Employee { Username = employeeDto.Username, Email = employeeDto.Email, Phone = employeeDto.Phone }; var uniqueTaskIds = employeeDto.Tasks.Distinct(); foreach (var taskId in uniqueTaskIds) { var currentTask = context.Tasks.FirstOrDefault(x => x.Id == taskId); if (currentTask == null) { sb.AppendLine(ErrorMessage); continue; } else { employee.EmployeesTasks.Add(new EmployeeTask { Employee = employee, Task = currentTask }); } } employees.Add(employee); sb.AppendLine(string.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count)); } context.Employees.AddRange(employees); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var employeesDto = JsonConvert.DeserializeObject <List <EmployeeImportDto> >(jsonString); StringBuilder sb = new StringBuilder(); var validEmployees = new List <Employee>(); foreach (var dto in employeesDto) { if (!IsValid(dto)) { sb.AppendLine(ErrorMessage); continue; } var employee = new Employee { Username = dto.Username, Email = dto.Email, Phone = dto.Phone }; var validTasks = new List <EmployeeTask>(); foreach (var taskNUmber in dto.Tasks.Distinct()) { var task = context.Tasks.FirstOrDefault(t => t.Id == taskNUmber); if (task == null) { sb.AppendLine(ErrorMessage); continue; } var taskEmployee = new EmployeeTask() { Employee = employee, Task = task }; validTasks.Add(taskEmployee); } employee.EmployeesTasks = validTasks; validEmployees.Add(employee); sb.AppendLine(string.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count)); } context.Employees.AddRange(validEmployees); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var emoployeesDto = JsonConvert.DeserializeObject <ImportEmployeeDto[]>(jsonString); var sb = new StringBuilder(); var employees = new List <Employee>(); foreach (var employeeDto in emoployeesDto) { //If all prop are valid in DtoEmployee if (!IsValid(employeeDto)) { sb.AppendLine(ErrorMessage); continue; } //Create new Employee var currentEmployee = new Employee() { Username = employeeDto.Username, Email = employeeDto.Email, Phone = employeeDto.Phone }; //Check if alldistinct tasks are valid foreach (var tasks in employeeDto.Tasks.Distinct()) { var findTaskInDb = context.Tasks.FirstOrDefault(t => t.Id == tasks); if (findTaskInDb == null) { sb.AppendLine(ErrorMessage); continue; } //Add valid task in Employee currentEmployee.EmployeesTasks.Add(new EmployeeTask { Employee = currentEmployee, Task = findTaskInDb }); } //Add created employee in the list of employees employees.Add(currentEmployee); sb.AppendLine(String.Format(SuccessfullyImportedEmployee, currentEmployee.Username, currentEmployee.EmployeesTasks.Count)); } //Add in DB the list of employees context.Employees.AddRange(employees); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var employeesDTO = JsonConvert.DeserializeObject <List <ImportEmployeesDTO> >(jsonString); var employees = new List <Employee>(); var sb = new StringBuilder(); //var emps = new List<EmployeeTask>(); foreach (var dto in employeesDTO) { if (!IsValid(dto)) { sb.AppendLine(ErrorMessage); } else { var employee = new Employee { Username = dto.Username, Email = dto.Username, Phone = dto.Phone }; foreach (var taskId in dto.Tasks) { if (context.Tasks.Any(a => a.Id == taskId)) { var employeeTask = new EmployeeTask { EmployeeId = employee.Id, TaskId = taskId }; //context.EmployeesTasks.Add(employeeTask); employee.EmployeesTasks.Add(employeeTask); } else { sb.AppendLine(ErrorMessage); } } employees.Add(employee); sb.AppendLine(string.Format(SuccessfullyImportedEmployee, employee.Username, employee.EmployeesTasks.Count)); } } context.Employees.AddRange(employees); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var employeesDto = JsonConvert.DeserializeObject <ImportEmployeeDto[]>(jsonString); var employees = new List <Employee>(); var employeeTasks = new List <EmployeeTask>(); var sb = new StringBuilder(); foreach (var employeeDto in employeesDto) { if (!IsValid(employeeDto)) { sb.AppendLine(ErrorMessage); continue; } Employee employee = new Employee { Username = employeeDto.Username, Email = employeeDto.Email, Phone = employeeDto.Phone }; employees.Add(employee); var currentEmployeeTasks = new List <EmployeeTask>(); foreach (int dtoTaskId in employeeDto.TasksId.ToHashSet()) { Task isTaskExist = context.Tasks.Find(dtoTaskId); if (isTaskExist == null) { sb.AppendLine(ErrorMessage); continue; } var employeeTask = new EmployeeTask { Employee = employee, TaskId = dtoTaskId }; currentEmployeeTasks.Add(employeeTask); } employeeTasks.AddRange(currentEmployeeTasks); sb.AppendLine(String.Format(CultureInfo.InvariantCulture, SuccessfullyImportedEmployee, employee.Username, currentEmployeeTasks.Count)); } context.Employees.AddRange(employees); context.EmployeesTasks.AddRange(employeeTasks); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(TeisterMaskContext context, string jsonString) { var importEmployees = JsonConvert.DeserializeObject <List <ImportEmployeeDto> >(jsonString); var sb = new StringBuilder(); var employees = new List <Employee>(); foreach (var dto in importEmployees) { if (!IsValid(dto)) { sb.AppendLine(ErrorMessage); continue; } var employee = new Employee { Username = dto.Username, Email = dto.Email, Phone = dto.Phone }; dto.Tasks = dto.Tasks.Distinct().ToList(); foreach (var task in dto.Tasks) { var currentTask = context.Tasks.FirstOrDefault(x => x.Id == task); if (currentTask == null) { sb.AppendLine(ErrorMessage); continue; } var employeeTask = new EmployeeTask { EmployeeId = employee.Id, TaskId = task }; employee.EmployeesTasks.Add(employeeTask); context.EmployeesTasks.Add(employeeTask); } context.Employees.Add(employee); context.SaveChanges(); sb.AppendLine($"Successfully imported employee - {employee.Username} with {employee.EmployeesTasks.Count()} tasks."); } return(sb.ToString().TrimEnd()); }