Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string ImportUsers(InstagraphContext context, string jsonString)
- {
- var userDtos = JsonConvert.DeserializeObject<UserDto[]>(jsonString);
- var users = new List<User>();
- var builder = new StringBuilder();
- foreach (var dto in userDtos)
- {
- bool hasValidPic = context.Pictures.Any(x => x.Path == dto.ProfilePicture)
- && !string.IsNullOrWhiteSpace(dto.ProfilePicture);
- bool hasValidName = !string.IsNullOrWhiteSpace(dto.Username)
- && dto.Username.Length < 31 && !users.Any(x => x.Username == dto.Username);
- bool hasValidPass = !string.IsNullOrWhiteSpace(dto.Password)
- && dto.Password.Length < 21;
- if (!hasValidPic || !hasValidName || !hasValidPass)
- {
- builder.AppendLine(errorMsg);
- continue;
- }
- var profilePicture = context.Pictures.SingleOrDefault(x => x.Path == dto.ProfilePicture);
- var user = Mapper.Map<User>(dto);
- user.ProfilePicture = profilePicture;
- users.Add(user);
- builder.AppendLine(string.Format(successMsg, $"User {user.Username}"));
- }
- context.Users.AddRange(users);
- context.SaveChanges();
- var result = builder.ToString().TrimEnd();
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement