Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

게시글 태그 삭제 기능 리팩터링 #93

Merged
merged 4 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public Page<PostResponse> findAll(Pageable pageable) {
@Override
@Transactional
public PostResponse update(UpdateRequest update, Long userId, Long postId) {
Post findPost = postRepository.joinUserFindById(postId)
.orElseThrow(() -> new IllegalArgumentException("exception.post.notExists"));
Post findPost = postRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException(POST_NOT_EXIST_MESSAGE));

if (!findPost.getUser().checkSameUserId(userId)) {
throw new IllegalArgumentException("exception.post.not.owner");
Expand All @@ -114,6 +114,12 @@ public PostResponse update(UpdateRequest update, Long userId, Long postId) {
public void delete(Long postId) {
Post findPost = postRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException(POST_NOT_EXIST_MESSAGE));
Set<RootTag> findRootTags = postTagRepository.joinRootTagFindByPostId(findPost.getId())
.stream()
.map(PostTag::getRootTag)
.collect(Collectors.toSet());
removeOrDecreaseUserTags(findPost.getUser(), findRootTags);
postTagRepository.deleteByPostId(postId);
postRepository.delete(findPost);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ void deleteByPostIdAndRootTagIds(
WHERE pt.post.id = :postId
""")
Set<PostTag> joinRootTagFindByPostId(@Param(value = "postId") Long postId);

@Modifying
@Query("""
DELETE
FROM PostTag pt
WHERE pt.post.id = :postId
""")
void deleteByPostId(@Param(value = "postId") Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.prgrms.prolog.domain.series.dto.SeriesResponse;
Expand All @@ -20,9 +20,9 @@ public class SeriesController {

private final SeriesService seriesService;

@GetMapping("/{title}")
@GetMapping()
ResponseEntity<SeriesResponse> findSeriesByTitle(
@PathVariable String title,
@RequestParam String title,
@AuthenticationPrincipal JwtAuthentication user
) {
return ResponseEntity.ok(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ void findSeriesByTitleTest() throws Exception {
// given
Claims claims = Claims.from(savedUser.getId(), USER_ROLE);
// when
mockMvc.perform(get("/api/v1/series/{title}", SERIES_TITLE)
mockMvc.perform(get("/api/v1/series")
.header(AUTHORIZATION, BEARER_TYPE + jwtTokenProvider.createAccessToken(claims))
.param("title", SERIES_TITLE)
)
// then
.andExpectAll(
Expand Down