示例#1
0
        public void AddTopic(AddTopicBindingModel model, string userId)
        {
            User currentUser = GetCurrentUser(userId);

            Topic topic = Mapper.Instance.Map <Topic>(model);

            topic.Author      = currentUser;
            topic.PublishDate = DateTime.Now;

            string[] tags = model.Tags.Split(new[] { ",", ";", " " }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string tagString in tags)
            {
                Tag tag = this.Context.Tags.FirstOrDefault(t => t.Name == tagString);
                if (tag == null)
                {
                    tag = new Tag()
                    {
                        Name = tagString
                    };
                    this.Context.Tags.Add(tag);
                    this.Context.SaveChanges();
                }

                topic.Tags.Add(tag);
            }

            this.Context.Topics.Add(topic);
            this.Context.SaveChanges();
        }
示例#2
0
        public AddTopicViewModel GetAddTopicViewModel(AddTopicBindingModel model)
        {
            if (model == null)
            {
                return(null);
            }
            AddTopicViewModel viewModel = Mapper.Instance.Map <AddTopicViewModel>(model);

            return(viewModel);
        }
 private bool ValidateTopic(AddTopicBindingModel atbm)
 {
     if (atbm.Title.Length > 30)
     {
         return(false);
     }
     if (atbm.Content.Length > 5000)
     {
         return(false);
     }
     return(true);
 }
        public void Add_Post_RedirectToAllTopics()
        {
            // the user is always null because we get his Id from the IPrincipal
            AddTopicBindingModel model = new AddTopicBindingModel()
            {
                Content = "Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                Title   = "SomeTak",
                Tags    = "add,tags,here"
            };

            this.controller.WithCallTo(c => c.Add(model))
            .ShouldRedirectTo(c => c.All);
        }
示例#5
0
 public ActionResult Add([Bind(Include = "Title,Content,Tags")] AddTopicBindingModel model)
 {
     if (this.ModelState.IsValid)
     {
         if (this.User == null)
         {
             return(this.RedirectToAction("All"));
         }
         string userId = this.User.Identity.GetUserId();
         this.service.AddTopic(model, userId);
         return(this.RedirectToAction("All"));
     }
     return(this.View(this.service.GetAddTopicViewModel(model)));
 }
        public IActionResult New(HttpSession session, HttpResponse response, AddTopicBindingModel atbm)
        {
            bool isTopicValid = this.ValidateTopic(atbm);

            if (isTopicValid)
            {
                var user = this.data.Logins.FindByPredicate(l => l.SessionId == session.Id).User;

                Topic topic = new Topic()
                {
                    Author      = user,
                    Category    = this.data.Categories.FindByPredicate(c => c.Name == atbm.Category),
                    Content     = atbm.Content,
                    PublishDate = DateTime.Now,
                    Title       = atbm.Title
                };
                this.data.Topics.Insert(topic);
                this.data.SaveChanges();
                this.Redirect(response, "/home/topics");
                return(null);
            }
            this.Redirect(response, "/topics/new");
            return(null);
        }