Exemple #1
0
        public async Task <IActionResult> Create([Bind("Name,Description,Id")] Topic topic)
        {
            if (ModelState.IsValid)
            {
                await topicService.CreateAsync(topic);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(topic));
        }
        /// <summary>
        /// Asks the service to asynchronously create a new topic, using the supplied information.
        /// </summary>
        /// <param name="data">
        /// The supplied POST-data used to create a new topic.
        /// </param>
        /// <returns>
        /// HTTP Status Code 201 - Created + Unique link to newly created topic,
        ///     HTTP Status Code 400 - Bad Request if no ID was returned, or the supplied POST-data failed validation. Also contains information on failed validation cases.
        ///     HTTP Status Code 409 - Conflict if a topic with a provided unique value already exists,
        ///     HTTP Status Code 500 - Internal Server Error if the other codes don't apply. Contains exception on DEBUG.
        /// </returns>
        public async Task <IHttpActionResult> Post(Topic data)
        {
            var result = await _topicService.CreateAsync(data);

            switch (result.ActionStatus.Status)
            {
            case ActionStatusEnum.Success:
                return(CreateHttpActionResult("TopicAsync", result.ActionStatus.Id));

            case ActionStatusEnum.ValidationError:
                return(ApiControllerExtension.BadRequest(this, result.BrokenValidationRules, data.GetType().Name));

            case ActionStatusEnum.Conflict:
                return(Conflict());
            }

            return(HandleErrorActionResult(result));
        }
Exemple #3
0
        public async Task <ActionResult> CreateTopic(PostPageModel model)
        {
            var site = await _contextService.CurrentSiteAsync();

            var user = await _contextService.CurrentUserAsync();

            var permissions = await _permissionModelBuilder.BuildPermissionModelsByForumId(site.Id, model.Forum.Id);

            var canPost = _securityService.HasPermission(PermissionType.Start, permissions) && !user.IsSuspended;

            if (!canPost)
            {
                _logger.LogWarning("Unauthorized access to create topic", new
                {
                    SiteId  = site.Id,
                    ForumId = model.Forum?.Id,
                    User    = User.Identity.Name
                });

                return(Unauthorized());
            }

            var command = new CreateTopic
            {
                ForumId = model.Forum.Id,
                Title   = model.Topic.Title,
                Content = model.Topic.Content,
                Status  = PostStatusType.Published,
                SiteId  = site.Id,
                UserId  = user.Id
            };

            var slug = await _topicService.CreateAsync(command);

            return(Ok(slug));
        }