Skip to content

Commit

Permalink
Merge pull request #11 from Nexters/feature/5-slope-webcam
Browse files Browse the repository at this point in the history
[#5] 스키장 슬로프, 웹캠 정보 조회 API
  • Loading branch information
jun108059 authored Oct 3, 2024
2 parents cdc4ab8 + 3d83e1c commit bf3a74c
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/kotlin/nexters/weski/slope/Slope.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nexters.weski.slope

import jakarta.persistence.*
import nexters.weski.common.BaseEntity
import nexters.weski.ski_resort.SkiResort

@Entity
@Table(name = "slopes")
data class Slope(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0,
val name: String,
val webcamNumber: Int? = null,
@Enumerated(EnumType.STRING)
val difficulty: DifficultyLevel,

val isDayOperating: Boolean = false,
val isNightOperating: Boolean = false,
val isLateNightOperating: Boolean = false,
val isDawnOperating: Boolean = false,
val isMidnightOperating: Boolean = false,

@ManyToOne
@JoinColumn(name = "resort_id")
val skiResort: SkiResort
) : BaseEntity()

enum class DifficultyLevel {
초급, 중급, 중상급, 상급, 최상급, 파크
}
17 changes: 17 additions & 0 deletions src/main/kotlin/nexters/weski/slope/SlopeController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package nexters.weski.slope

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/slopes")
class SlopeController(
private val slopeService: SlopeService
) {
@GetMapping("/{resortId}")
fun getSlopesAndWebcams(@PathVariable resortId: Long): SlopeResponseDto {
return slopeService.getSlopesAndWebcams(resortId)
}
}
25 changes: 25 additions & 0 deletions src/main/kotlin/nexters/weski/slope/SlopeDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nexters.weski.slope

data class SlopeDto(
val name: String,
val difficulty: String,
val isDayOperating: Boolean,
val isNightOperating: Boolean,
val isLateNightOperating: Boolean,
val isDawnOperating: Boolean,
val isMidnightOperating: Boolean
) {
companion object {
fun fromEntity(entity: Slope): SlopeDto {
return SlopeDto(
name = entity.name,
difficulty = entity.difficulty.name,
isDayOperating = entity.isDayOperating,
isNightOperating = entity.isNightOperating,
isLateNightOperating = entity.isLateNightOperating,
isDawnOperating = entity.isDawnOperating,
isMidnightOperating = entity.isMidnightOperating
)
}
}
}
7 changes: 7 additions & 0 deletions src/main/kotlin/nexters/weski/slope/SlopeRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nexters.weski.slope

import org.springframework.data.jpa.repository.JpaRepository

interface SlopeRepository : JpaRepository<Slope, Long> {
fun findAllBySkiResortResortId(resortId: Long): List<Slope>
}
33 changes: 33 additions & 0 deletions src/main/kotlin/nexters/weski/slope/SlopeResponseDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package nexters.weski.slope

import nexters.weski.ski_resort.SkiResort
import nexters.weski.webcam.Webcam
import nexters.weski.webcam.WebcamDto

data class SlopeResponseDto(
val dayOperatingHours: String?,
val nightOperatingHours: String?,
val lateNightOperatingHours: String?,
val dawnOperatingHours: String?,
val midnightOperatingHours: String?,
val slopes: List<SlopeDto>,
val webcams: List<WebcamDto>
) {
companion object {
fun fromEntities(
skiResort: SkiResort,
slopes: List<Slope>,
webcams: List<Webcam>
): SlopeResponseDto {
return SlopeResponseDto(
dayOperatingHours = skiResort.dayOperatingHours,
nightOperatingHours = skiResort.nightOperatingHours,
lateNightOperatingHours = skiResort.lateNightOperatingHours,
dawnOperatingHours = skiResort.dawnOperatingHours,
midnightOperatingHours = skiResort.midnightOperatingHours,
slopes = slopes.map { SlopeDto.fromEntity(it) },
webcams = webcams.map { WebcamDto.fromEntity(it) }
)
}
}
}
20 changes: 20 additions & 0 deletions src/main/kotlin/nexters/weski/slope/SlopeService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nexters.weski.slope

import nexters.weski.ski_resort.SkiResortRepository
import nexters.weski.webcam.WebcamRepository
import org.springframework.stereotype.Service

@Service
class SlopeService(
private val skiResortRepository: SkiResortRepository,
private val slopeRepository: SlopeRepository,
private val webcamRepository: WebcamRepository
) {
fun getSlopesAndWebcams(resortId: Long): SlopeResponseDto {
val skiResort = skiResortRepository.findById(resortId).orElseThrow { Exception("Resort not found") }
val slopes = slopeRepository.findAllBySkiResortResortId(resortId)
val webcams = webcamRepository.findAllBySkiResortResortId(resortId)

return SlopeResponseDto.fromEntities(skiResort, slopes, webcams)
}
}
22 changes: 22 additions & 0 deletions src/main/kotlin/nexters/weski/webcam/Webcam.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nexters.weski.webcam

import jakarta.persistence.*
import nexters.weski.common.BaseEntity
import nexters.weski.ski_resort.SkiResort

@Entity
@Table(name = "webcams")
data class Webcam(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0,

val name: String,
val number: Int,
val description: String?,
val url: String,

@ManyToOne
@JoinColumn(name = "resort_id")
val skiResort: SkiResort
) : BaseEntity()
19 changes: 19 additions & 0 deletions src/main/kotlin/nexters/weski/webcam/WebcamDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nexters.weski.webcam

data class WebcamDto(
val name: String,
val number: Int,
val description: String?,
val url: String?
) {
companion object {
fun fromEntity(entity: Webcam): WebcamDto {
return WebcamDto(
name = entity.name,
number = entity.number,
description = entity.description,
url = entity.url
)
}
}
}
7 changes: 7 additions & 0 deletions src/main/kotlin/nexters/weski/webcam/WebcamRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nexters.weski.webcam

import org.springframework.data.jpa.repository.JpaRepository

interface WebcamRepository : JpaRepository<Webcam, Long> {
fun findAllBySkiResortResortId(resortId: Long): List<Webcam>
}

0 comments on commit bf3a74c

Please sign in to comment.