public ActionResult Edit(int id)
        {
            Tweet           tweet     = this.tweetsRepo.GetById(id);
            InputTweetModel editModel = new InputTweetModel()
            {
                Content = tweet.Content,
                Tags    = "#" + string.Join(" #", tweet.Tags.Select(t => t.Name))
            };

            return(View(editModel));
        }
示例#2
0
        public ActionResult AddTweet(InputTweetModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.RedirectToAction("Index", "Home"));
            }

            this.tweets.CreateTweet(model.Content, this.User.Identity.GetUserId());

            return(this.RedirectToAction("Index", "Home"));
        }
        public ActionResult AddTweet(InputTweetModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.RedirectToAction("Index", "Home");
            }

            this.tweets.CreateTweet(model.Content, this.User.Identity.GetUserId());

            return this.RedirectToAction("Index", "Home");
        }
        public ActionResult Create(InputTweetModel model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                model.UserId = this.UserProfile.Id;
                var tweet = Mapper.Map<Tweet>(model);
                tweet.DatePosted = DateTime.Now;
                this.Data.Tweets.Add(tweet);
                this.Data.SaveChanges();
            }

            return RedirectToAction("Index", "Home");
        }
        public ActionResult Create(InputTweetModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            Tweet tweet = new Tweet()
            {
                Content    = inputModel.Content,
                DatePosted = DateTime.Now,
                UserId     = this.User.Identity.GetUserId(),
                Tags       = new HashSet <Tag>()
            };

            IList <Tag>    dbTags = this.tagsRepo.All().ToList();
            IList <string> tags   = inputModel.Tags.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();

            foreach (var item in tags)
            {
                // Constants are some object class from ASP .NET
                if (!Regex.IsMatch(item, Common.Constants.Pattern, RegexOptions.IgnoreCase))
                {
                    continue;
                }

                string hastag = item.Substring(1);
                // This does not makes query to database because I called .ToList() above ↑.
                // If you want turn on a SQL Profiler.
                Tag tag = dbTags.FirstOrDefault(t => t.Name == hastag);
                if (tag == null)
                {
                    tag = new Tag {
                        Name = hastag
                    };
                    this.tagsRepo.Add(tag);
                    this.tagsRepo.SaveChanges();
                }

                tweet.Tags.Add(tag);
            }

            this.tweetsRepo.Add(tweet);
            this.tweetsRepo.SaveChanges();
            return(this.RedirectToAction("Details", new { id = tweet.Id }));
        }
        public ActionResult Edit(int id, InputTweetModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            Tweet dbTweet = this.tweetsRepo.GetById(id);

            if (dbTweet == null)
            {
                return(this.RedirectToAction("Index"));
            }

            dbTweet.Content = inputModel.Content;

            this.tweetsRepo.Update(dbTweet);
            this.tweetsRepo.SaveChanges();

            return(this.RedirectToAction("Details", new { id = dbTweet.Id }));
        }