Exemplo n.º 1
0
        public async Task <IActionResult> Create(PostAnonymousCreateViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var thread = await _threadService.GetAsync(viewModel.ThreadId);

                if (!thread.IsClosed)
                {
                    var postDto = _mapper.Map <PostDto>(viewModel);
                    postDto.UserIpAddress = UserIpAddress.ToString();
                    postDto.UserAgent     = UserAgent;

                    var postId = await _postService.CreateAsync(viewModel.Attachments, postDto);

                    return
                        (Redirect(Url.Action("Details", "Threads",
                                             new
                    {
                        categoryAlias = viewModel.CategoryAlias,
                        threadId = viewModel.ThreadId
                    }) + "#" + postId));
                }
                else
                {
                    throw new HttpResponseException(HttpStatusCode.Forbidden, $"Thread {thread.Id} is closed.");
                }
            }
            else
            {
                ViewBag.ErrorMessage = ModelState.ModelErrorsToString();
                return(View(viewModel));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(ThreadAnonymousCreateViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var categoryDto = await _categoryService.GetAsync(viewModel.CategoryAlias);

                    var threadDto = _mapper.Map <ThreadDto>(viewModel);
                    threadDto.BumpLimit = categoryDto.DefaultBumpLimit;
                    threadDto.ShowThreadLocalUserHash = categoryDto.DefaultShowThreadLocalUserHash;
                    threadDto.CategoryId = categoryDto.Id;

                    var postDto = _mapper.Map <PostDto>(viewModel);
                    postDto.UserIpAddress = UserIpAddress.ToString();
                    postDto.UserAgent     = UserAgent;

                    var threadCreateDto = new ThreadPostCreateDto
                    {
                        Category = categoryDto,
                        Thread   = threadDto,
                        Post     = postDto,
                    };

                    var createResultDto = await _threadService.CreateThreadPostAsync(viewModel.Attachments, threadCreateDto, true);

                    return(RedirectToAction("Details", "Threads", new { categoryAlias = viewModel.CategoryAlias, threadId = createResultDto.ThreadId }));
                }
                catch (Exception ex)
                {
                    _logger.LogError(EventIdentifiers.ThreadCreateError.ToEventId(), ex, $"Can't create new thread due to exception");

                    ViewBag.ErrorMessage = "Can't create new thread";
                    return(View(viewModel));
                }
            }
            else
            {
                ViewBag.ErrorMessage = ModelState.ModelErrorsToString();
                return(View(viewModel));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create(ThreadAnonymousCreateViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var category = await _categoryService.GetAsync(viewModel.CategoryAlias);

                var threadDto = _mapper.Map <ThreadDto>(viewModel);
                threadDto.BumpLimit = category.DefaultBumpLimit;
                threadDto.ShowThreadLocalUserHash = category.DefaultShowThreadLocalUserHash;
                threadDto.CategoryId = category.Id;

                var threadId = await _threadService.CreateAsync(threadDto);

                var postDto = _mapper.Map <PostDto>(viewModel);
                postDto.ThreadId      = threadId;
                postDto.UserIpAddress = UserIpAddress.ToString();
                postDto.UserAgent     = UserAgent;

                try
                {
                    var postId = await _postService.CreateAsync(viewModel.Attachments, postDto);
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Can't create new post due to exception: {ex}. Thread creation failed.");
                    await _threadService.DeleteAsync(threadId);

                    throw;
                }

                return(RedirectToAction("Details", "Threads", new { categoryAlias = viewModel.CategoryAlias, threadId = threadId }));
            }
            else
            {
                ViewBag.ErrorMessage = ModelState.ModelErrorsToString();
                return(View(viewModel));
            }
        }