public async Task LikingAndUnlikingCommentTest() { HttpClient client = _testServer.CreateClient(1); CommentLikeDto commentLikeDto = new CommentLikeDto { CommentId = 1 }; HttpResponseMessage createResponse = await client.PostAsync(_apiEndpoint + "api/commentLike/post", commentLikeDto, new JsonMediaTypeFormatter()); Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode); HttpResponseMessage duplicateCreateResponse = await client.PostAsync(_apiEndpoint + "api/commentLike/post", commentLikeDto, new JsonMediaTypeFormatter()); Assert.AreEqual(HttpStatusCode.OK, duplicateCreateResponse.StatusCode); HttpResponseMessage deleteResponse = await client.PostAsync(_apiEndpoint + "api/commentLike/delete", commentLikeDto, new JsonMediaTypeFormatter()); Assert.AreEqual(HttpStatusCode.OK, deleteResponse.StatusCode); HttpResponseMessage duplicateDeleteResponse = await client.PostAsync(_apiEndpoint + "api/commentLike/delete", commentLikeDto, new JsonMediaTypeFormatter()); Assert.AreEqual(HttpStatusCode.NoContent, duplicateDeleteResponse.StatusCode); }
public async Task <IActionResult> DeleteCommentLike([FromBody] CreateCommentLikeDto createCommentLikeDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var commentLike = await context.CommentLikes.SingleOrDefaultAsync(like => like.CommentId == createCommentLikeDto.CommentId && like.UserId == createCommentLikeDto.UserId); if (commentLike == null) { return(NotFound()); } context.CommentLikes.Remove(commentLike); await context.SaveChangesAsync(); var commentLikeDto = new CommentLikeDto { CommentId = commentLike.CommentId, CountLike = await context.CommentLikes .Where(like => like.CommentId == commentLike.CommentId).CountAsync() }; return(Ok(commentLikeDto)); }
public async Task UnlikeInvalidCommentIdTest() { HttpClient client = _testServer.CreateClient(1); CommentLikeDto commentLikeDto = new CommentLikeDto { CommentId = -1 }; HttpResponseMessage duplicateDeleteResponse = await client.PostAsync(_apiEndpoint + "api/postLike/delete", commentLikeDto, new JsonMediaTypeFormatter()); Assert.AreEqual(HttpStatusCode.NotFound, duplicateDeleteResponse.StatusCode); }
public async Task <CommentLikeDto> GetCommentLike(string commentId) { using (var context = new ProductInformationContext()) { var trueNumber = await context.CommentLikes.Where(x => x.CommentId == commentId && x.LikeStatus == true).CountAsync(); var falseNumber = await context.CommentLikes.Where(x => x.CommentId == commentId && x.LikeStatus == false).CountAsync(); var entity = new CommentLikeDto { FalseNumber = falseNumber, TrueNumber = trueNumber }; return(entity); } }
public async Task <IActionResult> GetCommentLike([FromRoute] int commentId) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var commentLikeDto = new CommentLikeDto { CommentId = commentId, CountLike = await context.CommentLikes .Where(like => like.CommentId == commentId).CountAsync() }; return(Ok(commentLikeDto)); }
public ResponseDto SaveLike(CommentLikeDto saveDto) { ResponseDto responseDto = new ResponseDto(); CommentLikeBo saveBo = new CommentLikeBo() { CommentId = saveDto.CommentId, IsLike = saveDto.IsLike, Session = Session }; ResponseBo responseBo = commentBusiness.SaveLike(saveBo); responseDto = responseBo.ToResponseDto(); return(responseDto); }
public IActionResult Delete([FromBody] CommentLikeDto commentLikeDto) { try { ClaimsIdentity identity = HttpContext.User.Identity as ClaimsIdentity; int userId = int.Parse(identity.FindFirst(ClaimTypes.NameIdentifier).Value); _service.DeleteCommentLike(userId, commentLikeDto.CommentId); return(Ok()); } catch (CommentLikeNotFoundException) { return(NoContent()); } catch (CommentNotFoundException) { return(NotFound()); } }
public IActionResult Create([FromBody] CommentLikeDto commentLikeDto) { try { ClaimsIdentity identity = HttpContext.User.Identity as ClaimsIdentity; int userId = int.Parse(identity.FindFirst(ClaimTypes.NameIdentifier).Value); _service.CreateCommentLike(userId, commentLikeDto.CommentId); return(StatusCode(StatusCodes.Status201Created)); } catch (CommentLikeAlreadyExistsException) { return(Ok()); } catch (CommentNotFoundException) { return(NotFound()); } }
public async Task <IActionResult> PostCommentLike([FromBody] CreateCommentLikeDto createCommentLikeDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var newCommentLike = mapper.Map <CreateCommentLikeDto, CommentLike>(createCommentLikeDto); context.CommentLikes.Add(newCommentLike); await context.SaveChangesAsync(); var commentLikeDto = new CommentLikeDto { CommentId = newCommentLike.CommentId, CountLike = await context.CommentLikes .Where(like => like.CommentId == newCommentLike.CommentId).CountAsync() }; return(CreatedAtRoute(new { id = newCommentLike.Id }, commentLikeDto)); }