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

Feature/tony/trip/user calling taxi #101

Merged
merged 7 commits into from
Oct 16, 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
12 changes: 11 additions & 1 deletion src/main/kotlin/com/hh2/katj/taxidriver/model/TaxiDriver.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.hh2.katj.taxidriver.model

import com.hh2.katj.taxi.model.Taxi
import com.hh2.katj.util.exception.ExceptionMessage.INCORRECT_STATUS_VALUE
import com.hh2.katj.util.exception.failWithMessage
import com.hh2.katj.util.model.BaseEntity
import com.hh2.katj.util.model.Gender
import com.hh2.katj.taxi.model.Taxi
import com.hh2.katj.util.model.RoadAddress
import jakarta.persistence.*
import java.time.LocalDate
Expand All @@ -19,6 +21,7 @@ class TaxiDriver(
address: RoadAddress,
img: String
): BaseEntity() {

@OneToOne
@JoinColumn(name = "taxi_id", nullable = false,
foreignKey = ForeignKey(ConstraintMode.NO_CONSTRAINT))
Expand Down Expand Up @@ -47,4 +50,11 @@ class TaxiDriver(

@Column(nullable = false)
val img: String = img

fun statusWaiting() {
if (this.status != TaxiDriverStatus.WAITING) {
failWithMessage(INCORRECT_STATUS_VALUE.name)
}
}

}
14 changes: 13 additions & 1 deletion src/main/kotlin/com/hh2/katj/trip/component/TripReader.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hh2.katj.trip.component

import com.hh2.katj.trip.model.Trip
import com.hh2.katj.trip.model.TripStatus
import com.hh2.katj.trip.repository.TripRepository
import com.hh2.katj.util.exception.ExceptionMessage
import com.hh2.katj.util.exception.ExceptionMessage.ID_DOES_NOT_EXIST
Expand All @@ -19,9 +20,20 @@ class TripReader (
return findTrip
}

@Transactional(readOnly = true)
fun findOneEndTripByUser(userId: Long, tripId: Long): Trip {
return tripRepository.findByIdAndUserIdAndTripStatus(tripId, userId, TripStatus.END) ?:
failWithMessage(ID_DOES_NOT_EXIST.name)
}

@Transactional(readOnly = true)
fun findAllEndTripByUserId(userId: Long): List<Trip>{
return tripRepository.findAllByUserIdAndTripStatus(userId, TripStatus.END)
}

@Transactional(readOnly = true)
fun findByIdAndUserId(tripId: Long, userId: Long): Trip {
return tripRepository.findByIdAndUserId(tripId, userId) ?:
failWithMessage(ID_DOES_NOT_EXIST.name)
failWithMessage(ID_DOES_NOT_EXIST.name)
}
}
23 changes: 22 additions & 1 deletion src/main/kotlin/com/hh2/katj/trip/controller/TripController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,33 @@ class TripController (

/**
* 택시 기사가 사용자에게 결제를 요청하고 결제가 완료되면
* trip status가 PAY_REQUEST면 결제 진행 후 PAY_COMPLETE로 변경
* trip status가 ASSIGN_TAXI 이면 결제 진행 후 END 로 변경
*/
@GetMapping("/taxi-driver/request-pay/{tripId}")
fun taxiDriverRequestPayToUser(@RequestParam userId: Long, @PathVariable tripId: Long): ResponseEntity<ResponseTrip> {
val responseTrip = billingService.userPayWithRegiPaymentMethod(userId, tripId)
return ResponseEntity.ok(responseTrip)
}

/**
* 사용자가 해당 택시 이용 기록을 조회한다
*/
@GetMapping("/search/end-trip/{tripId}")
fun findOneEndTripByUser(@RequestParam userId: Long, @PathVariable tripId: Long) : ResponseEntity<ResponseTrip> {
val responseTrip = callingService.findOneEndTripByUser(userId, tripId)
return ResponseEntity.ok(responseTrip)
}

/**
* 사용자가 모든 택시 이용 기록을 조회한다
*/
@GetMapping("/search/end-trip")
fun findAllEndTripByUser(@RequestParam userId: Long) : ResponseEntity<List<ResponseTrip>> {
val responseTripList = callingService.findAllEndTripByUser(userId)
return ResponseEntity.ok(responseTripList)
}




}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.hh2.katj.trip.repository

import com.hh2.katj.trip.model.Trip
import com.hh2.katj.trip.model.TripStatus
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository
interface TripRepository: JpaRepository<Trip, Long> {
fun findByIdAndUserId(tripId: Long, userId: Long): Trip?
fun findAllByUserId(userId: Long): List<Trip>
fun findByIdAndUserIdAndTripStatus(tripId: Long, userId: Long, tripStatus: TripStatus): Trip?
fun findAllByUserIdAndTripStatus(userId: Long, tripStatus: TripStatus): List<Trip>

}
18 changes: 12 additions & 6 deletions src/main/kotlin/com/hh2/katj/trip/service/BillingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.hh2.katj.trip.service

import com.hh2.katj.payment.component.PaymentMethodReader
import com.hh2.katj.payment.model.entity.PaymentMethod
import com.hh2.katj.payment.service.PaymentMethodService
import com.hh2.katj.taxidriver.model.TaxiDriver
import com.hh2.katj.trip.component.TripManager
import com.hh2.katj.trip.component.TripReader
import com.hh2.katj.trip.model.Trip
Expand All @@ -11,10 +11,8 @@ import com.hh2.katj.trip.model.response.ResponseTrip
import com.hh2.katj.user.model.entity.User
import com.hh2.katj.user.service.UserService
import com.hh2.katj.util.exception.DataNotFoundException
import com.hh2.katj.util.exception.ExceptionMessage
import com.hh2.katj.util.exception.ExceptionMessage.*
import com.hh2.katj.util.exception.ExceptionMessage.INCORRECT_STATUS_VALUE
import org.springframework.stereotype.Service
import java.lang.IllegalArgumentException

@Service
class BillingService(
Expand All @@ -29,10 +27,15 @@ class BillingService(
val validatedTrip = tripValidation(validatedUser.id, tripId)

/**
* 요청온 trip의 상태 코드가 PAY_REQUEST인지 재검증
* 요청온 trip의 상태 코드가 ASSIGN_TAXI 인지 재검증
*/
requestPayCheck(validatedTrip)

/**
* 택시 기사 상태가 WAITRING 인지 확인
*/
taxiDriverStatusCheck(validatedTrip.taxiDriver!!)

/**
* 사용자 결제 수단 조회
* 1.기본 결제 수단 존재시 먼저 사용
Expand All @@ -56,10 +59,13 @@ class BillingService(
* 사용자 입장에서 결제가 무사히 종료되면 기사에게 알리기?
* 인데 그런거 없으니 PAY_COMPLETE로 변경되는거 기사쪽에서 알면 상태 변경 해야할듯
*/

return updateTrip.toResponseDto()
}

private fun taxiDriverStatusCheck(taxiDriver: TaxiDriver) {
taxiDriver.statusWaiting()
}

private fun requestPayCheck(trip: Trip) {
if (trip.tripStatus != TripStatus.ASSIGN_TAXI) {
throw IllegalArgumentException(INCORRECT_STATUS_VALUE.name)
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/com/hh2/katj/trip/service/CallingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,17 @@ class CallingService (
return userService.userValidationCheck(findUser.id)
}

fun findOneEndTripByUser(userId: Long, tripId: Long): ResponseTrip {
val validatedUser = userValidation(userId)

val findOneTrip = tripReader.findOneEndTripByUser(validatedUser.id, tripId)
return findOneTrip.toResponseDto()
}

fun findAllEndTripByUser(userId: Long): List<ResponseTrip> {
val validatedUser = userValidation(userId)

return tripReader.findAllEndTripByUserId(validatedUser.id).map(Trip::toResponseDto)
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.hh2.katj.payment.service
package com.hh2.katj.trip.service

import com.hh2.katj.payment.component.PaymentMethodReader
import com.hh2.katj.payment.model.dto.request.RequestAddBankAccount
import com.hh2.katj.payment.model.dto.request.RequestAddCard
import com.hh2.katj.payment.model.entity.Bank
import com.hh2.katj.payment.model.entity.PaymentMethod
import com.hh2.katj.payment.repository.PaymentMethodRepository
import com.hh2.katj.payment.service.PaymentMethodService
import com.hh2.katj.taxi.model.ChargeType
import com.hh2.katj.taxi.model.FuelType
import com.hh2.katj.taxi.model.Taxi
Expand All @@ -18,7 +19,6 @@ import com.hh2.katj.trip.model.DestinationRoadAddress
import com.hh2.katj.trip.model.Trip
import com.hh2.katj.trip.model.TripStatus
import com.hh2.katj.trip.repository.TripRepository
import com.hh2.katj.trip.service.BillingService
import com.hh2.katj.user.model.entity.User
import com.hh2.katj.user.model.entity.UserStatus
import com.hh2.katj.user.repository.UserRepository
Expand Down Expand Up @@ -87,7 +87,7 @@ class BillingServiceTest(

@BeforeEach
fun setUp() {
val roadAddress: RoadAddress = RoadAddress(
val roadAddress = RoadAddress(
addressName = "address_name",
region1depthName = "r_1",
region2depthName = "r_2",
Expand Down Expand Up @@ -125,7 +125,7 @@ class BillingServiceTest(
issueDate = LocalDate.now().minusYears(5),
securityId = "security_id",
name = "Tom",
status = TaxiDriverStatus.STARTDRIVE,
status = TaxiDriverStatus.WAITING,
gender = Gender.UNKNOWN,
address = roadAddress,
img = "123"
Expand Down
106 changes: 103 additions & 3 deletions src/test/kotlin/com/hh2/katj/trip/service/CallingServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import com.hh2.katj.user.model.entity.User
import com.hh2.katj.user.model.entity.UserStatus
import com.hh2.katj.user.repository.UserRepository
import com.hh2.katj.util.annotation.KATJTestContainerE2E
import com.hh2.katj.util.exception.ExceptionMessage
import com.hh2.katj.util.exception.ExceptionMessage.ID_DOES_NOT_EXIST
import com.hh2.katj.util.exception.ExceptionMessage.INCORRECT_STATUS_VALUE
import com.hh2.katj.util.model.Gender
import com.hh2.katj.util.model.RoadAddress
import org.assertj.core.api.Assertions
import org.assertj.core.api.AssertionsForInterfaceTypes.*
import org.assertj.core.api.AssertionsForInterfaceTypes.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -231,7 +232,7 @@ class CallingServiceTest(
assertThrows<IllegalArgumentException> {
callingService.callTaxiByUser(requestTrip)
}.apply {
Assertions.assertThat(message).isEqualTo(ExceptionMessage.INCORRECT_STATUS_VALUE.name)
Assertions.assertThat(message).isEqualTo(INCORRECT_STATUS_VALUE.name)
}
}

Expand All @@ -256,5 +257,104 @@ class CallingServiceTest(

// then
}

@Test
fun `사용자가 택시 이용 기록을 조회한다`() {
// given
val requestCreateTripByUser = RequestTrip(
user = user,
taxiDriver = taxiDriver,
departure = departure,
fare = 5000,
destination = destination,
driveStartDate = LocalDate.now(),
driveStartAt = LocalDateTime.now(),
spentTime = 12000000,
tripStatus = TripStatus.END,
)

val requestTrip = requestCreateTripByUser.toEntity()
val saveTrip = tripRepository.save(requestTrip)

// when
val responseTrip: ResponseTrip = callingService.findOneEndTripByUser(user.id, saveTrip.id)

// then
assertThat(responseTrip.tripStatus).isEqualTo(TripStatus.END)
}

@Test
fun `사용자가 택시 이용 기록 조회에 실패한다`() {
// given
val tripId = 0L

// when // then
assertThrows<IllegalArgumentException>{
callingService.findOneEndTripByUser(user.id, tripId)
}.apply{
assertThat(message).isEqualTo(ID_DOES_NOT_EXIST.name)
}
}

@Test
fun `사용자가 택시 이용 기록을 모두 조회한다`() {
// given
val firstTrip = RequestTrip(
user = user,
taxiDriver = taxiDriver,
departure = departure,
fare = 4000,
destination = destination,
driveStartDate = LocalDate.now(),
driveStartAt = LocalDateTime.now(),
spentTime = 11000000,
tripStatus = TripStatus.END,
)
val secondTrip = RequestTrip(
user = user,
taxiDriver = taxiDriver,
departure = departure,
fare = 5000,
destination = destination,
driveStartDate = LocalDate.now(),
driveStartAt = LocalDateTime.now(),
spentTime = 12000000,
tripStatus = TripStatus.END,
)
val thirdTrip = RequestTrip(
user = user,
taxiDriver = taxiDriver,
departure = departure,
fare = 6000,
destination = destination,
driveStartDate = LocalDate.now(),
driveStartAt = LocalDateTime.now(),
spentTime = 13000000,
tripStatus = TripStatus.END,
)
val firstRequest = firstTrip.toEntity()
val secondRequest = secondTrip.toEntity()
val thirdRequest = thirdTrip.toEntity()
tripRepository.save(firstRequest)
tripRepository.save(secondRequest)
tripRepository.save(thirdRequest)

// when
val trips = callingService.findAllEndTripByUser(user.id)

// then
Assertions.assertThat(trips.size).isEqualTo(3)

assertThat(trips).extracting("fare")
.containsExactlyInAnyOrder(firstTrip.fare, secondTrip.fare, thirdTrip.fare)
assertThat(trips).extracting("spentTime")
.containsExactlyInAnyOrder(firstTrip.spentTime, secondTrip.spentTime, thirdTrip.spentTime)
assertThat(trips).extracting("tripStatus")
.containsExactlyInAnyOrder(firstTrip.tripStatus, secondTrip.tripStatus, secondTrip.tripStatus)
assertThat(trips).extracting("user")
.containsExactlyInAnyOrder(firstTrip.user, secondTrip.user, thirdTrip.user)
}



}
Loading