Пример #1
0
        public ActionResult InsertTweet(CreateTweetBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                this.Response.StatusCode = 400;
                return(this.Json(this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)));
            }

            var loggedUserId       = this.User.Identity.GetUserId();
            var loggedUserUsername = this.User.Identity.GetUserName();

            var tweet = new Tweet {
                Text = model.Text, AuthorId = loggedUserId, DatePosted = DateTime.Now
            };

            this.Data.Tweets.Add(tweet);

            this.Data.SaveChanges();

            return(this.PartialView(
                       "_Tweet",
                       new TweetViewModel
            {
                Id = tweet.Id,
                Author = loggedUserUsername,
                DatePosted = tweet.DatePosted,
                RetweetsCount = tweet.Retweets.Count,
                RepliesCount = tweet.Replies.Count,
                Text = tweet.Text,
                UsersFavouriteCount = tweet.UsersFavourite.Count
            }));
        }
Пример #2
0
        public ActionResult CreateTweet(CreateTweetBindingModel model)
        {
            if (ModelState.IsValid && model.TweetContent != null)
            {
                Tweet tweet = new Tweet();
                tweet.OwnerId = this.User.Identity.GetUserId();
                tweet.Content = model.TweetContent;
                this.Data.Tweets.Add(tweet);
                this.Data.Users.GetById(this.User.Identity.GetUserId()).OwnedTweets.Add(tweet);

                this.Data.SaveChanges();
                TweetViewModel tweetModel = new TweetViewModel()
                {
                    Id                = tweet.Id,
                    Content           = model.TweetContent,
                    TimeOfPublication = tweet.TimeOfPublicated,
                    Username          = this.User.Identity.GetUserName()
                };

                var hubContext = GlobalHost.ConnectionManager.GetHubContext <TweetHub>();
                hubContext.Clients.All.receiveMessage(tweetModel);

                return(RedirectToAction("Index", "User"));
            }
            else
            {
                throw new ArgumentOutOfRangeException("The content of tweet must be less than 300 symbols!");
            }

            return(RedirectToAction("Index", "User"));
        }
Пример #3
0
        public ActionResult Tweet(CreateTweetBindingModel model)
        {
            if (model == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Missing data"));
            }

            if (!this.ModelState.IsValid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, JsonConvert.SerializeObject(this.ModelState)));
            }

            var user = this.Data.Users.Find(this.User.Identity.GetUserId());

            var tweet = new Tweet()
            {
                Content   = model.Content,
                UserId    = user.Id,
                TweetDate = DateTime.Now
            };

            this.Data.Tweets.Add(tweet);

            this.Data.SaveChanges();

            TwitterHub hub = new TwitterHub();

            hub.InformFollowers(user.Followers.Select(u => u.UserName).ToList(), tweet.Id);

            return(PartialView("_FullTweetPartial", Mapper.Map <TweetViewModel>(tweet)));
        }
        public ActionResult Tweet(CreateTweetBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return Json(this.ModelState);
            }

            var user = this.Data.Users.Find(this.User.Identity.GetUserId());

            var tweet = new Tweet()
            {
                Content = model.Content,
                UserId = user.Id,
                TweetDate = DateTime.Now
            };

            this.Data.Tweets.Add(tweet);

            this.Data.SaveChanges();

            TwitterHub hub = new TwitterHub();

            hub.InformFollowers(user.Followers.Select(u => u.Id).ToList(), tweet.Id);

            return RedirectToAction("Index", "Home");
        }
Пример #5
0
        public ActionResult InsertTweet(CreateTweetBindingModel model, bool directToHome = false)
        {
            if (!this.ModelState.IsValid)
            {
                this.Response.StatusCode = 400;
                return(this.Json(this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)));
            }

            var loggedUserId       = this.User.Identity.GetUserId();
            var loggedUserUsername = this.User.Identity.GetUserName();

            // for test
            model.GroupId = 1;
            var group = Data.Group.Find(model.GroupId);

            if (group == null)
            {
                this.Response.StatusCode = 400;
                return(this.Json(this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)));
            }

            var tweet = new Tweet
            {
                Text          = model.Text,
                AuthorId      = loggedUserId,
                DatePosted    = DateTime.Now,
                IsSoftDeleted = false,
                GroupId       = model.GroupId
            };

            this.Data.Tweets.Add(tweet);
            this.Data.SaveChanges();

            if (directToHome)
            {
                return(RedirectToAction("Index", "Home"));
            }

            return(this.PartialView(
                       "_Tweet",
                       new TweetViewModel
            {
                Id = tweet.Id,
                Author = loggedUserUsername,
                DatePosted = tweet.DatePosted,
                RetweetsCount = tweet.Retweets.Count,
                RepliesCount = tweet.Reply.Count,
                Text = tweet.Text,
                UsersFavouriteCount = tweet.UsersFavourite.Count,
                GroupId = tweet.GroupId
            }));
        }
Пример #6
0
        public ActionResult CreateTweet(CreateTweetBindingModel model)
        {
            // pass Group info to view
            ViewData["GroupName"] = "";
            ViewData["GroupId"]   = model.GroupId;

            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            var loggedUserId = this.User.Identity.GetUserId();

            var group = Data.Group.Find(model.GroupId);

            if (group == null)
            {
                this.Response.StatusCode = 400;
                return(this.Json(this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)));
            }

            // update group last tweet update time
            group.LastTweetUpdateTime = DateTime.Now;
            this.Data.Group.Update(group);
            this.Data.SaveChanges();

            var tweet = new Tweet
            {
                Text          = model.Text,
                AuthorId      = loggedUserId,
                DatePosted    = DateTime.Now,
                IsSoftDeleted = false,
                GroupId       = model.GroupId
            };

            this.Data.Tweets.Add(tweet);
            this.Data.SaveChanges();

            return(this.RedirectToAction("Get", "Group", new { groupId = model.GroupId, p = 1 }));
        }
Пример #7
0
        public ActionResult CreateTweet(CreateTweetBindingModel model)
        {
            if (this.ModelState.IsValid && this.ModelState != null)
            {
                var loggedUserId = User.Identity.GetUserId();

                var tweet = new Tweet
                {
                    Content   = model.Content,
                    CreatedAt = DateTime.Now,
                    AuthorId  = loggedUserId
                };

                this.Data.Tweets.Add(tweet);
                this.Data.SaveChanges();
                this.TempData["SuccessMessage"] = "Tweet successfully created!";

                return(RedirectToAction("Index", "Home"));
            }

            return(View(model));
        }