Advertisement
Guest User

Untitled

a guest
Dec 6th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1. public static string ImportUsers(InstagraphContext context, string jsonString)
  2.         {
  3.             var userDtos = JsonConvert.DeserializeObject<UserDto[]>(jsonString);
  4.             var users = new List<User>();
  5.  
  6.             var builder = new StringBuilder();
  7.  
  8.             foreach (var dto in userDtos)
  9.             {
  10.                 bool hasValidPic = context.Pictures.Any(x => x.Path == dto.ProfilePicture)
  11.                     && !string.IsNullOrWhiteSpace(dto.ProfilePicture);
  12.                 bool hasValidName = !string.IsNullOrWhiteSpace(dto.Username)
  13.                     && dto.Username.Length < 31 && !users.Any(x => x.Username == dto.Username);
  14.                 bool hasValidPass = !string.IsNullOrWhiteSpace(dto.Password)
  15.                     && dto.Password.Length < 21;
  16.  
  17.  
  18.                 if (!hasValidPic || !hasValidName || !hasValidPass)
  19.                 {
  20.                     builder.AppendLine(errorMsg);
  21.                     continue;
  22.                 }
  23.                 var profilePicture = context.Pictures.SingleOrDefault(x => x.Path == dto.ProfilePicture);
  24.  
  25.                 var user = Mapper.Map<User>(dto);
  26.                 user.ProfilePicture = profilePicture;
  27.                 users.Add(user);
  28.                 builder.AppendLine(string.Format(successMsg, $"User {user.Username}"));
  29.             }
  30.             context.Users.AddRange(users);
  31.             context.SaveChanges();
  32.  
  33.             var result = builder.ToString().TrimEnd();
  34.             return result;
  35.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement