Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FileDataContext" /> class.
        /// </summary>
        /// <param name="dataFileUserUrl">The data file user URL.</param>
        /// <param name="dataFileTweetsUrl">The data file tweets URL.</param>
        public FileDataContext(string dataFileUserUrl, string dataFileTweetsUrl)
        {
            try
            {
                // default collections
                Users = new Collection<User>();
                UserFollowers = new Collection<Followers>();

                using (StreamReader sr = new StreamReader(dataFileUserUrl))
                {
                    string line = string.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        // Check if user is following anyone
                        var indexOfFollows = line.IndexOf(" follows ");

                        // If nothing, take entire name or get first part of name
                        var userName = (indexOfFollows == -1 ? line : line.Substring(0, indexOfFollows)).Trim();

                        // Check if user already exists
                        var user = Users.Where(x => x.Name == userName).FirstOrDefault();

                        // Checking if user already exists
                        if (user == null)
                        {
                            user = new User()
                            {
                                Name = userName.Trim(),
                                Tweets = new Collection<Tweet>()
                            };

                            this.AddUser(userName);
                        }

                        if (indexOfFollows != -1)
                        {
                            var followers = line.Substring(indexOfFollows + 9);
                            var list = followers.Split(',');
                            foreach (var item in list)
                            {
                                var follower = Users.Where(x => x.Name == item.Trim()).FirstOrDefault();
                                if (follower == null)
                                {
                                    follower = new User()
                                    {
                                        Name = item.Trim(),
                                        Tweets = new Collection<Tweet>()
                                    };

                                    this.AddUser(item);
                                }

                                this.AddFollower(user, follower);
                            }
                        }
                    }
                };

                using (StreamReader sr = new StreamReader(dataFileTweetsUrl))
                {
                    int id = 0;
                    string line = string.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        id++;
                        var userName = line.Substring(0, line.IndexOf(">"));
                        var message = line.Substring(line.IndexOf(">") + 1);

                        var user = Users.Where(x => x.Name == userName).FirstOrDefault();

                        // Id was used to keep track of order but an alternative is using a date field, unfortunately this was not supplied in text file
                        Tweet tweet = new Tweet()
                        {
                            ID = id,
                            Message = message,
                            User = user
                        };

                        user.Tweets.Add(tweet);
                    }
                };
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the user.
        /// </summary>
        /// <param name="name">The name.</param>
        public void AddUser(string userName)
        {
            var user = new User()
            {
                Name = userName,
                Tweets = new Collection<Tweet>()
            };

            this.Users.Add(user);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Adds the user.
 /// </summary>
 /// <param name="user">The user.</param>
 public void AddUser(User user)
 {
     this.Users.Add(user);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds the follower.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="follower">The follower.</param>
        public void AddFollower(User user, User follower)
        {
            Followers fl = new Followers()
            {
                User = user,
                FollowsUser = follower
            };

            UserFollowers.Add(fl);
        }