Esempio n. 1
0
        public ActionResult <PostConfirmationDto> CreatePost([FromBody] PostCreationDto post, [FromHeader] string key)
        {
            if (!authService.Authorize(key))
            {
                return(StatusCode(StatusCodes.Status401Unauthorized, "The user is not authorized!"));
            }
            if (userMockRepository.GetAccountByUserAccountID(post.UserId) == null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "User with that id does not exist!"));
            }
            if (contentRepository.GetContentById(post.ContentId) == null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "Content with that id does not exist!"));
            }


            try
            {
                Post postEntity = mapper.Map <Post>(post);
                postEntity.DateOfPublication = DateTime.Now;
                postEntity.ExpiryDate        = postEntity.DateOfPublication.AddMonths(1);
                postRepository.CreatePost(postEntity);
                postRepository.SaveChanges();
                string location = linkGenerator.GetPathByAction("GetPostById", "Post", new { postId = postEntity.PostId });
                fakeLoggerRepository.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", "Post created", null);

                return(Created(location, mapper.Map <PostConfirmationDto>(postEntity)));
            }
            catch (Exception ex)
            {
                fakeLoggerRepository.Log(LogLevel.Error, contextAccessor.HttpContext.TraceIdentifier, "", "Error while adding post", null);

                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Esempio n. 2
0
        public async Task <PostDto> CreatePost(ThreadDto threadDto, PostCreationDto postDto)
        {
            throwHelper.ThrowIfNull(threadDto, nameof(threadDto));
            throwHelper.ThrowIfNull(postDto, nameof(postDto));

            var thread = mapper.Map <Thread>(threadDto);

            if (thread.Id != 0 || context.Threads.Local.Any(t => t.Id == thread.Id))
            {
                context.Attach(thread);
            }

            var post = new Post
            {
                CreationTime = DateTime.UtcNow,
                Message      = postDto.Message,
                Name         = postDto.AuthorName,
                Thread       = thread,
                UserId       = postDto.AuthorId
            };

            if (postDto.Attachment != null)
            {
                post.Image = await CreateImageAsync(postDto.Attachment);
            }

            context.Posts.Add(post);
            await context.SaveChangesAsync();

            return(mapper.Map <PostDto>(post));
        }
        public PostDto CreatePost(PostCreationDto post)
        {
            if (userMockRepository.GetUserByID(post.UserId) == null)
            {
                throw new _404Exception("User not found!");
            }
            if (!typeRepository.ContainsType(post.Type))
            {
                throw new _404Exception("Type not found!");
            }

            var newPost = autoMapper.Map <Post>(post);

            newPost.PostPublishingDateTime = DateTime.Now;
            newPost.LastModified           = DateTime.Now;
            newPost.PostTypeId             = typeRepository.GetIdByType(post.Type);
            try
            {
                var addedPost = postRepository.CreatePost(newPost);
                return(autoMapper.Map <PostDto>(addedPost));
            } catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Esempio n. 4
0
        public Post Create(int userId, PostCreationDto postDto)
        {
            Post post = new Post {
                authorId = userId,
                content  = postDto.content,
                created  = DateTime.Now
            };

            return(post);
        }
Esempio n. 5
0
        public CreationResultDto <long> CreatePost(PostCreationDto newPost, long userId)
        {
            if (newPost == null)
            {
                throw new PostException(PostError.RequestIsEmpty);
            }
            if (string.IsNullOrWhiteSpace(newPost.Title))
            {
                throw new PostException(PostError.TitleIsNullOrWhitespace);
            }
            if (newPost.Title.Length > PostRestrictions.TitleMaxLength)
            {
                throw new PostException(PostError.TitleMaxLengthExceeded);
            }

            var utcNow = DateTime.UtcNow;
            var user   = _dbContext.Users.FirstOrDefault(x => x.UserId == userId);

            if (user == null)
            {
                throw new UserException(UserError.DoesNotExist);
            }

            var sections = newPost.Sections?.Select(x => new Section
            {
                Type         = (SectionType)x.SectionType,
                Content      = x.Content,
                CreationDate = utcNow
            }).ToList();
            var post = new Post
            {
                Title        = newPost.Title,
                CreationDate = DateTime.UtcNow,
                Author       = user,
                Sections     = sections,
                HeaderImage  = newPost.HeaderImage
            };

            _dbContext.Posts.Add(post);

            _dbContext.SaveChanges();

            return(new CreationResultDto <long>
            {
                Id = post.PostId
            });
        }
Esempio n. 6
0
 public ActionResult <PostDto> CreateNewPost([FromHeader] string secretToken, [FromBody] PostCreationDto newPost)
 {
     if (authorizationMockService.AuthorizeToken(secretToken))
     {
         try
         {
             var toBeCreated = postService.CreatePost(newPost);
             loggerRepository.LogInformation("New post successfuly created!");
             return(StatusCode(201, toBeCreated));
         }
         catch (Exception ex)
         {
             if (ex.Message == "User not found!" || ex.Message == "Type not found!")
             {
                 return(StatusCode(404, ex.Message));
             }
             return(StatusCode(500, ex.Message));
         }
     }
     return(StatusCode(StatusCodes.Status401Unauthorized, "Authorization failed!"));
 }
Esempio n. 7
0
 public CreationResultDto <long> CreatePost(PostCreationDto newPost)
 {
     return(_postBusiness.CreatePost(newPost, UserId));
 }
Esempio n. 8
0
        public async Task <PostDto> AddPostToThreadAsync(string boardId, int threadId, PostCreationDto data)
        {
            throwHelper.ThrowIfNull(boardId, nameof(boardId));
            throwHelper.ThrowIfNull(data, nameof(data));

            var thread = await context.Threads.AsNoTracking().FirstOrDefaultAsync(t => t.BoardId == boardId && t.Id == threadId);

            if (thread == null)
            {
                throw new KeyNotFoundException($"Thread '{threadId}' does not exist on board {boardId}.");
            }

            return(await posts.CreatePost(new ThreadDto { ThreadId = threadId }, data));
        }