예제 #1
0
        public async Task <IActionResult> Create([FromBody] Post post)
        {
            if (post == null)
            {
                return(BadRequest());
            }
            if (post.ThreadId == null)
            {
                return(BadRequest());
            }

            var thread = _threadService.FindById(post.ThreadId);

            if (thread.ApprovalStatus != ApprovalStatus.Approved)
            {
                return(BadRequest());
            }
            if (thread.Locked)
            {
                return(BadRequest());
            }

            thread.LastActivity = DateTime.Now;

            post.CreatedById = CurrentUserId;
            var createdBy = _userService.FindById(CurrentUserId);

            if (createdBy.Role == Role.Administrator || createdBy.Role == Role.Moderator)
            {
                _postService.SetApprovalStatus(CurrentUserId, post, ApprovalStatus.Approved);
                thread.NumberOfPosts += 1;
            }

            await _postService.AddAsync(post);

            await _unitOfWork.SaveChangesAsync();

            post = _postService.FindWithCreatedBy(post.Id);
            var dto = _mapper.Map <PostDto>(post);

            return(StatusCode(StatusCodes.Status201Created, dto));
        }