Exemplo n.º 1
0
        public async Task LikingAndUnlikingPostTest()
        {
            HttpClient client = _testServer.CreateClient(2);

            PostLikeDto postLikeDto = new PostLikeDto
            {
                PostId = 1
            };

            HttpResponseMessage createResponse = await client.PostAsync(_apiEndpoint + "api/postLike/post", postLikeDto, new JsonMediaTypeFormatter());

            Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode);

            HttpResponseMessage duplicateCreateResponse = await client.PostAsync(_apiEndpoint + "api/postLike/post", postLikeDto, new JsonMediaTypeFormatter());

            Assert.AreEqual(HttpStatusCode.OK, duplicateCreateResponse.StatusCode);

            HttpResponseMessage deleteResponse = await client.PostAsync(_apiEndpoint + "api/postLike/delete", postLikeDto, new JsonMediaTypeFormatter());

            Assert.AreEqual(HttpStatusCode.OK, deleteResponse.StatusCode);

            HttpResponseMessage duplicateDeleteResponse = await client.PostAsync(_apiEndpoint + "api/postLike/delete", postLikeDto, new JsonMediaTypeFormatter());

            Assert.AreEqual(HttpStatusCode.NoContent, duplicateDeleteResponse.StatusCode);
        }
Exemplo n.º 2
0
        /// <summary>
        /// PostLikeandDisLike 傳入Po文Id和按讚數
        /// </summary>
        /// <param name="entity"></param>
        public void PostLikeAndDisLike(PostLikeDto entity)
        {
            //  var dbContext = new MyDBContext();
            // using (var transaction = dbContext.Database.BeginTransaction())
            //{


            try
            {
                var mRepo = _posts.GetFirst(x => x.PostId == entity.PostId);
                var post  = _history.GetFirst(x => x.PostId == entity.PostId && x.UserId == entity.UserId);
                if (post != null && mRepo != null)
                {
                    //將user按讚狀態變更
                    mRepo.LikeNumber    = mRepo.LikeNumber + entity.LikeNumber;
                    mRepo.DisLikeNumber = mRepo.DisLikeNumber + entity.DisLikeNumber;
                    _posts.Update(mRepo);
                    _posts.SaveContext();

                    //處理總按讚數
                    post.IsDisLike = entity.IsDisLike;
                    post.IsLike    = entity.IsLike;

                    _history.Update(post);
                    _history.SaveContext();
                    //transaction.Commit();
                }
                else if (mRepo != null)
                {
                    LikeAndDislikeHistory history = new LikeAndDislikeHistory()
                    {
                        Id        = Guid.NewGuid(),
                        PostId    = entity.PostId,
                        UserId    = entity.UserId,
                        IsDisLike = entity.IsDisLike,
                        IsLike    = entity.IsLike
                    };
                    _history.Create(history);
                    _history.SaveContext();

                    mRepo.LikeNumber    = mRepo.LikeNumber + entity.LikeNumber;
                    mRepo.DisLikeNumber = mRepo.DisLikeNumber + entity.DisLikeNumber;

                    _posts.Update(mRepo);
                    _posts.SaveContext();
                    //transaction.Commit();
                }
                else
                {
                    //HttpStatusCode()
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                //transaction.Rollback();
            }
            // }
        }
Exemplo n.º 3
0
        public async Task UnlikeInvalidPostIdTest()
        {
            HttpClient client = _testServer.CreateClient(2);

            PostLikeDto postLikeDto = new PostLikeDto
            {
                PostId = -1
            };

            HttpResponseMessage duplicateDeleteResponse = await client.PostAsync(_apiEndpoint + "api/postLike/delete", postLikeDto, new JsonMediaTypeFormatter());

            Assert.AreEqual(HttpStatusCode.NotFound, duplicateDeleteResponse.StatusCode);
        }
        public ApiResult <PostLikeDto> PostLikeAndDisLike([FromBody] PostLikeDto Dto)
        {
            var result = new ApiResult <PostLikeDto>();

            if (ModelState.IsValid)
            {
                //  var service = new LikeService();
                _plikeService.PostLikeAndDisLike(Dto);
                return(result);
            }
            else
            {
                return(new ApiResult <PostLikeDto>("Dto"));
            }
        }
Exemplo n.º 5
0
        public IActionResult Delete([FromBody] PostLikeDto postLikeDto)
        {
            try
            {
                ClaimsIdentity identity = HttpContext.User.Identity as ClaimsIdentity;
                int            userId   = int.Parse(identity.FindFirst(ClaimTypes.NameIdentifier).Value);

                _service.DeletePostLike(userId, postLikeDto.PostId);

                return(Ok());
            }
            catch (PostLikeNotFoundException)
            {
                return(NoContent());
            }
            catch (PostNotFoundException)
            {
                return(NotFound());
            }
        }
Exemplo n.º 6
0
        public IActionResult Create([FromBody] PostLikeDto postLikeDto)
        {
            try
            {
                ClaimsIdentity identity = HttpContext.User.Identity as ClaimsIdentity;
                int            userId   = int.Parse(identity.FindFirst(ClaimTypes.NameIdentifier).Value);

                _service.CreatePostLike(userId, postLikeDto.PostId);

                return(StatusCode(StatusCodes.Status201Created));
            }
            catch (PostLikeAlreadyExistsException)
            {
                return(Ok());
            }
            catch (PostNotFoundException)
            {
                return(NotFound());
            }
        }