Exemplo n.º 1
0
        public async Task <int> Handle(CreatePostCommand request, CancellationToken cancellationToken)
        {
            //var entity = _mapper.Map<Domain.Entities.Post>(request);
            //_context.Post.Add(entity);
            //!! must be refactore
            var tags = new List <Domain.Entities.Tags>();

            foreach (var item in request.Tags)
            {
                tags.Add(new Domain.Entities.Tags()
                {
                    Tag = item
                });
            }
            var post = new Domain.Entities.Post()
            {
                Title       = request.Title,
                Description = request.Description,
                UserId      = _currentUserService.UserId,
                Tags        = tags
            };

            _context.Post.Add(post);
            await _context.SaveChangesAsync(cancellationToken);

            return(post.Id);
        }
        public async Task <string> Handle(CreatePostWithFileCommand request, CancellationToken cancellationToken)
        {
            Domain.Entities.Post post =
                await _postService.CreatePostWithFileAsync(request.UserId, request.CreatePostWithFileDto);

            if (!PostHelpers.IsContentFile(request.CreatePostWithFileDto.Content))
            {
                throw new HttpStatusCodeException(HttpStatusCode.BadRequest, "Post creation failed");
            }

            FileData data = BaseHelpers.GetContentFileData <Domain.Entities.Post>(
                request.CreatePostWithFileDto.Content, post.Id
                );

            if (post.ContentType.Equals(data.FileName.GetContentType()))
            {
                await _blobService.UploadImageBlobAsync(data.Content, data.FileName);

                request.CreatePostWithFileDto.Content     = data.FileName;
                request.CreatePostWithFileDto.ContentType = data.FileName.GetContentType();

                await _postService.UpdatePostAsync(post, new UpdatePostDto
                {
                    Content = request.CreatePostWithFileDto.Content,
                    Title   = post.Title,
                });
            }
            else
            {
                throw new HttpStatusCodeException(HttpStatusCode.BadRequest,
                                                  "Can not update content type of a post");
            }

            return(post.Id);
        }
Exemplo n.º 3
0
 protected void SubmitForm(object sender, EventArgs e)
 {
     var post = new Domain.Entities.Post
     {
         Title = TitleTb.Text,
         ShortDescription = ShortDescription.Text,
         Tags = postService.GetTags(Tags.Text).ToList(),
         Chapters = new List<Chapter>
         {
             new Chapter()
             {
                 Title = Chapter1Title.Text,
                 Content = Chapter2Content.Text,
                 OrderNumber = 1
             },
             new Chapter()
             {
                 Title = Chapter2Title.Text,
                 Content = Chapter2Content.Text,
                 OrderNumber = 2
             }
         }
     };
     post.IsPublished = true;
     post.LastUpdateDate = DateTime.Now;
     postService.Create(post);
     Response.Redirect("~/Views/Post/Show?id="+post.Id);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new instance of the <see cref="CreatePostCommand"/> class.
        /// </summary>
        /// <param name="options">The document db options</param>
        /// <param name="post">The post to create</param>
        /// <exception cref="System.ArgumentNullException">The post can't be null</exception>
        public CreatePostCommand(DocumentDbOptions options, Domain.Entities.Post post)
            : base(options)
        {
            if (post == null)
            {
                throw new ArgumentNullException("post");
            }

            this.post = post;
        }
Exemplo n.º 5
0
        public IActionResult UpdatePost(Guid id, Guid topicId, Guid userId, string content)
        {
            var userType = getByIdUserUseCase.GetById(userId);
            var topic    = getByIdTopicUseCase.GetById(topicId);
            var postId   = getByIdPostUseCase.GetById(id);

            if (postId == null && topic == null && userType == null || userType.UserType != 1)
            {
                return(BadRequest("Something went wrong"));
            }

            var post = new Domain.Entities.Post(postId.Id, postId.Title, content, topic, postId.CreatedAt);

            updatePostUseCase.Update(post);
            return(new OkObjectResult(post));
        }
Exemplo n.º 6
0
        public async Task <TResponse> Handle(IPostRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
        {
            Domain.Entities.Post post = null;
            try
            {
                post = await _blogContext.Posts
                       .Find(d => d.Id == request.PostId)
                       .FirstOrDefaultAsync(cancellationToken);
            }
            catch (FormatException)
            {
                ThrowEntityDoesNotExistsException(request);
            }
            if (post == null)
            {
                ThrowEntityDoesNotExistsException(request);
            }

            var response = await next();

            return(response);
        }
Exemplo n.º 7
0
        public IActionResult CreatePost(string title, string content, Guid topicId, Guid userId)
        {
            var topic = getByIdTopicUseCase.GetById(topicId);

            if (topic == null && getByIdUserUseCase.GetById(userId) == null)
            {
                return(BadRequest());
            }
            var    user   = getByIdUserUseCase.GetById(userId);
            string author = user.Name;

            var post = new Domain.Entities.Post(title, content, topic, user);

            var validationResult = new PostValidator().Validate(post);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            addPostUseCase.Add(post);
            return(new OkObjectResult(post));
        }
Exemplo n.º 8
0
        public async Task <PostDto> Handle(CreatePostCommand request, CancellationToken cancellationToken)
        {
            var incomingPostDto = request.Post;

            var post = new Domain.Entities.Post(incomingPostDto.Title,
                                                incomingPostDto.Author,
                                                incomingPostDto.Body,
                                                incomingPostDto.Lead);

            await _blogContext.Posts.InsertOneAsync(post, cancellationToken : cancellationToken);

            var createdPost = await _blogContext.Posts
                              .Find(d => d.Id == post.Id)
                              .FirstOrDefaultAsync(CancellationToken.None);

            var postRatings = await _blogContext.PostRatings.Find(d => d.PostId == createdPost.Id)
                              .ToListAsync(cancellationToken);

            createdPost.Ratings = postRatings;

            var postDto = _mapper.Map <Domain.Entities.Post, PostDto>(createdPost);

            return(postDto);
        }
 /// <summary>
 /// Adds a post in the collection of posts
 /// </summary>
 /// <param name="post">The post to add</param>
 public void AddPost(Domain.Entities.Post post)
 {
     this.postsList.Add(new PostSummaryViewModel(post));
 }
Exemplo n.º 10
0
 public int Update(Domain.Entities.Post post)
 {
     return(postWriteOnlyUseCase.Update(post));
 }
Exemplo n.º 11
0
 public DeletePostCommand(Domain.Entities.Post post)
 {
     Post = post;
 }
Exemplo n.º 12
0
 public int Remove(Domain.Entities.Post post)
 {
     return(postWriteOnlyUseCase.Remove(post));
 }
Exemplo n.º 13
0
 public int Add(Domain.Entities.Post post)
 {
     return(postWriteOnlyUseCase.Add(post));
 }
Exemplo n.º 14
0
 public UpdatePostCommand(Domain.Entities.Post post, UpdatePostDto updatePostDto)
 {
     Post          = post;
     UpdatePostDto = updatePostDto;
 }