Ejemplo n.º 1
0
        public ActionResult CreateTweet(TweetViewModel tweet)
        {
            var user = this.Data.Users.GetById(User.Identity.GetUserId());

            if (!ModelState.IsValid)
            {

                ModelState.AddModelError("CustomError",
                    "Tweet length must be between 1 and 140 symbols!");
            }
            else
            {
                Tweet entity = new Tweet()
                {
                    DateTweeted = DateTime.Now,
                    Text = tweet.Text,
                    User = user
                };

                this.Data.Tweets.Add(entity);

                var tags = Regex.Matches(tweet.Text, @"((?:#){1}[\w\d]{1,140})");

                foreach (object tag in tags)
                {
                    string tagAsString = tag.ToString();

                    Tag newTag = this.Data.Tags.All().FirstOrDefault(t => t.Name == tagAsString);

                    if (newTag == null)
                    {
                        newTag = new Tag()
                        {
                            Name = tagAsString
                        };
                    }

                    entity.Tags.Add(newTag);
                }

                this.Data.SaveChanges();
            }

            var tweets = user.Tweets
                                 .Select(t => new TweetViewModel()
                                 {
                                     DateTweeted = t.DateTweeted,
                                     Text = t.Text,
                                     Author = t.User.UserName
                                 })
                                 .OrderByDescending(t => t.DateTweeted)
                                 .ToList();

            return PartialView("_TweetPartial", tweets);
        }
Ejemplo n.º 2
0
        public ActionResult Create(Tag tag)
        {
            if (ModelState.IsValid)
            {
                this.Data.Tags.Add(tag);
                this.Data.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tag);
        }
Ejemplo n.º 3
0
 public ActionResult Edit(Tag tag)
 {
     if (ModelState.IsValid)
     {
         this.Data.Tags.Update(tag);
         //db.Entry(tag).State = EntityState.Modified;
         this.Data.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(tag);
 }
 public ActionResult Edit(Tag tag)
 {
     if (ModelState.IsValid)
     {
         db.Tags.Update(tag);
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(tag);
 }