Ejemplo n.º 1
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            Picture[]     deserializedPictures = JsonConvert.DeserializeObject <Picture[]>(jsonString);
            StringBuilder sb = new StringBuilder();

            var pictures = new List <Picture>();

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

                bool pictureExists = context.Pictures.Any(p => p.Path == picture.Path) ||
                                     pictures.Any(p => p.Path == picture.Path);

                if (!isValid || pictureExists)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }
                pictures.Add(picture);
                sb.AppendLine(String.Format(successMsg, $"Picture {picture.Path}"));
            }

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

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

            return(result);
        }
Ejemplo n.º 2
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var jsonPictures = JsonConvert.DeserializeObject <Picture[]>(jsonString);

            var sb = new StringBuilder();


            var pictures = new List <Picture>();

            foreach (var p in jsonPictures)
            {
                bool isValidPath = string.IsNullOrWhiteSpace(p.Path);

                bool isValidSize = p.Size > 0;

                bool isExistPath = context.Pictures.Any(pi => pi.Path == p.Path) || pictures.Any(pi => pi.Path == p.Path);

                if (isValidPath || !isValidSize || isExistPath)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                pictures.Add(p);
                sb.AppendLine($"Successfully imported Picture {p.Path}.");
            }

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

            return(sb.ToString().TrimEnd());
        }
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var pictureDtos = JsonConvert.DeserializeObject <PictureDto[]>(jsonString);

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

            foreach (var pictureDto in pictureDtos)
            {
                var isAlreadyAdded = validObjects.Any(p => p.Path == pictureDto.Path);

                if (!IsValid(pictureDto) || isAlreadyAdded)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var picture = Mapper.Map <Picture>(pictureDto);

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

            context.Pictures.AddRange(validObjects);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
Ejemplo n.º 4
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var pictures      = JsonConvert.DeserializeObject <List <Picture> >(jsonString);
            var picturesToAdd = new List <Picture>();
            var sb            = new StringBuilder();

            foreach (var picture in pictures)
            {
                bool isValid = !string.IsNullOrWhiteSpace(picture.Path) &&
                               !context.Pictures.Any(p => p.Path == picture.Path) &&
                               picture.Size > 0;

                if (!isValid)
                {
                    sb.AppendLine(FailureMsg);
                    continue;
                }

                picturesToAdd.Add(picture);
                sb.AppendLine(string.Format(SuccessMsg, $"Picture {picture.Path}"));
            }

            context.Pictures.AddRange(picturesToAdd);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
Ejemplo n.º 5
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var deserializePicturs = JsonConvert.DeserializeObject <Picture[]>(jsonString);

            var sb = new StringBuilder();

            var pictures = new List <Picture>();

            foreach (var picture in deserializePicturs)
            {
                var isValid = !String.IsNullOrWhiteSpace(picture.Path) && picture.Size > 0;

                var isPictureExists = context.Pictures.Any(p => p.Path == picture.Path) || pictures.Any(p => p.Path == picture.Path);

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

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

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

            return(sb.ToString().Trim());
        }
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var pictures = new List <Picture>();
            var result   = new StringBuilder();

            var objPictures = JsonConvert.DeserializeObject <Picture[]>(jsonString);

            foreach (var picture in objPictures)
            {
                var ifPictureExists = pictures.Any(p => p.Path == picture.Path);
                if (!IsValid(picture) || ifPictureExists || picture.Size <= 0)
                {
                    result.AppendLine(OutputMessages.Error);
                    continue;
                }

                pictures.Add(picture);
                result.AppendLine(string.Format(OutputMessages.Picture, picture.Path));
            }

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

            return(result.ToString().Trim());
        }
Ejemplo n.º 7
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var deserializedPics = JsonConvert.DeserializeObject <Picture[]>(jsonString);

            StringBuilder sb = new StringBuilder();

            var validPictures = new List <Picture>();

            foreach (var picture in deserializedPics)
            {
                bool picAlreadyExists = validPictures.Any(x => x.Path == picture.Path) ||
                                        context.Pictures.Any(x => x.Path == picture.Path);

                if (!IsValid(picture) || picAlreadyExists)
                {
                    sb.AppendLine(errorMessage);
                    continue;
                }

                validPictures.Add(picture);
                sb.AppendLine(string.Format(successMessage, $"Picture {picture.Path}"));
            }
            context.Pictures.AddRange(validPictures);
            context.SaveChanges();

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

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

            var pictures = new List <Picture>();

            var sb = new StringBuilder();

            foreach (var pic in allPictures)
            {
                bool isValid          = !String.IsNullOrWhiteSpace(pic.Path) && pic.Size > 0;
                bool isPicExistinDB   = context.Pictures.Any(p => p.Path == pic.Path);
                bool isPicExictInList = pictures.Any(p => p.Path == pic.Path);

                if (!isValid || isPicExistinDB || isPicExictInList)
                {
                    sb.AppendLine(errorMsg);
                }
                else
                {
                    pictures.Add(pic);
                    sb.AppendLine(string.Format(successMsg, $"Picture {pic.Path}"));
                }
            }

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

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

            return(result);
        }
Ejemplo n.º 9
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var sb = new StringBuilder();

            var deserializedPictures = JsonConvert.DeserializeObject <List <PictureDto> >(jsonString);

            var pictures = new List <Picture>();

            foreach (var pictureDto in deserializedPictures)
            {
                bool pictureExists = context.Pictures.Any(p => p.Path == pictureDto.Path) ||
                                     pictures.Any(p => p.Path == pictureDto.Path);

                if (!IsValid(pictureDto) || pictureExists)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var picture = Mapper.Map <Picture>(pictureDto);

                pictures.Add(picture);

                sb.AppendLine(string.Format(SUCCESS_MESSAGE, $"Picture {picture.Path}"));
            }

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

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

            return(result);
        }
Ejemplo n.º 10
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var deserializedPicturesDto = JsonConvert.DeserializeObject <PictureDto[]>(jsonString);

            var resultSb = new StringBuilder();

            var validPictures = new List <Picture>();

            foreach (var pictureDto in deserializedPicturesDto)
            {
                bool repeatedPicture = validPictures.Any(p => p.Path == pictureDto.Path) && context.Pictures.Any(p => p.Path == pictureDto.Path);

                if (!IsValid(pictureDto) || repeatedPicture || String.IsNullOrEmpty(pictureDto.Path) || String.IsNullOrWhiteSpace(pictureDto.Path))
                {
                    resultSb.AppendLine(ErrorMsg);
                    continue;
                }

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

                validPictures.Add(picture);
                resultSb.AppendLine(String.Format(SuccessMsgPicture, picture.Path));
            }

            context.Pictures.AddRange(validPictures);
            context.SaveChanges();

            return(resultSb.ToString());
        }
Ejemplo n.º 11
0
        public static string ImportUsers(InstagraphContext context, string jsonString)
        {
            var deserializedUsersDto = JsonConvert.DeserializeObject <UserDto[]>(jsonString);

            var resultSb = new StringBuilder();

            var validUsers = new List <User>();

            foreach (var userDto in deserializedUsersDto)
            {
                bool pictureExists = context.Pictures.Any(p => p.Path == userDto.ProfilePicture);

                if (!IsValid(userDto) || !pictureExists)
                {
                    resultSb.AppendLine(ErrorMsg);
                    continue;
                }

                var user = new User()
                {
                    Username       = userDto.Username,
                    Password       = userDto.Password,
                    ProfilePicture = context.Pictures.SingleOrDefault(p => p.Path == userDto.ProfilePicture)
                };

                validUsers.Add(user);
                resultSb.AppendLine(String.Format(SuccessMsgUser, user.Username));
            }

            context.Users.AddRange(validUsers);
            context.SaveChanges();
            Console.WriteLine(resultSb.ToString());
            return(resultSb.ToString());
        }
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var sb = new StringBuilder();

            var deserializedPictures = JsonConvert.DeserializeObject <PictureDto[]>(jsonString);

            var validPicutres = new List <Picture>();

            foreach (var pictureDto in deserializedPictures)
            {
                if (!IsValid(pictureDto))
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                sb.AppendLine($"Successfully imported Picture {pictureDto.Path}.");
                var picture = Mapper.Map <Picture>(pictureDto);
                validPicutres.Add(picture);
            }

            context.Pictures.AddRange(validPicutres);
            context.SaveChanges();

            return(sb.ToString());
        }
        public static string ImportFollowers(InstagraphContext context, string jsonString)
        {
            UserFollowerImportDto[] userFollowerDtos = JsonConvert.DeserializeObject <UserFollowerImportDto[]>(jsonString);

            StringBuilder builder = new StringBuilder();

            List <UserFollower> userFollowers = new List <UserFollower>();

            foreach (var ufDto in userFollowerDtos)
            {
                var user     = context.Users.FirstOrDefault(x => x.Username == ufDto.User);
                var follower = context.Users.FirstOrDefault(x => x.Username == ufDto.Follower);

                bool alreadyFollowed = userFollowers.Any(x => x.User.Username == ufDto.User && x.Follower.Username == ufDto.Follower) ||
                                       context.UsersFollowers.Any(x => x.User.Username == ufDto.User && x.Follower.Username == ufDto.Follower);

                if (user == null || follower == null || alreadyFollowed)
                {
                    builder.AppendLine("Error: Invalid data.");
                    continue;
                }

                UserFollower uf = Mapper.Map <UserFollower>(ufDto);
                uf.User     = user;
                uf.Follower = follower;

                userFollowers.Add(uf);
                builder.AppendLine($"Successfully imported Follower {ufDto.Follower} to User {ufDto.User}.");
            }

            context.UsersFollowers.AddRange(userFollowers);
            context.SaveChanges();

            return(builder.ToString());
        }
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            PictureImportDto[] pictureDtos = JsonConvert.DeserializeObject <PictureImportDto[]>(jsonString);

            StringBuilder builder = new StringBuilder();

            List <Picture> pictures = new List <Picture>();

            foreach (var pictureDto in pictureDtos)
            {
                if (pictureDto.Size == null || pictureDto.Size <= 0 || string.IsNullOrWhiteSpace(pictureDto.Path) || context.Pictures.Any(x => x.Path == pictureDto.Path))
                {
                    builder.AppendLine("Error: Invalid data.");
                }
                else
                {
                    builder.AppendLine($"Successfully imported Picture {pictureDto.Path}.");
                    pictures.Add(Mapper.Map <Picture>(pictureDto));
                }
            }

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

            return(builder.ToString().Trim());
        }
        public static string ImportPosts(InstagraphContext context, string xmlString)
        {
            var result = new StringBuilder();
            var posts  = new List <Post>();

            var serializer = new XmlSerializer(typeof(PostDto[]), new XmlRootAttribute("posts"));
            var dtoPosts   = (PostDto[])serializer.Deserialize(new StringReader(xmlString));

            foreach (var dtoPost in dtoPosts)
            {
                var user    = context.Users.FirstOrDefault(u => u.Username == dtoPost.User);
                var picture = context.Pictures.FirstOrDefault(p => p.Path == dtoPost.Picture);
                if (!IsValid(dtoPost) || user == null || picture == null)
                {
                    result.AppendLine(OutputMessages.Error);
                    continue;
                }

                dtoPost.UserId    = user.Id;
                dtoPost.PictureId = picture.Id;
                var post = Mapper.Map <Post>(dtoPost);
                posts.Add(post);
                result.AppendLine(string.Format(OutputMessages.Post, post.Caption));
            }

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

            return(result.ToString().Trim());
        }
Ejemplo n.º 16
0
        public static string ImportUsers(InstagraphContext context, string jsonString)
        {
            var sb = new StringBuilder();

            var deserializedUsers = JsonConvert.DeserializeObject <List <UserDto> >(jsonString);

            var users = new List <User>();

            foreach (var userDto in deserializedUsers)
            {
                var picture = context.Pictures.FirstOrDefault(p => p.Path == userDto.ProfilePicture);

                if (!IsValid(userDto) || picture == null)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

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

                users.Add(user);

                sb.AppendLine(string.Format(SUCCESS_MESSAGE, $"User {user.Username}"));
            }

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

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

            return(result);
        }
        public static string ImportComments(InstagraphContext context, string xmlString)
        {
            var result   = new StringBuilder();
            var comments = new List <Comment>();

            var serializer  = new XmlSerializer(typeof(CommentDto[]), new XmlRootAttribute("comments"));
            var dtoComments = (CommentDto[])serializer.Deserialize(new StringReader(xmlString));

            foreach (var dtoComment in dtoComments)
            {
                var user = context.Users.FirstOrDefault(u => u.Username == dtoComment.User);
                var post = dtoComment.PostElement == null ? null : context.Posts.FirstOrDefault(p => p.Id == dtoComment.PostElement.PostId);
                if (!IsValid(dtoComment) || user == null || post == null)
                {
                    result.AppendLine(OutputMessages.Error);
                    continue;
                }

                dtoComment.UserId = user.Id;
                var comment = Mapper.Map <Comment>(dtoComment);
                comments.Add(comment);
                result.AppendLine(string.Format(OutputMessages.Comment, comment.Content));
            }

            context.Comments.AddRange(comments);
            context.SaveChanges();
            return(result.ToString().Trim());
        }
Ejemplo n.º 18
0
        public static string ImportPosts(InstagraphContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var serializer        = new XmlSerializer(typeof(PostDto[]), new XmlRootAttribute("posts"));
            var deserializedPosts =
                (PostDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString)));

            var validPosts = new List <Post>();

            foreach (var postDto in deserializedPosts)
            {
                var user    = context.Users.FirstOrDefault(u => u.Username == postDto.User);
                var picture = context.Pictures.FirstOrDefault(p => p.Path == postDto.Picture);

                if (!IsValid(postDto) || (user == null || picture == null))
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var post = Mapper.Map <Post>(postDto);
                post.User    = user;
                post.Picture = picture;

                sb.AppendLine($"Successfully imported Post {postDto.Caption}.");

                validPosts.Add(post);
            }

            context.Posts.AddRange(validPosts);
            context.SaveChanges();

            return(sb.ToString());
        }
        public static string ImportUsers(InstagraphContext context, string jsonString)
        {
            var users  = new List <User>();
            var result = new StringBuilder();

            var dtoUsers = JsonConvert.DeserializeObject <UserDto[]>(jsonString);

            foreach (var dtoUser in dtoUsers)
            {
                var ifuserExists = users.Any(u => u.Username == dtoUser.Username);
                if (!IsValid(dtoUser) || ifuserExists)
                {
                    result.AppendLine(OutputMessages.Error);
                    continue;
                }

                var picture = context.Pictures.FirstOrDefault(p => p.Path == dtoUser.ProfilePicture);
                if (picture == null)
                {
                    result.AppendLine(OutputMessages.Error);
                    continue;
                }

                dtoUser.ProfilePictureId = picture.Id;
                users.Add(Mapper.Map <User>(dtoUser));
                result.AppendLine(string.Format(OutputMessages.User, dtoUser.Username));
            }

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

            return(result.ToString().Trim());
        }
Ejemplo n.º 20
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var deserializedPictures = JsonConvert.DeserializeObject <PictureDto[]>(jsonString);

            var validPictures = new List <Picture>();

            var sb = new StringBuilder();

            foreach (var pictureDto in deserializedPictures)
            {
                if (!IsValid(pictureDto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var picture = new Picture()
                {
                    Path = pictureDto.Path,
                    Size = pictureDto.Size
                };

                validPictures.Add(picture);
                sb.AppendLine(String.Format(PictureImportSuccess, picture.Path));
            }

            context.Pictures.AddRange(validPictures);
            context.SaveChanges();

            return(sb.ToString());
        }
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            List <Picture> picsFromJson = ImportFromJson <Picture>(jsonString);
            StringBuilder  sb           = new StringBuilder();
            List <Picture> resultPics   = new List <Picture>();

            foreach (Picture picture in picsFromJson)
            {
                bool isPicValid       = !string.IsNullOrWhiteSpace(picture.Path) && picture.Size > 0;
                bool picAlreadyExists = resultPics.Any(p => p.Path == picture.Path);

                if (!isPicValid || picAlreadyExists)
                {
                    sb.AppendLine(ErrorMsg);
                    continue;
                }

                resultPics.Add(picture);
                sb.AppendLine(string.Format(ImportedEntitySuccessMsg, nameof(Picture), picture.Path));
            }

            context.Pictures.AddRange(resultPics);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
Ejemplo n.º 22
0
        public static string ImportUsers(InstagraphContext context, string jsonString)
        {
            var deserializedUsers = JsonConvert.DeserializeObject <UserDto[]>(jsonString);

            var validUsers = new List <User>();

            var sb = new StringBuilder();

            foreach (var userDto in deserializedUsers)
            {
                var profilePicture = context.Pictures.FirstOrDefault(p => p.Path.Equals(userDto.ProfilePicture));

                if (!IsValid(userDto) || profilePicture == null)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var user = new User()
                {
                    Username       = userDto.Username,
                    Password       = userDto.Password,
                    ProfilePicture = profilePicture
                };

                validUsers.Add(user);
                sb.AppendLine(string.Format(UserImportSuccess, userDto.Username));
            }

            context.Users.AddRange(validUsers);
            context.SaveChanges();

            return(sb.ToString());
        }
Ejemplo n.º 23
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var deserializedPictures = JsonConvert.DeserializeObject <PictureDto[]>(jsonString);

            var sb = new StringBuilder();

            var validPictures = new List <Picture>();

            foreach (var dto in deserializedPictures)
            {
                if (!IsValid(dto) ||
                    validPictures.Any(p => p.Path.Equals(dto.Path, StringComparison.OrdinalIgnoreCase)))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var picture = Mapper.Map <Picture>(dto);
                validPictures.Add(picture);
                sb.AppendLine(string.Format(SuccessMessage, nameof(Picture), picture.Path));
            }

            context.Pictures.AddRange(validPictures);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
Ejemplo n.º 24
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var pictureDtos = JsonConvert.DeserializeObject <PictureDto[]>(jsonString);

            var validPictures = new List <Picture>();

            StringBuilder sb = new StringBuilder();

            foreach (var pictureDto in pictureDtos)
            {
                if (!IsValid(pictureDto) ||
                    pictureDto.Size <= 0 ||
                    validPictures.Any(p => p.Path == pictureDto.Path))
                {
                    sb.AppendLine(InvalidDataMsg);
                    continue;
                }

                var picture = Mapper.Map <Picture>(pictureDto);

                validPictures.Add(picture);

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

            context.Pictures.AddRange(validPictures);
            context.SaveChanges();

            return(sb.ToString());
        }
Ejemplo n.º 25
0
        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());
        }
Ejemplo n.º 26
0
        public static string ImportUsers(InstagraphContext context, string jsonString)
        {
            var userDtos = JsonConvert.DeserializeObject <UserDto[]>(jsonString);

            var sb = new StringBuilder();

            var validUsers = new List <User>();


            foreach (var userDto in userDtos)
            {
                var picture = context.Pictures
                              .SingleOrDefault(p => p.Path == userDto.ProfilePicture);

                if (!IsValid(userDto) || picture == null)
                {
                    sb.AppendLine(InvalidDataMsg);
                    continue;
                }

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

                user.ProfilePicture = picture;

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

                validUsers.Add(user);
            }

            context.Users.AddRange(validUsers);
            context.SaveChanges();

            return(sb.ToString());
        }
Ejemplo n.º 27
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            Picture[] deserializedPictures = JsonConvert.DeserializeObject <Picture[]>(jsonString);

            StringBuilder sb = new StringBuilder();

            var pictures = new List <Picture>();

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

                // проверяваме в базата дали я има тая снимка, а също и в колекцията снимки:
                // но на нас назата ни е празна и е достатъчно само да проверим в кол-ята
                bool pictureExists = context.Pictures.Any(p => p.Path == picture.Path) ||
                                     pictures.Any(p => p.Path == picture.Path);

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

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

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

            return(result);
        }
Ejemplo n.º 28
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            Picture[]     pictures = JsonConvert.DeserializeObject <Picture[]>(jsonString);
            StringBuilder sb       = new StringBuilder();

            var validPictures = new List <Picture>();

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

                bool pictureExists = context.Pictures.Any(x => x.Path == pic.Path) || validPictures.Any(x => x.Path == pic.Path);

                if (!isValid || pictureExists)
                {
                    sb.AppendLine(errorMessage);
                    continue;
                }
                validPictures.Add(pic);
                sb.AppendLine(String.Format(successMessage, $"Picture {pic.Path}"));
            }
            context.Pictures.AddRange(validPictures);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
Ejemplo n.º 29
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var pictures = JsonConvert.DeserializeObject <List <Picture> >(jsonString);

            StringBuilder stringBuilder = new StringBuilder();

            List <Picture> validPictures = new List <Picture>();

            foreach (var picture in pictures)
            {
                if (!IsValid(picture))
                {
                    stringBuilder.AppendLine("Error: Invalid data.");
                    continue;
                }

                var isPathExist = context.Pictures.Any(x => x.Path == picture.Path);

                if (isPathExist || validPictures.Any(x => x.Path == picture.Path))
                {
                    stringBuilder.AppendLine("Error: Invalid data.");
                    continue;
                }

                stringBuilder.AppendLine($"Successfully imported Picture {picture.Path}.");
                validPictures.Add(picture);
            }

            context.Pictures.AddRange(validPictures);
            context.SaveChanges();

            return(stringBuilder.ToString().TrimEnd());
        }
Ejemplo n.º 30
0
        public static string ImportPictures(InstagraphContext context, string jsonString)
        {
            var sb = new StringBuilder();

            Picture[] pictures      = JsonConvert.DeserializeObject <Picture[]>(jsonString);
            var       picturesToAdd = new List <Picture>();

            foreach (var picture in pictures)
            {
                bool validPath = context.Pictures
                                 .Any(p => p.Path == picture.Path) ||
                                 picturesToAdd.Any(p => p.Path == picture.Path);

                if (!string.IsNullOrWhiteSpace(picture.Path) && picture.Size > 0 && !validPath)
                {
                    picturesToAdd.Add(picture);
                    sb.AppendLine(string.Format(SuccessMsgPicture, picture.Path));
                }
                else
                {
                    sb.Append(ErrorMsg);
                }
            }
            context.Pictures.AddRange(picturesToAdd);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }