Пример #1
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var sb       = new StringBuilder();
            var pictures = JsonConvert.DeserializeObject <Picture[]>(jsonString);

            var picturesList = new List <Picture>();

            foreach (var pic in pictures)
            {
                bool isValid = !String.IsNullOrWhiteSpace(pic.Path) && pic.Size > 0;

                bool picExists = context.Pictures.Any(x => x.Path == pic.Path) &&
                                 picturesList.Any(x => x.Path == pic.Path);

                if (!isValid || picExists)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }

                picturesList.Add(pic);
                sb.AppendLine(String.Format(successMsg, $"Picture {pic.Path}"));
            }
            context.AddRange(picturesList);
            context.SaveChanges();

            string result = sb.ToString();

            return(result);
        }
Пример #2
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var result = new StringBuilder();

            var importedPictures = JsonConvert.DeserializeObject <List <Picture> >(jsonString);

            var pictures = new List <Picture>();

            var uniquePaths = new HashSet <string>();

            foreach (var picture in importedPictures)
            {
                var oldCount = uniquePaths.Count;
                uniquePaths.Add(picture.Path);
                var newCount = uniquePaths.Count;

                if (string.IsNullOrEmpty(picture.Path) || string.IsNullOrWhiteSpace(picture.Path) || picture.Size <= 0 || oldCount == newCount)
                {
                    result.AppendLine("Error: Invalid data.");
                    continue;
                }

                pictures.Add(picture);
                result.AppendLine($"Successfully imported Picture {picture.Path}.");
            }

            context.AddRange(pictures);
            context.SaveChanges();

            return(result.ToString().TrimEnd());
        }
Пример #3
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var desPictures = JsonConvert.DeserializeObject <Picture[]>(jsonString);

            var result        = new StringBuilder();
            var picturesToAdd = new List <Picture>();


            foreach (var p in desPictures)
            {
                bool isValid = p.Size > 0 && !string.IsNullOrWhiteSpace(p.Path);

                bool ifExists = picturesToAdd.Any(x => x.Path == p.Path) || context.Pictures.Any(x => x.Path == p.Path);

                if (isValid && !ifExists)
                {
                    picturesToAdd.Add(p);
                    result.AppendLine($"Successfully imported Picture {p.Path}.");
                }
                else
                {
                    result.AppendLine("Error: Invalid data.");
                }
            }
            context.AddRange(picturesToAdd);
            context.SaveChanges();

            return(result.ToString().TrimEnd());
        }
Пример #4
0
        public static string ImportFollowers(InstagraphContext context, string jsonString)
        {
            var result = new StringBuilder();

            var allUsers = context.Users.ToList();

            var importedUserFollowers = JsonConvert.DeserializeObject <List <ImportUserFollowerDto> >(jsonString);

            var usersFollowers = new List <UserFollower>();

            foreach (var userFollower in importedUserFollowers)
            {
                var user     = allUsers.FirstOrDefault(u => u.Username == userFollower.User);
                var follower = allUsers.FirstOrDefault(uf => uf.Username == userFollower.Follower);

                if (user == null || follower == null || usersFollowers.Any(uf => uf.User.Username == user.Username && uf.Follower.Username == follower.Username))
                {
                    result.AppendLine("Error: Invalid data.");
                    continue;
                }

                usersFollowers.Add(new UserFollower
                {
                    User     = user,
                    Follower = follower
                });

                result.AppendLine($"Successfully imported Follower {follower.Username} to User {user.Username}.");
            }

            context.AddRange(usersFollowers.Distinct());
            context.SaveChanges();

            return(result.ToString().TrimEnd());
        }
Пример #5
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var deserializedPictures = JsonConvert.DeserializeObject <PictureDto[]>(jsonString);

            var sb          = new StringBuilder();
            var pictureList = new List <Picture>();

            foreach (var deserializedPicture in deserializedPictures)
            {
                var isPathUnique = pictureList.Any(p => p.Path == deserializedPicture.Path);

                if (!IsValid(deserializedPicture) || isPathUnique)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var picture = new Picture
                {
                    Path = deserializedPicture.Path,
                    Size = deserializedPicture.Size
                };

                pictureList.Add(picture);
                sb.AppendLine($"Successfully imported Picture {deserializedPicture.Path}.");
            }

            context.AddRange(pictureList);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
        public static string ImportFollowers(InstagraphContext context, string jsonString)
        {
            var deserFollower = JsonConvert.DeserializeObject <UserFollowerDTO[]>(jsonString);
            var sb            = new StringBuilder();
            var followers     = new List <UserFollower>();

            foreach (var userFollower in deserFollower)
            {
                var userExist     = context.Users.Any(x => x.Username == userFollower.User);
                var followerExist = context.Users.Any(x => x.Username == userFollower.Follower);

                if (!userExist || !followerExist || followers.Any(x => x.User.Username == userFollower.User && x.Follower.Username == userFollower.Follower))
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }
                var userFoll = new UserFollower()
                {
                    Follower = context.Users.FirstOrDefault(x => x.Username == userFollower.Follower),
                    User     = context.Users.FirstOrDefault(x => x.Username == userFollower.User)
                };
                followers.Add(userFoll);
                sb.AppendLine($"Successfully imported Follower {userFollower.Follower} to User {userFollower.User}.");
            }
            context.AddRange(followers);
            context.SaveChanges();
            return(sb.ToString().Trim());
        }
        public static string ImportUsers(InstagraphContext context, string jsonString)
        {
            var deserializeUsers = JsonConvert.DeserializeObject <UserDto[]>(jsonString);

            var sb = new StringBuilder();

            var users = new List <User>();

            foreach (var userDto in deserializeUsers)
            {
                var isValidUsername = !String.IsNullOrWhiteSpace(userDto.Username) && userDto.Username.Length <= 30;
                var isValidPassword = !String.IsNullOrWhiteSpace(userDto.Password) && userDto.Password.Length <= 20 && !String.IsNullOrWhiteSpace(userDto.ProfilePicture);
                var picture         = context.Pictures.FirstOrDefault(p => p.Path == userDto.ProfilePicture);
                var userExist       = users.Any(u => u.Username == userDto.Username);

                if (!isValidUsername || !isValidPassword || picture == null || userExist)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }

                var user = Mapper.Map <User>(userDto);
                user.ProfilePicture = picture;

                users.Add(user);
                sb.AppendLine($"Successfully imported User {userDto.Username}.");
            }

            context.AddRange(users);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
Пример #8
0
        public static string ImportComments(InstagraphContext context, string xmlString)
        {
            var xDoc = XDocument.Parse(xmlString);

            var elements = xDoc.Root.Elements();

            StringBuilder sb = new StringBuilder();

            var comments = new List <Comment>();

            foreach (var element in elements)
            {
                string content   = element.Element("content")?.Value;
                string username  = element.Element("user")?.Value;
                string postIdStr = element.Element("post")?.Attribute("id")?.Value;


                bool isInputValid = !String.IsNullOrWhiteSpace(content) &&
                                    !String.IsNullOrWhiteSpace(username) &&
                                    !String.IsNullOrWhiteSpace(postIdStr);

                if (!isInputValid)
                {
                    sb.AppendLine(errorMessage);
                    continue;
                }

                int?postId = int.Parse(postIdStr);
                int?userId = context.Users.FirstOrDefault(x => x.Username == username)?.Id;

                bool postExist = context.Posts.Any(x => x.Id == postId);

                bool userExists = userId != null;

                if (!userExists || !postExist)
                {
                    sb.AppendLine(errorMessage);
                    continue;
                }

                var comment = new Comment()
                {
                    Content = content,
                    UserId  = userId.Value,
                    PostId  = postId.Value
                };
                comments.Add(comment);
                sb.AppendLine(String.Format(successMessage, $"Comment {content}"));
            }
            context.AddRange(comments);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
Пример #9
0
        public static string ImportPosts(InstagraphContext context, string xmlString)
        {
            var xDoc = XDocument.Parse(xmlString);

            var sb = new StringBuilder();

            var posts = new List <Post>();

            foreach (var element in xDoc.Root.Elements())
            {
                var caption     = element.Element("caption")?.Value;
                var username    = element.Element("user")?.Value;
                var picturePath = element.Element("picture")?.Value;

                bool IsValid = !String.IsNullOrWhiteSpace(caption) &&
                               !String.IsNullOrWhiteSpace(username) &&
                               !String.IsNullOrWhiteSpace(picturePath);

                if (!IsValid)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }

                int?userId    = context.Users.FirstOrDefault(u => u.Username == username)?.Id;
                int?pictureId = context.Pictures.FirstOrDefault(p => p.Path == picturePath)?.Id;

                if (userId == null || pictureId == null)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }

                var post = new Post()
                {
                    Caption   = caption,
                    UserId    = userId.Value,
                    PictureId = pictureId.Value
                };

                posts.Add(post);
                sb.AppendLine(String.Format(succsessMSg, $"Post {caption}"));
            }

            context.AddRange(posts);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
Пример #10
0
        public static string ImportComments(InstagraphContext context, string xmlString)
        {
            var result = new StringBuilder();

            var serializer = new XmlSerializer(typeof(List <ImportCommentDto>), new XmlRootAttribute("comments"));

            var importedComments = (List <ImportCommentDto>)serializer.Deserialize(new StringReader(xmlString));

            var users = context.Users.ToList();

            var posts = context.Posts.ToList();

            var comments = new List <Comment>();

            foreach (var importCommentDto in importedComments)
            {
                if (string.IsNullOrEmpty(importCommentDto.Username) || importCommentDto.ImportSinglePostDto == null)
                {
                    result.AppendLine("Error: Invalid data.");
                    continue;
                }
                var user = users.FirstOrDefault(u => u.Username == importCommentDto.Username);

                var post = posts.FirstOrDefault(p => p.Id == importCommentDto.ImportSinglePostDto.Id);

                if (user == null || post == null)
                {
                    result.AppendLine("Error: Invalid data.");
                    continue;
                }

                comments.Add(new Comment
                {
                    User    = user,
                    Post    = post,
                    Content = importCommentDto.Content
                });

                result.AppendLine($"Successfully imported Comment {importCommentDto.Content}.");
            }

            context.AddRange(comments);
            context.SaveChanges();

            return(result.ToString().TrimEnd());
        }
        public static string ImportUsers(InstagraphContext context, string jsonString)
        {
            var dbPictures = context.Pictures
                             .Select(p => p)
                             .ToArray();

            dynamic jUsers = JsonConvert.DeserializeObject(jsonString);

            var users = new List <User>();

            var builder = new StringBuilder();

            foreach (var user in jUsers)
            {
                if (user.Username is null || user.Password is null || user.ProfilePicture is null)
                {
                    builder.AppendLine("Error: Invalid data.");
                    continue;
                }

                var userPicture = dbPictures.SingleOrDefault(p => p.Path.Equals(user.ProfilePicture.ToString()));

                if (userPicture is null)
                {
                    builder.AppendLine("Error: Invalid data.");
                    continue;
                }

                users.Add(new User
                {
                    Username       = user.Username,
                    Password       = user.Password,
                    ProfilePicture = userPicture
                });

                builder.AppendLine($"Successfully imported User {user.Username}.");
            }

            context.AddRange(users);

            context.SaveChanges();

            return(builder.ToString());
        }
        public static string ImportComments(InstagraphContext context, string xmlString)
        {
            var xDoc     = XDocument.Parse(xmlString);
            var elements = xDoc.Root.Elements();
            var list     = new List <Comment>();
            var sb       = new StringBuilder();

            foreach (var xElement in elements)
            {
                var content  = xElement.Element("content")?.Value;
                var username = xElement.Element("user")?.Value;
                var postId   = xElement.Element("post")?.Attribute("id")?.Value;

                bool isValid = !string.IsNullOrWhiteSpace(content) && content.Length <= 250 &&
                               !string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(postId);
                if (!isValid)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }
                var user     = context.Users.FirstOrDefault(x => x.Username == username);
                var postInDb = context.Posts.FirstOrDefault(x => x.Id == int.Parse(postId));
                if (user == null || postInDb == null)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }
                var comment = new Comment()
                {
                    Content = content,
                    PostId  = int.Parse(postId),
                    Post    = postInDb,
                    User    = user
                };
                list.Add(comment);
                sb.AppendLine($"Successfully imported Comment {content}.");
            }
            context.AddRange(list);
            context.SaveChanges();
            Console.WriteLine(sb.ToString());
            return(sb.ToString().TrimEnd());
        }
Пример #13
0
        public static string ImportPosts(InstagraphContext context, string xmlString)
        {
            var result = new StringBuilder();

            var users = context.Users.ToList();

            var pictures = context.Pictures.ToList();

            var serializer = new XmlSerializer(typeof(List <ImportPostDto>), new XmlRootAttribute("posts"));

            var importedPosts = (List <ImportPostDto>)serializer.Deserialize(new StringReader(xmlString));

            var posts = new List <Post>();

            foreach (var importPostDto in importedPosts)
            {
                var user = users.FirstOrDefault(u => u.Username == importPostDto.Username);

                var picture = pictures.FirstOrDefault(p => p.Path == importPostDto.PicturePath);

                if (user == null || picture == null)
                {
                    result.AppendLine("Error: Invalid data.");
                    continue;
                }

                posts.Add(new Post
                {
                    Caption = importPostDto.Caption,
                    User    = user,
                    Picture = picture
                });

                result.AppendLine($"Successfully imported Post {importPostDto.Caption}.");
            }

            context.AddRange(posts);
            context.SaveChanges();

            return(result.ToString().TrimEnd());
        }
        public static string ImportPosts(InstagraphContext context, string xmlString)
        {
            var xDoc     = XDocument.Parse(xmlString);
            var elements = xDoc.Root.Elements();
            var list     = new List <Post>();
            var sb       = new StringBuilder();

            foreach (var element in elements)
            {
                var caption     = element.Element("caption")?.Value;
                var username    = element.Element("user")?.Value;
                var picturePath = element.Element("picture")?.Value;
                var isValid     = !string.IsNullOrWhiteSpace(caption) && !string.IsNullOrWhiteSpace(username) &&
                                  !string.IsNullOrWhiteSpace(picturePath);

                if (!isValid)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }
                var user    = context.Users.FirstOrDefault(x => x.Username == username);
                var picture = context.Pictures.FirstOrDefault(x => x.Path == picturePath);
                if (user == null || picture == null)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }
                var post = new Post()
                {
                    Caption   = caption,
                    UserId    = user.Id,
                    PictureId = picture.Id
                };

                list.Add(post);
                sb.AppendLine($"Successfully imported Post {caption}.");
            }
            context.AddRange(list);
            context.SaveChanges();
            return(sb.ToString().TrimEnd());
        }
        public static string ImportComments(InstagraphContext context, string xmlString)
        {
            var sb            = new StringBuilder();
            var xdoc          = XDocument.Parse(xmlString);
            var elements      = xdoc.Root.Elements();
            var validComments = new List <Comment>();

            foreach (var element in elements)
            {
                var content        = element.Element("content")?.Value;
                var userName       = element.Element("user")?.Value;
                var postIdAsString = element.Element("post")?.Attribute("id")?.Value;

                if (string.IsNullOrWhiteSpace(content) || string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(postIdAsString))
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }
                var user   = context.Users.FirstOrDefault(e => e.Username == userName);
                var postId = int.Parse(postIdAsString);
                var post   = context.Posts.FirstOrDefault(e => e.Id == postId);
                if (user == null || post == null)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }
                var comment = new Comment()
                {
                    Content = content,
                    Post    = post,
                    User    = user
                };
                validComments.Add(comment);
                sb.AppendLine($"Successfully imported Comment {comment.Content}.");
            }
            context.AddRange(validComments);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
Пример #16
0
        public static string ImportUsers(InstagraphContext context, string jsonString)
        {
            var result          = new StringBuilder();
            var pictures        = context.Pictures.ToList();
            var users           = new List <User>();
            var uniqueUsernames = new HashSet <string>();

            var importedUsers = JsonConvert.DeserializeObject <List <ImportUserJsonDto> >(jsonString);

            foreach (var user in importedUsers)
            {
                var profilePicture = pictures.FirstOrDefault(p => p.Path == user.ProfilePicture);
                var oldCount       = uniqueUsernames.Count;
                uniqueUsernames.Add(user.Username);
                var newCount = uniqueUsernames.Count;

                if (string.IsNullOrEmpty(user.Username) || string.IsNullOrWhiteSpace(user.Username) ||
                    string.IsNullOrEmpty(user.Password) || string.IsNullOrWhiteSpace(user.Password) ||
                    user.Username.Length > 30 || user.Password.Length > 20 ||
                    oldCount == newCount || profilePicture == null)
                {
                    result.AppendLine("Error: Invalid data.");
                    continue;
                }

                users.Add(new User
                {
                    Username       = user.Username,
                    Password       = user.Password,
                    ProfilePicture = profilePicture
                });

                result.AppendLine($"Successfully imported User {user.Username}.");
            }

            context.AddRange(users);
            context.SaveChanges();

            return(result.ToString().TrimEnd());
        }
Пример #17
0
        public static string ImportUsers(InstagraphContext context, string jsonString)
        {
            var deserializedUsers = JsonConvert.DeserializeObject <ImportUserDto[]>(jsonString);

            StringBuilder sb = new StringBuilder();

            var validUsers = new List <User>();

            foreach (var user in deserializedUsers)
            {
                bool userAlreadyExists = validUsers.Any(x => x.Username == user.Username) ||
                                         context.Users.Any(x => x.Username == user.Username);

                var picture = context.Pictures
                              .FirstOrDefault(x => x.Path == user.ProfilePicture);

                if (!IsValid(user) || userAlreadyExists || picture == null)
                {
                    sb.AppendLine(errorMessage);
                    continue;
                }

                var currUser = new User()
                {
                    Username       = user.Username,
                    Password       = user.Password,
                    ProfilePicture = picture
                };

                validUsers.Add(currUser);
                sb.AppendLine(string.Format(successMessage, $"User {user.Username}"));
            }
            context.AddRange(validUsers);
            context.SaveChanges();

            string result = sb.ToString().Trim();

            return(result);
        }
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var sb = new StringBuilder();
            var deserializedPictures = JsonConvert.DeserializeObject <Picture[]>(jsonString);

            var validPictures = new List <Picture>();

            foreach (var picture in deserializedPictures)
            {
                if (String.IsNullOrWhiteSpace(picture.Path) || validPictures.Any(e => e.Path == picture.Path) ||
                    picture.Size <= 0 || context.Pictures.Any(e => e.Path == picture.Path))
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }
                validPictures.Add(picture);
                sb.AppendLine($"Successfully imported Picture {picture.Path}.");
            }
            context.AddRange(validPictures);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
        public static string ImportPosts(InstagraphContext context, string xmlString)
        {
            Dictionary <string, User>    users    = context.Users.ToDictionary(u => u.Username);
            Dictionary <string, Picture> pictures = context.Pictures.ToDictionary(u => u.Path);
            StringBuilder sb = new StringBuilder();

            XDocument xmlPostsDoc         = XDocument.Parse(xmlString);
            IEnumerable <XElement> xPosts = xmlPostsDoc.Root.Elements();
            List <Post>            posts  = new List <Post>();

            foreach (XElement xPost in xPosts)
            {
                string username    = xPost.Element("user")?.Value;
                string caption     = xPost.Element("caption")?.Value;
                string picturePath = xPost.Element("picture")?.Value;

                bool usernameIsNotNullOrEmpty = !string.IsNullOrWhiteSpace(username);
                bool captionIsNotNullOrEmpty  = !string.IsNullOrWhiteSpace(caption);
                bool pictureIsNotNullOrEmpty  = !string.IsNullOrWhiteSpace(picturePath);

                bool userExists = false;

                if (usernameIsNotNullOrEmpty)
                {
                    userExists = users.ContainsKey(username);
                }

                bool pictureExists = false;

                if (pictureIsNotNullOrEmpty)
                {
                    pictureExists = pictures.ContainsKey(picturePath);
                }

                if (captionIsNotNullOrEmpty && userExists && pictureExists)
                {
                    User    user    = users[username];
                    Picture picture = pictures[picturePath];

                    Post newPost = new Post()
                    {
                        Caption   = caption,
                        UserId    = user.Id,
                        PictureId = picture.Id
                    };

                    posts.Add(newPost);
                    sb.AppendLine($"Successfully imported Post {caption}.");
                }
                else
                {
                    sb.AppendLine("Error: Invalid data.");
                }
            }

            context.AddRange(posts);
            context.SaveChanges();

            string result = sb.ToString().Trim();

            return(result);
        }
        public static string ImportFollowers(InstagraphContext context, string jsonString)
        {
            // SeedDatabase(context);

            dynamic usersFollowersJson = JsonConvert.DeserializeObject(jsonString);

            var dbUsers = context.Users
                          .Select(u => u)
                          .ToList();

            var dbUsersFollowers = context.UsersFollowers
                                   .Select(uf => new
            {
                uf.UserId,
                uf.FollowerId,
                uf.User.Username,
                FollowerUsername = uf.Follower.Username
            })
                                   .ToArray();

            var userFollowers = new List <UserFollower>();

            var builder = new StringBuilder();

            foreach (var ufp in usersFollowersJson)
            {
                string curUserName     = ufp.User.ToString();
                string curFollowerName = ufp.Follower.ToString();

                if (!dbUsers.Any(u => u.Username.Equals(curUserName)) ||
                    !dbUsers.Any(u => u.Username.Equals(curFollowerName)))
                {
                    builder.AppendLine("Error: Invalid data.");
                    continue;
                }

                if (dbUsersFollowers.Any(uf => uf.Username.Equals(curUserName)) &&
                    dbUsersFollowers.Any(uf => uf.FollowerUsername.Equals(curFollowerName)))
                {
                    builder.AppendLine("Error: Invalid data.");
                    continue;
                }

                var curUser     = dbUsers.SingleOrDefault(u => u.Username.Equals(curUserName));
                var curFollower = dbUsers.SingleOrDefault(f => f.Username.Equals(curFollowerName));

                if (curUser is null || curFollower is null)
                {
                    builder.AppendLine("Error: Invaldi data.");
                    continue;
                }

                if (userFollowers.Count > 0 && userFollowers.Any(
                        u => u.UserId.Equals(curUser.Id) && u.FollowerId.Equals(curFollower.Id)))
                {
                    builder.AppendLine("Error: Invalid data.");
                    continue;
                }

                userFollowers.Add(new UserFollower
                {
                    UserId     = curUser.Id,
                    FollowerId = curFollower.Id
                });

                builder.AppendLine($"Successfully imported Follower {curFollowerName} to User {curUserName}.");
            }

            //var temp = builder.ToString();

            context.AddRange(userFollowers);

            context.SaveChanges();

            return(builder.ToString());
        }