Esempio n. 1
0
        public ActionResult UserPage(string Id)
        {
            var appUser = this.Data.Users.GetById(Id);

            UserPageViewModel vm = new UserPageViewModel()
            {
                Username = appUser.UserName,
                Tweets = appUser.Tweets.OrderByDescending(x => x.CreatedOn).ToList(),
                Tweet = new Tweet()
            };

            return View(vm);
        }
Esempio n. 2
0
        public ActionResult UserPage(string Id, [Bind(Exclude = "Id")] Tweet postedTweet)
        {
            var appUser = this.Data.Users.GetById(Id);
            if (ModelState.IsValid)
            {

                postedTweet.UserId = User.Identity.GetUserId();
                postedTweet.CreatedOn = DateTime.Now;
                appUser.Tweets.Add(postedTweet);
                this.Data.SaveChanges();

                var words = postedTweet.Message.Split();
                postedTweet.Tags = new HashSet<Tag>();
                foreach (var word in words)
                {
                    if (word[0] == '#')
                    {
                        var newTag = new Tag()
                        {
                            Name = word
                        };
                        var tag = this.Data.Tags.All().FirstOrDefault(x => x.Name == word);
                        if (tag != null)
                        {
                            postedTweet.Tags.Add(tag);
                        }
                        else
                        {
                            postedTweet.Tags.Add(newTag);
                        }
                    }
                }

                this.Data.SaveChanges();
            }

            UserPageViewModel vm = new UserPageViewModel()
            {
                Username = appUser.UserName,
                Tweets = appUser.Tweets.OrderByDescending(x => x.CreatedOn).ToList(),
                Tweet = new Tweet()
            };

            return View(vm);
        }