-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: 방문 기록 생성 API 구현 #21 #64
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0c26dd7
feat: 방문 기록 생성 기능 구현
BurningFalls bb04ba0
feat: getter 및 builder 추가
BurningFalls 161e70e
feat: VisitService에 Transactional 적용
BurningFalls 9d32020
test: 방문 기록 생성 테스트
BurningFalls 64db602
fix: 충돌 해결
BurningFalls 34fadbc
fix: 오타 수정
BurningFalls fb2ac67
style: 코드 컨벤션 적용
BurningFalls dee878c
fix: deleteById에 Transactional annotation 추가
BurningFalls ba67186
Merge branch 'develop-be' of https://github.com/woowacourse-teams/202…
BurningFalls 98c87b4
refactor: builder 파라미터 NonNull 설정 추가
BurningFalls 8ce7c06
refactor: 데이터 개수 감소
BurningFalls 9de185c
refactor: 예외 메시지 구체화 및 상태 코드 변경
BurningFalls File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/com/staccato/visit/repository/VisitImageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.staccato.visit.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.staccato.visit.domain.VisitImage; | ||
|
||
public interface VisitImageRepository extends JpaRepository<VisitImage, Long> { | ||
} |
54 changes: 51 additions & 3 deletions
54
backend/src/main/java/com/staccato/visit/service/VisitService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,68 @@ | ||
package com.staccato.visit.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.staccato.exception.StaccatoException; | ||
import com.staccato.pin.domain.Pin; | ||
import com.staccato.pin.repository.PinRepository; | ||
import com.staccato.travel.domain.Travel; | ||
import com.staccato.travel.repository.TravelRepository; | ||
import com.staccato.visit.domain.Visit; | ||
import com.staccato.visit.domain.VisitImage; | ||
import com.staccato.visit.repository.VisitImageRepository; | ||
import com.staccato.visit.repository.VisitLogRepository; | ||
import com.staccato.visit.repository.VisitRepository; | ||
import com.staccato.visit.service.dto.request.VisitRequest; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class VisitService { | ||
private final VisitRepository visitRepository; | ||
private final PinRepository pinRepository; | ||
private final TravelRepository travelRepository; | ||
private final VisitImageRepository visitImageRepository; | ||
private final VisitLogRepository visitLogRepository; | ||
|
||
@Transactional | ||
public long createVisit(VisitRequest visitRequest) { | ||
Pin pin = getPinById(visitRequest.pinId()); | ||
Travel travel = getTravelById(visitRequest.travelId()); | ||
Visit visit = visitRepository.save(visitRequest.toVisit(pin, travel)); | ||
|
||
List<VisitImage> visitImages = makeVisitImages(visitRequest.visitedImages(), visit); | ||
visitImageRepository.saveAll(visitImages); | ||
|
||
return visit.getId(); | ||
} | ||
|
||
@Transactional | ||
public void deleteById(Long visitId) { | ||
visitLogRepository.deleteByVisitId(visitId); | ||
visitRepository.deleteById(visitId); | ||
} | ||
|
||
private List<VisitImage> makeVisitImages(List<String> visitedImages, Visit visit) { | ||
return visitedImages.stream() | ||
.map(visitImage -> VisitImage.builder() | ||
.imageUrl(visitImage) | ||
.visit(visit) | ||
.build()) | ||
.toList(); | ||
} | ||
|
||
private Pin getPinById(long pinId) { | ||
return pinRepository.findById(pinId) | ||
.orElseThrow(() -> new StaccatoException("요청하신 핀을 찾을 수 없어요.")); | ||
} | ||
|
||
private Travel getTravelById(long travelId) { | ||
return travelRepository.findById(travelId) | ||
.orElseThrow(() -> new StaccatoException("요청하신 여행을 찾을 수 없어요.")); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
backend/src/main/java/com/staccato/visit/service/dto/request/VisitRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.staccato.visit.service.dto.request; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
import org.springframework.format.annotation.DateTimeFormat; | ||
|
||
import com.staccato.pin.domain.Pin; | ||
import com.staccato.travel.domain.Travel; | ||
import com.staccato.visit.domain.Visit; | ||
|
||
public record VisitRequest( | ||
@NotNull(message = "핀을 선택해주세요.") | ||
Long pinId, | ||
List<String> visitedImages, | ||
@NotNull(message = "방문 날짜를 입력해주세요.") | ||
@DateTimeFormat(pattern = "yyyy-MM-dd") | ||
LocalDate visitedAt, | ||
@NotNull(message = "여행 상세를 선택해주세요.") | ||
Long travelId) { | ||
public Visit toVisit(Pin pin, Travel travel) { | ||
return Visit.builder() | ||
.visitedAt(visitedAt) | ||
.pin(pin) | ||
.travel(travel) | ||
.build(); | ||
} | ||
} |
94 changes: 89 additions & 5 deletions
94
backend/src/test/java/com/staccato/visit/controller/VisitIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,104 @@ | ||
package com.staccato.visit.controller; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
|
||
import com.staccato.IntegrationTest; | ||
import com.staccato.pin.domain.Pin; | ||
import com.staccato.pin.repository.PinRepository; | ||
import com.staccato.travel.domain.Travel; | ||
import com.staccato.travel.repository.TravelRepository; | ||
import com.staccato.visit.service.dto.request.VisitRequest; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.http.ContentType; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) | ||
class VisitIntegrationTest { | ||
class VisitIntegrationTest extends IntegrationTest { | ||
private static final String USER_AUTHORIZATION = "1"; | ||
|
||
@Autowired | ||
private PinRepository pinRepository; | ||
@Autowired | ||
private TravelRepository travelRepository; | ||
|
||
static Stream<Arguments> invalidVisitRequestProvider() { | ||
return Stream.of( | ||
Arguments.of( | ||
new VisitRequest(null, List.of("https://example1.com.jpg"), LocalDate.of(2023, 7, 1), 1L), | ||
"핀을 선택해주세요." | ||
), | ||
Arguments.of( | ||
new VisitRequest(1L, List.of("https://example1.com.jpg"), null, 1L), | ||
"방문 날짜를 입력해주세요." | ||
), | ||
Arguments.of( | ||
new VisitRequest(1L, List.of("https://example1.com.jpg"), LocalDate.of(2023, 7, 1), null), | ||
"여행 상세를 선택해주세요." | ||
) | ||
); | ||
} | ||
|
||
@BeforeEach | ||
void init() { | ||
pinRepository.save(Pin.builder().place("장소").address("주소").build()); | ||
travelRepository.save(Travel.builder() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. travelRepository에 의존하기보다는 여행 상세 생성 API가 구현되어 있으니 DynamicTest로 여행 상세 생성을 호출하고 필요한 테스트를 수행하면 어떨까 제안해봐요!(필수 사항은 아닙니다.)
|
||
.thumbnailUrl("https://example1.com.jpg") | ||
.title("2023 여름 휴가") | ||
.description("친구들과 함께한 여름 휴가 여행") | ||
.startAt(LocalDate.of(2023, 7, 1)) | ||
.endAt(LocalDate.of(2023, 7, 10)) | ||
.build()); | ||
} | ||
|
||
@DisplayName("사용자가 방문 기록 정보를 입력하면, 새로운 방문 기록을 생성한다.") | ||
@Test | ||
void createVisit() { | ||
// given | ||
VisitRequest visitRequest = new VisitRequest(1L, List.of("https://example1.com.jpg"), | ||
LocalDate.of(2023, 7, 1), 1L); | ||
|
||
// when & then | ||
RestAssured.given().log().all() | ||
.contentType(ContentType.JSON) | ||
.header(HttpHeaders.AUTHORIZATION, USER_AUTHORIZATION) | ||
.body(visitRequest) | ||
.when().log().all() | ||
.post("/visits") | ||
.then().log().all() | ||
.assertThat().statusCode(HttpStatus.CREATED.value()) | ||
.header(HttpHeaders.LOCATION, containsString("/visits/")); | ||
} | ||
|
||
@DisplayName("사용자가 잘못된 방식으로 정보를 입력하면, 방문 기록을 생성할 수 없다.") | ||
@ParameterizedTest | ||
@MethodSource("invalidVisitRequestProvider") | ||
void failCreateTravel(VisitRequest visitRequest, String expectedMessage) { | ||
RestAssured.given().log().all() | ||
.contentType(ContentType.JSON) | ||
.header(HttpHeaders.AUTHORIZATION, USER_AUTHORIZATION) | ||
.body(visitRequest) | ||
.when().log().all() | ||
.post("/visits") | ||
.then().log().all() | ||
.assertThat().statusCode(HttpStatus.BAD_REQUEST.value()) | ||
.body("message", is(expectedMessage)) | ||
.body("status", is(HttpStatus.BAD_REQUEST.toString())); | ||
} | ||
|
||
@DisplayName("Visit을 삭제한다.") | ||
@Test | ||
void deleteById() { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍