Skip to content

Commit

Permalink
Merge pull request #13 from TeamUStory/feat/like-count
Browse files Browse the repository at this point in the history
feat: 좋아요 카운트 코드 구현
  • Loading branch information
yungic authored Jul 14, 2024
2 parents 230510d + 7b95980 commit 9f2fa09
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/main/java/com/elice/ustory/domain/like/LikeController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.elice.ustory.domain.like;

import com.elice.ustory.domain.like.dto.LikeCountResponse;
import com.elice.ustory.domain.like.dto.LikeListResponse;
import com.elice.ustory.domain.like.dto.LikeResponse;
import com.elice.ustory.domain.paper.entity.Paper;
Expand All @@ -9,6 +10,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
Expand Down Expand Up @@ -97,6 +99,22 @@ public ResponseEntity<LikeResponse> isPaperLiked(@PathVariable Long paperId,
return ResponseEntity.ok(new LikeResponse(isLiked));
}

@Operation(summary = "Like Count API",
description = "해당 페이퍼에서 좋아요의 총 개수를 반환한다. <br>" +
"countLike로 개수를 알 수 있다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Ok", content = @Content(mediaType = "application/json", schema = @Schema(implementation = LikeCountResponse.class))),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "500", description = "Internal Server Error", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping("/{paperId}/count")
public ResponseEntity<LikeCountResponse> countLiked(@PathVariable Long paperId) {

int count = likeService.countLikedById(paperId);

return ResponseEntity.ok(new LikeCountResponse(count));
}

@Operation(summary = "Delete Like API", description = "좋아요를 해제한다.")
@ApiResponses({
@ApiResponse(responseCode = "204", description = "No Content", content = @Content(mediaType = "application/json")),
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/elice/ustory/domain/like/LikeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.elice.ustory.domain.user.entity.Users;
import com.elice.ustory.domain.user.repository.UserRepository;
import com.elice.ustory.global.exception.model.ConflictException;
import com.elice.ustory.global.exception.model.InternalServerException;
import com.elice.ustory.global.exception.model.NotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -64,4 +65,14 @@ public void deleteLike(Long userId, Long paperId) {

likeRepository.delete(like);
}

/** 좋아요 총 개수 반환 메서드 **/
public int countLikedById(Long paperId) {

// 먼저 해당 페이퍼가 있는지 없는지 체크한 뒤, 페이퍼가 없다면 에러 반환
Paper paper = paperRepository.findById(paperId)
.orElseThrow(() -> new NotFoundException(String.format(NOT_FOUND_PAPER_MESSAGE, paperId)));

return likeRepository.countLikeById(paperId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.elice.ustory.domain.like.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class LikeCountResponse {

@Schema(description = "서비스 내에서 해당 페이퍼에 좋아요 총 개수", example = "정수 타입으로, 0 부터 시작")
private int countLike;

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface LikeQueryDslRepository {

/** userId에 해당하는 Bookmark 들의 Paper List 가져오기 */
List<Paper> findLikesByUserId(Long userId, Pageable pageable);

Integer countLikeById(Long paperId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ public List<Paper> findLikesByUserId(Long userId, Pageable pageable) {
.limit(pageable.getPageSize())
.fetch();
}

@Override
public Integer countLikeById(Long paperId) {
return queryFactory.select(like.count().intValue())
.from(like)
.where(like.paper.id.eq(paperId))
.fetchOne();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public ResponseEntity<List<PaperMapListResponse>> getPapersByUserForMap(@JwtAuth
return ResponseEntity.ok(response);
}

@Operation(summary = "Count Write Paper By Specific User API", description = "특정 유저가 작성한 모든 페이퍼의 갯수를 불러온다.")
@Operation(summary = "Count Write Paper By Specific User API", description = "특정 유저가 작성한 모든 페이퍼의 개수를 불러온다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Ok", content = @Content(mediaType = "application/json", schema = @Schema(implementation = PaperCountResponse.class))),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))),
Expand Down

0 comments on commit 9f2fa09

Please sign in to comment.