Пример #1
0
        public Topic CreateTopic(TopicCreationModel model)
        {
            if (!_settings.CanCreateNewTopics())
            {
                throw new FeatureDisabledException();
            }

            var user = _currentUser.DiscussionUser;

            if (_settings.RequireUserPhoneNumberVerified && !user.PhoneNumberId.HasValue)
            {
                throw new UserVerificationRequiredException();
            }

            // ReSharper disable once PossibleInvalidOperationException
            var topic = new Topic
            {
                Title        = model.Title,
                Content      = model.Content,
                Type         = model.Type.Value,
                CreatedBy    = user.Id,
                CreatedAtUtc = _clock.Now.UtcDateTime
            };

            _topicRepo.Save(topic);

            return(topic);
        }
        public void should_create_topic()
        {
            var topicController = _myApp.CreateController <TopicController>();


            var model = new TopicCreationModel()
            {
                Title = "first topic you created", Content = "**This is the content of this markdown**\r\n* markdown content is greate*"
            };

            topicController.CreateTopic(model);


            var repo      = _myApp.GetService <IDataRepository <Topic> >();
            var allTopics = repo.All.ToList();

            var createdTopic = allTopics.Find(topic => topic.Title == model.Title);

            createdTopic.ShouldNotBeNull();
            createdTopic.Title.ShouldEqual(model.Title);
            createdTopic.Content.ShouldEqual(model.Content);

            var createdAt = (DateTime.UtcNow - createdTopic.CreatedAt);

            Assert.True(createdAt.TotalMilliseconds > 0);
            Assert.True(createdAt.TotalMinutes < 2);

            createdTopic.LastRepliedAt.ShouldBeNull();
            createdTopic.ReplyCount.ShouldEqual(0);
            createdTopic.ViewCount.ShouldEqual(0);
        }
Пример #3
0
        public void should_create_topic()
        {
            var user            = _app.MockUser();
            var topicController = _app.CreateController <TopicController>();

            var model = new TopicCreationModel()
            {
                Title   = "first topic you created",
                Content = "**This is the content of this markdown**\r\n* markdown content is greate*",
                Type    = TopicType.Job
            };

            var actionResult = topicController.CreateTopic(model);

            var topicCreated = VerifyTopicCreated(actionResult, model);

            topicCreated.LastRepliedAt.ShouldBeNull();
            topicCreated.ReplyCount.ShouldEqual(0);
            topicCreated.ViewCount.ShouldEqual(0);

            var topicCreatedLog = _app.GetLogs().FirstOrDefault(log => log.Message.Contains("创建话题成功"));

            Assert.NotNull(topicCreatedLog);
            Assert.Contains($"UserId = {user.Id}", topicCreatedLog.Message);
            Assert.Contains($"Id = {topicCreated.Id}", topicCreatedLog.Message);
            Assert.Contains(topicCreated.Title, topicCreatedLog.Message);
        }
Пример #4
0
        public void should_create_topic()
        {
            _app.MockUser();
            var topicController = _app.CreateController <TopicController>();

            var model = new TopicCreationModel()
            {
                Title   = "first topic you created",
                Content = "**This is the content of this markdown**\r\n* markdown content is greate*",
                Type    = TopicType.Job
            };

            topicController.CreateTopic(model);

            var repo      = _app.GetService <IRepository <Topic> >();
            var allTopics = repo.All().ToList();

            var createdTopic = allTopics.Find(topic => topic.Title == model.Title);

            createdTopic.ShouldNotBeNull();
            createdTopic.Title.ShouldEqual(model.Title);
            createdTopic.Content.ShouldEqual(model.Content);
            createdTopic.Type.ShouldEqual(TopicType.Job);
            createdTopic.CreatedBy.ShouldEqual(_app.GetDiscussionUser().Id);

            var createdAt = DateTime.UtcNow - createdTopic.CreatedAtUtc;

            Assert.True(createdAt.TotalMilliseconds >= 0);
            Assert.True(createdAt.TotalMinutes < 2);

            createdTopic.LastRepliedAt.ShouldBeNull();
            createdTopic.ReplyCount.ShouldEqual(0);
            createdTopic.ViewCount.ShouldEqual(0);
        }
Пример #5
0
        public ActionResult CreateTopic(TopicCreationModel model)
        {
            var user     = HttpContext.DiscussionUser();
            var userName = user.UserName;

            if (!ModelState.IsValid)
            {
                _logger.LogModelState("创建话题", ModelState, user.Id, userName);
                return(BadRequest());
            }

            try
            {
                var topic = _topicService.CreateTopic(model);
                _logger.LogInformation("创建话题成功:{@NewTopicAttempt}",
                                       new { topic.Title, topic.Id, UserId = user.Id, user.UserName });
                // ReSharper disable once Mvc.ActionNotResolved
                return(RedirectToAction("Index", new { topic.Id }));
            }
            catch (InvalidOperationException ex)
            {
                _logger.LogWarning("创建话题失败:{@NewTopicAttempt}",
                                   new { UserId = user.Id, user.UserName, Result = ex.Message });
                return(BadRequest());
            }
        }
Пример #6
0
        private ModelStateDictionary ValidateTopic(string title, string content, TopicType?type = TopicType.Discussion)
        {
            var createModel = new TopicCreationModel {
                Title = title, Content = content, Type = type
            };

            return(_app.ValidateModel(createModel));
        }
Пример #7
0
        private TopicController CreateControllerAndValidateTopic(string title, string content)
        {
            var createModel = new TopicCreationModel {
                Title = title, Content = content
            };

            var topicController = _myApp.CreateController <TopicController>();

            topicController.TryValidateModel(createModel);

            return(topicController);
        }
Пример #8
0
        public ActionResult CreateTopic(TopicCreationModel model)
        {
            if (!ModelState.IsValid)
            {
                return(HttpBadRequest());
            }

            var topic = new Topic
            {
                Title     = model.Title,
                Content   = model.Content,
                TopicType = TopicType.Sharing,
                CreatedAt = DateTime.UtcNow
            };

            _topicRepo.Create(topic);
            return(RedirectToAction("Index", new { topic.Id }));
        }
Пример #9
0
        public ActionResult CreateTopic(TopicCreationModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var topic = new Topic
            {
                Title        = model.Title,
                Content      = model.Content,
                Type         = model.Type.Value,
                CreatedBy    = HttpContext.DiscussionUser().Id,
                CreatedAtUtc = DateTime.UtcNow
            };

            _topicRepo.Save(topic);
            return(RedirectToAction("Index", new { topic.Id }));
        }
Пример #10
0
        private Topic VerifyTopicCreated(ActionResult actionResult, TopicCreationModel model)
        {
            Assert.IsType <RedirectToActionResult>(actionResult);

            var repo      = _app.GetService <IRepository <Topic> >();
            var allTopics = repo.All().ToList();

            var createdTopic = allTopics.Find(topic => topic.Title == model.Title);

            createdTopic.ShouldNotBeNull();
            createdTopic.Title.ShouldEqual(model.Title);
            createdTopic.Content.ShouldEqual(model.Content);
            createdTopic.Type.ShouldEqual(TopicType.Job);
            createdTopic.CreatedBy.ShouldEqual(_app.GetDiscussionUser().Id);

            var createdAt = DateTime.UtcNow - createdTopic.CreatedAtUtc;

            Assert.True(createdAt.TotalMilliseconds >= 0);
            Assert.True(createdAt.TotalMinutes < 2);

            return(createdTopic);
        }
Пример #11
0
        public ActionResult CreateTopic(TopicCreationModel model)
        {
            var userName = HttpContext.DiscussionUser().UserName;

            if (!ModelState.IsValid)
            {
                _logger.LogModelState("创建话题", ModelState, userName);
                return(BadRequest());
            }

            try
            {
                var topic = _topicService.CreateTopic(model);
                _logger.LogInformation($"创建话题成功:{userName}:{topic.Title}(id: {topic.Id})");
                return(RedirectToAction("Index", new { topic.Id }));
            }
            catch (UserVerificationRequiredException ex)
            {
                _logger.LogWarning($"创建话题失败:{userName}:{ex.Message}");
                return(BadRequest());
            }
        }