Exemple #1
0
        public ActionResult CreateOk(Message model, string tags)
        {
            if (model.Text == model.Title)
            {
                ModelState.AddModelError("Description", "Text and Title should have different values!");
            }

            Message message = new Message()
            {
                Title = model.Title,
                Text = model.Text,
                CreatedOn = DateTime.Now,
                Author = this.Data.Users.All().FirstOrDefault(
                x => x.UserName == this.HttpContext.User.Identity.Name)
            };

            string[] tagNames = tags.Split(',');
            foreach (var item in tagNames)
            {
                var current = this.Data.Tags.All().FirstOrDefault(x => item.ToLower() == x.Name);
                if (current == null)
                {
                    current = new Tag { Name = item.ToLower() };
                }

                message.Tags.Add(current);
            }

            this.Data.Messages.Add(message);
            this.Data.SaveChanges();
            return RedirectToAction("ByUser");
        }
Exemple #2
0
        public ActionResult AddTag(TagShortModel tag)
        {
            if (ModelState.IsValid)
            {
                var tagToAdd = new Tag();
                tagToAdd.Name = tag.Name;
                this.Data.Tags.Add(tagToAdd);
                this.Data.SaveChanges();
                return RedirectToAction("Index", new { successMessage = "Tag added" });
            }

            return RedirectToAction("Index", new { errorMessage = "Tag text out of character limit - [2, 50]" });
        }
        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);
        }