コード例 #1
0
        public async Task <Post> Create(PostCreate createData, Space space, User user)
        {
            Post p = factory.Create(createData, space, user);
            await repo.Add(p);

            await bus.Dispatch(new PostCreateEvent(p));

            return(p);
        }
コード例 #2
0
        protected override async Task <PostView?> HandleInput(PostCreateParams input)
        {
            using (var connection = database.GetConnection()) {
                ISpaceRepo spaceRepo = database.GetRepo <ISpaceRepo>(connection);
                IPostRepo  postRepo  = database.GetRepo <IPostRepo>(connection);
                IVoteRepo  voteRepo  = database.GetRepo <IVoteRepo>(connection);

                Space?space = await spaceRepo.FindByName(input.Space);

                if (space == null)
                {
                    throw new InvalidOperationException($"No space with name ${input.Space} found.");
                }

                using (var transaction = connection.BeginTransaction()) {
                    Post post = new Post()
                    {
                        Type         = input.Type,
                        Title        = input.Title,
                        Body         = input.Body,
                        User         = input.User,
                        CreationDate = DateTime.UtcNow,
                        Space        = space
                    };

                    if (post.Type == PostType.Link && !System.Text.RegularExpressions.Regex.IsMatch(post.Body, Regex.UrlProtocol))
                    {
                        post.Body = $"http://{post.Body}";
                    }

                    // Not liking these count caches. Makes no sense?
                    post.Upvotes++;
                    await postRepo.Add(post);

                    Vote upvote = new Vote()
                    {
                        User         = input.User,
                        ResourceId   = post.Id,
                        ResourceType = VoteResourceType.Post,
                        Direction    = VoteDirection.Up
                    };

                    await voteRepo.Add(upvote);

                    post.Vote = upvote;

                    transaction.Commit();
                    return(postMapper.Map(post));
                }
            }
        }
コード例 #3
0
        public async Task <IActionResult> AddPost(AddPostDto addPostDto)
        {
            List <PostTag> postTags = await _tagRepo.GetTagsAndUpdateAmmountAdding(addPostDto.Description);

            Post post = new Post
            {
                UserId      = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value),
                Description = addPostDto.Description,
                CreateAt    = DateTime.Now,
                Photos      = addPostDto.Photos,
                PostTags    = postTags
            };

            _repo.Add(post);

            await _repo.SaveAll();

            return(StatusCode(201));
        }
コード例 #4
0
        public ActionResult AddPlus(Post post)
        {
            var currentlyLoggedUserID = _user.GetIDOfCurrentlyLoggedUser().Value;
            var postToUpvote          = _post.GetPostByID(post.Post_Id);

            bool checkPostState = true;
            var  postIsUpvoted  = _post.GetUpvPost(postToUpvote.Post_Id, currentlyLoggedUserID);

            //prevent user from double voting
            if (postToUpvote != null)
            {
                if (postIsUpvoted != null)
                {
                    checkPostState = postIsUpvoted.IsPostUpvoted;
                }
                else
                {
                    var postIsUpvtd = new IsPostUpvd()
                    {
                        User_Id       = currentlyLoggedUserID,
                        Post_Id       = postToUpvote.Post_Id,
                        IsPostUpvoted = false
                    };
                    _post.Add(postIsUpvtd);
                    _post.SaveChanges();
                    checkPostState = postIsUpvtd.IsPostUpvoted;
                }
            }

            if (postToUpvote != null && User.Identity.Name.ToLower() != postToUpvote.User.NickName.ToLower() && !checkPostState)
            {
                postToUpvote.Votes          = postToUpvote.Votes + 1;
                postIsUpvoted               = _post.GetUpvPost(postToUpvote.Post_Id, currentlyLoggedUserID);
                postIsUpvoted.IsPostUpvoted = true;

                _post.UpdateOnlyVotes(postToUpvote);
                _post.UpdateIsVotedState(postIsUpvoted);
                _post.SaveChanges();

                var result = new { result = true, votes = postToUpvote.Votes };
                return(Json(result,
                            JsonRequestBehavior.AllowGet));
            }


            else if (postToUpvote != null && User.Identity.Name.ToLower() != postToUpvote.User.NickName.ToLower() && checkPostState)
            {
                postToUpvote.Votes          = postToUpvote.Votes - 1;
                postIsUpvoted               = _post.GetUpvPost(postToUpvote.Post_Id, currentlyLoggedUserID);
                postIsUpvoted.IsPostUpvoted = false;
                _post.UpdateOnlyVotes(postToUpvote);
                _post.UpdateIsVotedState(postIsUpvoted);
                _post.SaveChanges();
                var result = new { result = false, votes = postToUpvote.Votes };
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            else
            {
                var result = new { result = false, votes = postToUpvote.Votes };
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
        }