コード例 #1
0
        public ActionResult GetTweet(int id)
        {
            var tweet = this.Data.Tweets.Find(id);

            if (tweet == null)
            {
                return this.HttpNotFound("Tweet not found.");
            }

            var viewModel = new TweetViewModel()
                {
                    Id = tweet.Id,
                    Content = tweet.Content,
                    Uri = tweet.PageUrl,
                    TweetedOn = tweet.Date,
                    Author =
                                new UserViewModel
                                {
                                    Username = tweet.Author.UserName,
                                    ProfileImage = tweet.Author.ProfileImage ?? DefaultValues.DefaultProfileImage
                                }
            };

            return this.PartialView("_TweetPartial", viewModel);
        }
コード例 #2
0
        public ActionResult Create(TweetViewModel tweet)
        {
            var tagsMatches = Regex.Matches(tweet.Content, @"(#[a-zA-Z_\d-]+)");
            var tags = new List<Tag>();

            foreach (Match tag in tagsMatches)
            {
                tags.Add(new Tag()
                {
                    Name = tag.Value
                });
            }

            var userId = User.Identity.GetUserId();

            var newTweet = new Tweet()
            {
                AuthorId = userId,
                Content = tweet.Content,
                Tags = tags
            };

            this.tweets.Create(newTweet);

            return this.RedirectToAction("Index", "Home");

        }
コード例 #3
0
        public ActionResult GetUserTimelinePartialView()
        {
            TweetInputModel tweetInputModel = new TweetInputModel();
            TweetModel tweetModel = new TweetModel();
            IEnumerable<TweetModel> foundTweets = tweetModel.GetUserTimeline(tweetInputModel);
         
            // Transfer data from TweetModel to TweetViewModel
            IList<TweetViewModel> groupOfTweets = new List<TweetViewModel>();
            foreach (TweetModel tweet in foundTweets)
            {
                TweetViewModel tweetViewModel = new TweetViewModel();
                tweetViewModel.Message = tweet.Message;
                groupOfTweets.Add(tweetViewModel);
            }

            return PartialView("UserTimelinePartialView", groupOfTweets);
        }
コード例 #4
0
        public ActionResult Create(TweetViewModel tweet)
        {
            if (ModelState.IsValid)
            {
                var newTweet = new Tweet()
                {
                    Content = tweet.Content,
                    CreatedDate = DateTime.UtcNow,
                    Title = tweet.Title,
                    UserId = this.UserProfile.Id
                };

                tweets.Add(newTweet);
                tweets.SaveChanges();

                var givenTags = tweet.Tags.Split(';');
                foreach (var tag in givenTags)
                {
                    var tagDb = this.tags.All().FirstOrDefault(t => t.Title == tag);
                    if (tagDb != null)
                    {
                        tagDb.Tweets.Add(newTweet);
                        this.tags.Update(tagDb);
                    }
                    else
                    {
                        var newTag = new Tag() {Title = tag};
                        newTag.Tweets.Add(newTweet);
                        this.tags.Add(newTag);
                    }
                }

                this.tags.SaveChanges();

                return RedirectToAction("Mine");
            }

            return View(tweet);
        }