Пример #1
0
        public async Task <IActionResult> CreateThread(ThreadCreateDto threadCreateDto)
        {
            try
            {
                // get current logged in user
                var user = await _userManager.GetUserAsync(HttpContext.User);

                var threadToAdd = _mapper.Map <Thread>(threadCreateDto);

                // set thread creator to current logged in user
                threadToAdd.ApplicationUser = user;

                await _threadService.AddThreadToDatabase(threadToAdd);

                // create opening post
                var openingPostCreateDto = new PostCreateDto
                {
                    Content    = threadCreateDto.OpeningPost,
                    TimePosted = threadCreateDto.TimeCreated
                };

                var openingPost = _mapper.Map <Post>(openingPostCreateDto);

                openingPost.Thread = threadToAdd;

                await _postService.AddPostToDatabase(openingPost, user);

                return(GetAllThreadsByMostRecentPost());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Пример #2
0
        public async Task <IActionResult> CreatePost(PostCreateDto postCreateDto)
        {
            var user = await _userManager.GetUserAsync(HttpContext.User);

            var thread = await _threadService.GetThreadById(postCreateDto.ThreadId);

            var postToCreate = _mapper.Map <Post>(postCreateDto);

            postToCreate.Thread = thread;

            try
            {
                await _postService.AddPostToDatabase(postToCreate, user);

                var threadReadDto = _mapper.Map <ThreadReadDto>(thread);
                return(Ok(threadReadDto));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }