Esempio n. 1
0
        public static string ImportDepartmentsCells(SoftJailDbContext context, string jsonString)
        {
            var               departmentsDto = JsonConvert.DeserializeObject <impDepartmentDto[]>(jsonString);
            StringBuilder     sb             = new StringBuilder();
            List <Department> departments    = new List <Department>();

            foreach (var dto in departmentsDto)
            {
                if (!AttributeValidation.IsValid(dto) ||
                    dto.Cells.Any(x => !AttributeValidation.IsValid(x)))
                {
                    sb.AppendLine("Invalid Data");
                    continue;
                }
                departments.Add(Mapper.Map <Department>(dto));
                sb.AppendLine($"Imported {dto.Name} with {dto.Cells.Count} cells");
            }
            context.Departments.AddRange(departments);
            context.SaveChanges();
            return(sb.ToString().Trim());
        }