diff --git a/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/persistence/Bus.kt b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/persistence/Bus.kt new file mode 100644 index 0000000..63034a2 --- /dev/null +++ b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/persistence/Bus.kt @@ -0,0 +1,23 @@ +package kr.hs.dgsw.canbusserver.domain.bus.persistence + +import jakarta.persistence.* + +@Entity +@Table(name = "bus") +class Bus ( + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long? = null, + + val duration: String, + + val timeRange: String, + + val busNumber: String, + + @OneToMany + @JoinColumn(name = "bus_id") + val stations: List + +) \ No newline at end of file diff --git a/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/persistence/BusRepository.kt b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/persistence/BusRepository.kt new file mode 100644 index 0000000..0937763 --- /dev/null +++ b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/persistence/BusRepository.kt @@ -0,0 +1,6 @@ +package kr.hs.dgsw.canbusserver.domain.bus.persistence + +import org.springframework.data.repository.CrudRepository + +interface BusRepository : CrudRepository { +} \ No newline at end of file diff --git a/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/persistence/Station.kt b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/persistence/Station.kt new file mode 100644 index 0000000..944fd47 --- /dev/null +++ b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/persistence/Station.kt @@ -0,0 +1,17 @@ +package kr.hs.dgsw.canbusserver.domain.bus.persistence + +import jakarta.persistence.* + +@Entity +@Table(name = "station") +class Station( + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long? = null, + + val name: String, + + val time: String, + +) \ No newline at end of file diff --git a/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/presentation/BusController.kt b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/presentation/BusController.kt new file mode 100644 index 0000000..8d703f6 --- /dev/null +++ b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/presentation/BusController.kt @@ -0,0 +1,24 @@ +package kr.hs.dgsw.canbusserver.domain.bus.presentation + +import kr.hs.dgsw.canbusserver.domain.bus.presentation.dto.ApplyBusRequest +import kr.hs.dgsw.canbusserver.domain.bus.presentation.dto.BusResponse +import kr.hs.dgsw.canbusserver.domain.bus.service.BusService +import org.springframework.web.bind.annotation.* + +@RestController +@RequestMapping("/bus") +class BusController( + private val busService: BusService +) { + + @GetMapping + fun getBusList(): List { + return busService.getBusList() + } + + @PostMapping("/apply") + fun applyBus(@RequestBody request: ApplyBusRequest) { + busService.applyBus(request) + } + +} \ No newline at end of file diff --git a/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/presentation/dto/ApplyBusRequest.kt b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/presentation/dto/ApplyBusRequest.kt new file mode 100644 index 0000000..4579226 --- /dev/null +++ b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/presentation/dto/ApplyBusRequest.kt @@ -0,0 +1,11 @@ +package kr.hs.dgsw.canbusserver.domain.bus.presentation.dto + +class ApplyBusRequest( + + val arrivalPlace: String, + + val departurePlace: String, + + val departureTime: String, + +) \ No newline at end of file diff --git a/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/presentation/dto/BusResponse.kt b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/presentation/dto/BusResponse.kt new file mode 100644 index 0000000..60ac278 --- /dev/null +++ b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/presentation/dto/BusResponse.kt @@ -0,0 +1,20 @@ +package kr.hs.dgsw.canbusserver.domain.bus.presentation.dto + +class BusResponse( + + val duration: String, + + val timeRange: String, + + val busNumber: String, + + val stations: List, + + ) { + + class Station( + val name: String, + val time: String, + ) + +} \ No newline at end of file diff --git a/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/service/BusService.kt b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/service/BusService.kt new file mode 100644 index 0000000..cebadc9 --- /dev/null +++ b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/bus/service/BusService.kt @@ -0,0 +1,55 @@ +package kr.hs.dgsw.canbusserver.domain.bus.service + +import kr.hs.dgsw.canbusserver.domain.bus.persistence.BusRepository +import kr.hs.dgsw.canbusserver.domain.bus.presentation.dto.ApplyBusRequest +import kr.hs.dgsw.canbusserver.domain.bus.presentation.dto.BusResponse +import kr.hs.dgsw.canbusserver.domain.passenger.Passenger +import kr.hs.dgsw.canbusserver.domain.passenger.PassengerRepository +import kr.hs.dgsw.canbusserver.domain.user.UserRepository +import kr.hs.dgsw.canbusserver.global.security.SecurityUtil +import org.springframework.data.repository.findByIdOrNull +import org.springframework.stereotype.Service + +@Service +class BusService( + private val busRepository: BusRepository, + private val userRepository: UserRepository, + private val passengerRepository: PassengerRepository, + private val securityUtil: SecurityUtil +) { + + fun getBusList(): List { + + val busList = busRepository.findAll() + + return busList.map { bus -> + BusResponse( + busNumber = bus.busNumber, + duration = bus.duration, + timeRange = bus.timeRange, + stations = bus.stations.map { + BusResponse.Station( + name = it.name, + time = it.time, + ) + } + ) + } + } + + fun applyBus(request: ApplyBusRequest) { + + val user = userRepository.findByIdOrNull(securityUtil.getCurrentUserId())!! + + val passenger = Passenger( + arrivalPlace = request.arrivalPlace, + departurePlace = request.departurePlace, + departureTime = request.departureTime, + user = user + ) + + passengerRepository.save(passenger) + + } + +} \ No newline at end of file diff --git a/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/passenger/Passenger.kt b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/passenger/Passenger.kt new file mode 100644 index 0000000..13bbe10 --- /dev/null +++ b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/passenger/Passenger.kt @@ -0,0 +1,24 @@ +package kr.hs.dgsw.canbusserver.domain.passenger + +import jakarta.persistence.* +import kr.hs.dgsw.canbusserver.domain.user.User + +@Entity +@Table(name = "passenger") +class Passenger ( + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long? = null, + + val arrivalPlace: String, + + val departurePlace: String, + + val departureTime: String, + + @ManyToOne + @JoinColumn(name = "user_id") + val user: User, + +) \ No newline at end of file diff --git a/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/passenger/PassengerRepository.kt b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/passenger/PassengerRepository.kt new file mode 100644 index 0000000..c182a56 --- /dev/null +++ b/canbus-server/src/main/kotlin/kr/hs/dgsw/canbusserver/domain/passenger/PassengerRepository.kt @@ -0,0 +1,6 @@ +package kr.hs.dgsw.canbusserver.domain.passenger + +import org.springframework.data.repository.CrudRepository + +interface PassengerRepository : CrudRepository { +} \ No newline at end of file diff --git a/canbus-server/src/main/resources/application.yml b/canbus-server/src/main/resources/application.yml index 17421dc..e54f890 100644 --- a/canbus-server/src/main/resources/application.yml +++ b/canbus-server/src/main/resources/application.yml @@ -4,7 +4,7 @@ spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://${DB_URL}:3306/${DB_SCHEMA}?useSSL=true + url: jdbc:mysql://${DB_URL}:3306/${DB_SCHEMA}?useSSL=true&characterEncoding=UTF-8 username: ${DB_USERNAME} password: ${DB_PASSWORD}