Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: Add Bus Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rlawhddbs committed Aug 27, 2024
1 parent 3fce887 commit 8c3d5ac
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<Station>

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kr.hs.dgsw.canbusserver.domain.bus.persistence

import org.springframework.data.repository.CrudRepository

interface BusRepository : CrudRepository<Bus, Long> {
}
Original file line number Diff line number Diff line change
@@ -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,

)
Original file line number Diff line number Diff line change
@@ -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<BusResponse> {
return busService.getBusList()
}

@PostMapping("/apply")
fun applyBus(@RequestBody request: ApplyBusRequest) {
busService.applyBus(request)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kr.hs.dgsw.canbusserver.domain.bus.presentation.dto

class ApplyBusRequest(

val arrivalPlace: String,

val departurePlace: String,

val departureTime: String,

)
Original file line number Diff line number Diff line change
@@ -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<Station>,

) {

class Station(
val name: String,
val time: String,
)

}
Original file line number Diff line number Diff line change
@@ -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<BusResponse> {

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)

}

}
Original file line number Diff line number Diff line change
@@ -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,

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kr.hs.dgsw.canbusserver.domain.passenger

import org.springframework.data.repository.CrudRepository

interface PassengerRepository : CrudRepository<Passenger, Long> {
}
2 changes: 1 addition & 1 deletion canbus-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down

0 comments on commit 8c3d5ac

Please sign in to comment.