Skip to content

Commit

Permalink
[#91] 게시물에 대한 좋아요 개수를 PostResponse에 추가 (#90)
Browse files Browse the repository at this point in the history
* add: 게시물 좋아요 개수 증가/감소 기능

- JPQL UPDATE 사용을 위해 @Modifying

* fix: 게시물 Response 좋아요 개수

* fix: log 설정 변경

* update: 좋아요 생성 반환 값 변경

- 좋아요 생성 리턴 LikeId -> void 로 수정
- @ResponseStatus 이용하여 void로 리턴

* update: 좋아요 생성 Rest docs 상태 코드 변경
  • Loading branch information
hikarigin99 authored Jan 27, 2023
1 parent 0d3306c commit 45b0a6e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.prgrms.prolog.domain.like.api;

import java.net.URI;

import javax.validation.Valid;

import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import com.prgrms.prolog.domain.like.dto.LikeDto;
import com.prgrms.prolog.domain.like.dto.LikeDto.likeRequest;
Expand All @@ -29,19 +27,18 @@ public class LikeController {
private final LikeServiceImpl likeService;

@PostMapping(value = "/{postId}")
public ResponseEntity<Long> insert(
@ResponseStatus(HttpStatus.NO_CONTENT)
public void insert(
@PathVariable Long postId,
@AuthenticationPrincipal JwtAuthentication user
) {
LikeDto.likeRequest request = new likeRequest(user.id(), postId);
Long likeId = likeService.save(request);
URI location = UriComponentsBuilder.fromUriString("/api/v1/like/" + likeId).build().toUri();
return ResponseEntity.created(location).build();
likeService.save(request);
}

@DeleteMapping
public ResponseEntity<Void> delete(@RequestBody @Valid likeRequest likeRequest) {
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@RequestBody @Valid likeRequest likeRequest) {
likeService.cancel(likeRequest);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public record PostResponse(String title,
Set<String> tags,
SeriesResponse seriesResponse,
List<Comment> comment,
int commentCount) {
int commentCount,
int likeCount) {

public static PostResponse toPostResponse(Post post) {
return new PostResponse(post.getTitle(),
Expand All @@ -28,6 +29,7 @@ public static PostResponse toPostResponse(Post post) {
PostTagsResponse.from(post.getPostTags()).tagNames(),
SeriesResponse.toSeriesResponse(post.getSeries()),
post.getComments(),
post.getComments().size());
post.getComments().size(),
post.getLikeCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void registerSeries(CreateRequest request, Post post, User owner) {
@Override
public PostResponse findById(Long postId) {
Post post = postRepository.joinCommentFindById(postId)
.orElseThrow(() -> new IllegalArgumentException(POST_NOT_EXIST_MESSAGE));
.orElseThrow(() -> new IllegalArgumentException("exception.post.notExists"));
Set<PostTag> findPostTags = postTagRepository.joinRootTagFindByPostId(postId);
post.addPostTagsFrom(findPostTags);
return PostResponse.toPostResponse(post);
Expand All @@ -95,7 +95,7 @@ public Page<PostResponse> findAll(Pageable pageable) {
@Transactional
public PostResponse update(UpdateRequest update, Long userId, Long postId) {
Post findPost = postRepository.joinUserFindById(postId)
.orElseThrow(() -> new IllegalArgumentException(POST_NOT_EXIST_MESSAGE));
.orElseThrow(() -> new IllegalArgumentException("exception.post.notExists"));

if (!findPost.getUser().checkSameUserId(userId)) {
throw new IllegalArgumentException("exception.post.not.owner");
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
<logger name="jdbc" level="OFF"/>
<logger name="jdbc.connection" level="OFF"/>
<logger name="jdbc.sqltiming" level="INFO" additivity="false">
<appender-ref ref="SQL_FILE"/>
<appender-ref ref="CONSOLE_APPENDER"/>
</logger>
<logger name="jdbc.resultsettable" level="INFO" additivity="false">
<appender-ref ref="SQL_FILE"/>
<appender-ref ref="CONSOLE_APPENDER"/>
</logger>
</springProfile>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,8 @@ void likeSaveApiTest() throws Exception {
.header(HttpHeaders.AUTHORIZATION, BEARER_TYPE + jwtTokenProvider.createAccessToken(claims))
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(likeRequest)))
.andExpect(status().isCreated())
.andExpect(status().isNoContent())
.andDo(restDocs.document(
requestFields(
fieldWithPath("userId").description("사용자 아이디"),
fieldWithPath("postId").description("게시물 아이디")
),
responseBody()
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ void findAll() throws Exception {
fieldWithPath("[].seriesResponse").type(JsonFieldType.OBJECT).description("series"),
fieldWithPath("[].seriesResponse.title").type(JsonFieldType.STRING).description("seriesTitle"),
fieldWithPath("[].seriesResponse.posts").type(JsonFieldType.ARRAY).description("postInSeries"),
fieldWithPath("[].seriesResponse.posts.[].id").type(JsonFieldType.NUMBER).description("postIdInSeries"),
fieldWithPath("[].seriesResponse.posts.[].title").type(JsonFieldType.STRING).description("postTitleInSeries"),
fieldWithPath("[].seriesResponse.count").type(JsonFieldType.NUMBER).description("seriesCount")
fieldWithPath("[].seriesResponse.posts.[].id").type(JsonFieldType.NUMBER)
.description("postIdInSeries"),
fieldWithPath("[].seriesResponse.posts.[].title").type(JsonFieldType.STRING)
.description("postTitleInSeries"),
fieldWithPath("[].seriesResponse.count").type(JsonFieldType.NUMBER).description("seriesCount"),
fieldWithPath("[].likeCount").type(JsonFieldType.NUMBER).description("likeCount")

)));
}

Expand Down Expand Up @@ -155,9 +159,12 @@ void findById() throws Exception {
fieldWithPath("seriesResponse").type(JsonFieldType.OBJECT).description("series"),
fieldWithPath("seriesResponse.title").type(JsonFieldType.STRING).description("seriesTitle"),
fieldWithPath("seriesResponse.posts").type(JsonFieldType.ARRAY).description("postInSeries"),
fieldWithPath("seriesResponse.posts.[].id").type(JsonFieldType.NUMBER).description("postIdInSeries"),
fieldWithPath("seriesResponse.posts.[].title").type(JsonFieldType.STRING).description("postTitleInSeries"),
fieldWithPath("seriesResponse.count").type(JsonFieldType.NUMBER).description("seriesCount")
fieldWithPath("seriesResponse.posts.[].id").type(JsonFieldType.NUMBER)
.description("postIdInSeries"),
fieldWithPath("seriesResponse.posts.[].title").type(JsonFieldType.STRING)
.description("postTitleInSeries"),
fieldWithPath("seriesResponse.count").type(JsonFieldType.NUMBER).description("seriesCount"),
fieldWithPath("likeCount").type(JsonFieldType.NUMBER).description("likeCount")
)));
}

Expand Down Expand Up @@ -195,9 +202,12 @@ void update() throws Exception {
fieldWithPath("seriesResponse").type(JsonFieldType.OBJECT).description("series"),
fieldWithPath("seriesResponse.title").type(JsonFieldType.STRING).description("seriesTitle"),
fieldWithPath("seriesResponse.posts").type(JsonFieldType.ARRAY).description("postInSeries"),
fieldWithPath("seriesResponse.posts.[].id").type(JsonFieldType.NUMBER).description("postIdInSeries"),
fieldWithPath("seriesResponse.posts.[].title").type(JsonFieldType.STRING).description("postTitleInSeries"),
fieldWithPath("seriesResponse.count").type(JsonFieldType.NUMBER).description("seriesCount")
fieldWithPath("seriesResponse.posts.[].id").type(JsonFieldType.NUMBER)
.description("postIdInSeries"),
fieldWithPath("seriesResponse.posts.[].title").type(JsonFieldType.STRING)
.description("postTitleInSeries"),
fieldWithPath("seriesResponse.count").type(JsonFieldType.NUMBER).description("seriesCount"),
fieldWithPath("likeCount").type(JsonFieldType.NUMBER).description("likeCount")
)
));
}
Expand Down

0 comments on commit 45b0a6e

Please sign in to comment.