Skip to content

Commit

Permalink
Merge pull request #389 from woowacourse-teams/feature/#388
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaeyoung22 authored Sep 21, 2023
2 parents 89db5d8 + 1fce51a commit 7ad7f59
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.tripdraw.post.application;

import static dev.tripdraw.trip.exception.TripExceptionType.TRIP_NOT_FOUND;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
Expand All @@ -25,13 +26,15 @@
import dev.tripdraw.trip.domain.PointRepository;
import dev.tripdraw.trip.domain.Trip;
import dev.tripdraw.trip.domain.TripRepository;
import java.util.List;
import dev.tripdraw.trip.exception.TripException;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RequiredArgsConstructor
@Transactional
@Service
Expand Down Expand Up @@ -91,18 +94,16 @@ public PostCreateResponse addAtExistingLocation(
}

@Transactional(readOnly = true)
public PostResponse read(LoginUser loginUser, Long postId) {
public PostResponse read(Long postId) {
Post post = postRepository.getByPostId(postId);
Member member = memberRepository.getById(loginUser.memberId());
post.validateAuthorization(member);
return PostResponse.from(post);
}

@Transactional(readOnly = true)
public PostsResponse readAllByTripId(LoginUser loginUser, Long tripId) {
Member member = memberRepository.getById(loginUser.memberId());
Trip trip = tripRepository.getById(tripId);
trip.validateAuthorization(member);
public PostsResponse readAllByTripId(Long tripId) {
if (!tripRepository.existsById(tripId)) {
throw new TripException(TRIP_NOT_FOUND);
}

return postRepository.findAllByTripId(tripId).stream()
.sorted(comparing(Post::pointRecordedAt).reversed())
Expand All @@ -128,6 +129,7 @@ public void delete(LoginUser loginUser, Long postId) {
postRepository.deleteById(postId);
}

@Transactional(readOnly = true)
public PostsSearchResponse readAll(PostSearchRequest postSearchRequest) {
PostSearchPaging postSearchPaging = postSearchRequest.toPostSearchPaging();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public ResponseEntity<PostResponse> read(
@Auth LoginUser loginUser,
@PathVariable Long postId
) {
PostResponse response = postService.read(loginUser, postId);
PostResponse response = postService.read(postId);
return ResponseEntity.ok(response);
}

Expand All @@ -119,7 +119,7 @@ public ResponseEntity<PostsResponse> readAllPostsOfTrip(
@Auth LoginUser loginUser,
@PathVariable Long tripId
) {
PostsResponse response = postService.readAllByTripId(loginUser, tripId);
PostsResponse response = postService.readAllByTripId(tripId);
return ResponseEntity.ok(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static dev.tripdraw.member.exception.MemberExceptionType.MEMBER_NOT_FOUND;
import static dev.tripdraw.post.exception.PostExceptionType.NOT_AUTHORIZED_TO_POST;
import static dev.tripdraw.post.exception.PostExceptionType.POST_NOT_FOUND;
import static dev.tripdraw.trip.exception.TripExceptionType.NOT_AUTHORIZED_TO_TRIP;
import static dev.tripdraw.trip.exception.TripExceptionType.POINT_NOT_FOUND;
import static dev.tripdraw.trip.exception.TripExceptionType.TRIP_NOT_FOUND;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -37,9 +36,6 @@
import dev.tripdraw.trip.domain.Trip;
import dev.tripdraw.trip.domain.TripRepository;
import dev.tripdraw.trip.exception.TripException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
Expand All @@ -48,6 +44,10 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@ServiceTest
Expand Down Expand Up @@ -224,7 +224,7 @@ void setUp() {
PostCreateResponse postCreateResponse = createPost("제주특별자치도 제주시 애월읍", LocalDateTime.now());

// when
PostResponse postResponse = postService.read(loginUser, postCreateResponse.postId());
PostResponse postResponse = postService.read(postCreateResponse.postId());

// then
assertSoftly(softly -> {
Expand All @@ -237,44 +237,19 @@ void setUp() {
@Test
void 특정_감상을_조회할_때_존재하지_않는_감상_ID이면_예외를_발생시킨다() {
// expect
assertThatThrownBy(() -> postService.read(loginUser, Long.MIN_VALUE))
assertThatThrownBy(() -> postService.read(Long.MIN_VALUE))
.isInstanceOf(PostException.class)
.hasMessage(POST_NOT_FOUND.message());
}

@Test
void 특정_감상을_조회할_때_존재하지_않는_사용자_닉네임이면_예외를_발생시킨다() {
// given
PostCreateResponse postCreateResponse = createPost("제주특별자치도 제주시 애월읍", LocalDateTime.now());
LoginUser wrongUser = new LoginUser(Long.MIN_VALUE);

// expect
Long postId = postCreateResponse.postId();
assertThatThrownBy(() -> postService.read(wrongUser, postId))
.isInstanceOf(MemberException.class)
.hasMessage(MEMBER_NOT_FOUND.message());
}

@Test
void 특정_감상을_조회할_때_로그인_한_사용자가_감상의_작성자가_아니면_예외가_발생한다() {
// given
PostCreateResponse postCreateResponse = createPost("제주특별자치도 제주시 애월읍", LocalDateTime.now());

// expect
Long postId = postCreateResponse.postId();
assertThatThrownBy(() -> postService.read(otherUser, postId))
.isInstanceOf(PostException.class)
.hasMessage(NOT_AUTHORIZED_TO_POST.message());
}

@Test
void 특정_여행의_모든_감상을_조회한다() {
// given
createPost("제주특별자치도 제주시 애월읍", LocalDateTime.now());
createPost("제주특별자치도 제주시 애월읍", LocalDateTime.now());

// when
PostsResponse postsResponse = postService.readAllByTripId(loginUser, trip.id());
PostsResponse postsResponse = postService.readAllByTripId(trip.id());

// then
List<PostResponse> posts = postsResponse.posts();
Expand All @@ -286,35 +261,14 @@ void setUp() {
});
}

@Test
void 특정_여행의_모든_감상을_조회할_때_존재하지_않는_사용자_닉네임이면_예외를_발생시킨다() {
// given
LoginUser wrongUser = new LoginUser(Long.MIN_VALUE);

// expect
Long tripId = trip.id();
assertThatThrownBy(() -> postService.readAllByTripId(wrongUser, tripId))
.isInstanceOf(MemberException.class)
.hasMessage(MEMBER_NOT_FOUND.message());
}

@Test
void 특정_여행의_모든_감상을_조회할_때_존재하지_않는_여행_ID이면_예외가_발생한다() {
// expect
assertThatThrownBy(() -> postService.readAllByTripId(loginUser, Long.MIN_VALUE))
assertThatThrownBy(() -> postService.readAllByTripId(Long.MIN_VALUE))
.isInstanceOf(TripException.class)
.hasMessage(TRIP_NOT_FOUND.message());
}

@Test
void 특정_여행의_모든_감상을_조회할_때_로그인_한_사용자가_여행의_주인이_아니면_예외가_발생한다() {
// expect
Long tripId = trip.id();
assertThatThrownBy(() -> postService.readAllByTripId(otherUser, tripId))
.isInstanceOf(TripException.class)
.hasMessage(NOT_AUTHORIZED_TO_TRIP.message());
}

@Test
void 조건에_해당하는_모든_여행을_조회한다() {
// given
Expand Down Expand Up @@ -357,7 +311,7 @@ void setUp() {
postService.update(loginUser, postCreateResponse.postId(), postUpdateRequest, null);

// then
PostResponse postResponseBeforeUpdate = postService.read(loginUser, postCreateResponse.postId());
PostResponse postResponseBeforeUpdate = postService.read(postCreateResponse.postId());

assertSoftly(softly -> {
softly.assertThat(postResponseBeforeUpdate.postId()).isEqualTo(postCreateResponse.postId());
Expand Down Expand Up @@ -422,7 +376,7 @@ void setUp() {
Long postId = postCreateResponse.postId();
assertDoesNotThrow(() -> postService.delete(loginUser, postId));

assertThatThrownBy(() -> postService.read(loginUser, postId))
assertThatThrownBy(() -> postService.read(postId))
.isInstanceOf(PostException.class)
.hasMessage(POST_NOT_FOUND.message());
}
Expand Down

0 comments on commit 7ad7f59

Please sign in to comment.