Skip to content

Commit 6f0f6d3

Browse files
committed
✨ Add: slope operation update batch and API
#32
1 parent 8c0ac0b commit 6f0f6d3

File tree

9 files changed

+163
-8
lines changed

9 files changed

+163
-8
lines changed

src/main/kotlin/nexters/weski/batch/ResortBatchController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestBody
1010
import org.springframework.web.bind.annotation.RestController
1111

1212

13-
@Tag(name = "스키장 개장일/폐장일 업데이트 API", description = "스키장 개장일/폐장일을 업데이트")
13+
@Tag(name = "스키장 데이터 업데이트 API", description = "스키장 데이터를 업데이트")
1414
@RestController
1515
class ResortBatchController(
1616
private val resortService: SkiResortService

src/main/kotlin/nexters/weski/batch/ResortStatusUpdateScheduler.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ class ResortStatusUpdateScheduler(
1212
fun scheduleResortStatusUpdate() {
1313
skiResortService.updateSkiResortStatus()
1414
}
15+
16+
@Scheduled(cron = "30 0 0 * * ?")
17+
fun scheduleResortSlopeCountUpdate() {
18+
skiResortService.updateSkiResortSlopeCount()
19+
}
1520
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package nexters.weski.batch
2+
3+
import io.swagger.v3.oas.annotations.Operation
4+
import io.swagger.v3.oas.annotations.media.Content
5+
import io.swagger.v3.oas.annotations.media.Schema
6+
import io.swagger.v3.oas.annotations.tags.Tag
7+
import nexters.weski.ski_resort.SkiResortService
8+
import nexters.weski.slope.SlopeService
9+
import org.springframework.web.bind.annotation.PostMapping
10+
import org.springframework.web.bind.annotation.RequestBody
11+
import org.springframework.web.bind.annotation.RestController
12+
13+
@Tag(name = "슬로프 데이터 업데이트 API", description = "스키장 슬로프 데이터를 업데이트")
14+
@RestController
15+
class SlopeBatchController(
16+
private val slopeService: SlopeService,
17+
private val resortService: SkiResortService
18+
) {
19+
@Operation(
20+
summary = "스키장 슬로프 운영 현황 업데이트 API",
21+
description = """
22+
스키장 id, 슬로프 이름, 주간/야간/심야/새벽/자정, 운영 여부를 파라미터로 전달해서 업데이트합니다.
23+
resortId는 다음과 같습니다.
24+
1, 지산 리조트
25+
2, 곤지암 스키장
26+
3, 비발디파크
27+
4, 엘리시안 강촌
28+
5, 웰리힐리파크
29+
6, 휘닉스파크
30+
7, 하이원 스키장
31+
8, 용평스키장 모나
32+
9, 무주덕유산
33+
10, 에덴벨리(양산)
34+
11, 오투리조트
35+
"""
36+
)
37+
@PostMapping("/batch/slope-status")
38+
fun updateSlopeStatus(
39+
@RequestBody
40+
@io.swagger.v3.oas.annotations.parameters.RequestBody(
41+
description = "slopes 운영여부 업데이트 요청",
42+
required = true,
43+
content = [Content(
44+
mediaType = "application/json",
45+
schema = Schema(implementation = SlopeDateUpdateRequest::class)
46+
)]
47+
)
48+
request: SlopeDateUpdateRequest
49+
) {
50+
slopeService.updateSlopeOpeningStatus(
51+
resortId = request.resortId,
52+
slopeName = request.slopeName,
53+
timeType = request.timeType,
54+
isOpen = request.isOpen
55+
)
56+
}
57+
58+
@Operation(
59+
summary = "스키장 슬로프 count 업데이트",
60+
description = "스키장의 슬로프 count가 업데이트됩니다."
61+
)
62+
@PostMapping("/batch/resort-slope-count")
63+
fun updateSlopeCount() {
64+
resortService.updateSkiResortSlopeCount()
65+
}
66+
67+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package nexters.weski.batch
2+
3+
import io.swagger.v3.oas.annotations.media.Schema
4+
import java.time.LocalDate
5+
6+
class SlopeDateUpdateRequest (
7+
@Schema(
8+
description = "스키장 ID",
9+
example = "1"
10+
)
11+
val resortId: Long,
12+
13+
@Schema(
14+
description = "슬로프 이름",
15+
example = "레몬1"
16+
)
17+
val slopeName: String,
18+
19+
@Schema(
20+
description = "주간/야간/심야/새벽/자정 중 하나",
21+
example = "주간"
22+
)
23+
val timeType: String,
24+
25+
@Schema(
26+
description = "운영 여부(Y/N)",
27+
example = "Y"
28+
)
29+
val isOpen: String,
30+
)

src/main/kotlin/nexters/weski/ski_resort/SkiResortController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.GetMapping
77
import org.springframework.web.bind.annotation.PathVariable
88
import org.springframework.web.bind.annotation.RestController
99

10-
@Tag(name = "전체 스키장 정보 API", description = "전체 스키장 날씨 및 슬로프 정보 관련")
10+
@Tag(name = "스키장 정보 API", description = "전체 스키장 날씨 및 슬로프 정보 관련")
1111
@RestController
1212
class SkiResortController(
1313
private val skiResortService: SkiResortService

src/main/kotlin/nexters/weski/ski_resort/SkiResortService.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nexters.weski.ski_resort
22

33
import nexters.weski.batch.DateType
4+
import nexters.weski.slope.SlopeService
45
import nexters.weski.weather.CurrentWeatherRepository
56
import nexters.weski.weather.DailyWeatherRepository
67
import org.springframework.stereotype.Service
@@ -10,7 +11,8 @@ import java.time.LocalDate
1011
class SkiResortService(
1112
private val skiResortRepository: SkiResortRepository,
1213
private val currentWeatherRepository: CurrentWeatherRepository,
13-
private val dailyWeatherRepository: DailyWeatherRepository
14+
private val dailyWeatherRepository: DailyWeatherRepository,
15+
private val slopeService: SlopeService
1416
) {
1517
fun getAllSkiResortsAndWeather(): List<SkiResortResponseDto> {
1618
val skiResorts = skiResortRepository.findAllByOrderByOpeningDateAsc()
@@ -63,4 +65,13 @@ class SkiResortService(
6365
}
6466
}
6567

68+
fun updateSkiResortSlopeCount() {
69+
val skiResorts = skiResortRepository.findAll()
70+
skiResorts.forEach { skiResort ->
71+
val totalSlopeCount = slopeService.getTotalSlopeCount(skiResort.resortId)
72+
val openingSlopeCount = slopeService.getOpeningSlopeCount(skiResort.resortId)
73+
val updatedSkiResort = skiResort.copy(totalSlopes = totalSlopeCount, openSlopes = openingSlopeCount)
74+
skiResortRepository.save(updatedSkiResort)
75+
}
76+
}
6677
}

src/main/kotlin/nexters/weski/slope/Slope.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ data class Slope(
1515
@Enumerated(EnumType.STRING)
1616
val difficulty: DifficultyLevel,
1717

18-
val isDayOperating: Boolean = false,
19-
val isNightOperating: Boolean = false,
20-
val isLateNightOperating: Boolean = false,
21-
val isDawnOperating: Boolean = false,
22-
val isMidnightOperating: Boolean = false,
18+
var isDayOperating: Boolean = false,
19+
var isNightOperating: Boolean = false,
20+
var isLateNightOperating: Boolean = false,
21+
var isDawnOperating: Boolean = false,
22+
var isMidnightOperating: Boolean = false,
2323

2424
@ManyToOne
2525
@JoinColumn(name = "resort_id")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
package nexters.weski.slope
22

33
import org.springframework.data.jpa.repository.JpaRepository
4+
import org.springframework.data.jpa.repository.Query
45

56
interface SlopeRepository : JpaRepository<Slope, Long> {
67
fun findAllBySkiResortResortId(resortId: Long): List<Slope>
8+
fun countBySkiResortResortId(resortId: Long): Int
9+
fun findBySkiResortResortIdAndName(resortId: Long, name: String): Slope?
10+
11+
@Query("""
12+
SELECT COUNT(s)
13+
FROM Slope s
14+
WHERE s.skiResort.resortId = :resortId
15+
AND (s.isDayOperating = true
16+
OR s.isNightOperating = true
17+
OR s.isLateNightOperating = true
18+
OR s.isDawnOperating = true
19+
OR s.isMidnightOperating = true)
20+
""")
21+
fun countOperatingSlopesByResortId(resortId: Long): Int
722
}

src/main/kotlin/nexters/weski/slope/SlopeService.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,31 @@ class SlopeService(
1717

1818
return SlopeResponseDto.fromEntities(skiResort, slopes, webcams)
1919
}
20+
fun getTotalSlopeCount(resortId: Long): Int {
21+
return slopeRepository.countBySkiResortResortId(resortId)
22+
}
23+
24+
fun getOpeningSlopeCount(resortId: Long): Int {
25+
return slopeRepository.countOperatingSlopesByResortId(resortId)
26+
}
27+
28+
fun updateSlopeOpeningStatus(
29+
resortId: Long,
30+
slopeName: String,
31+
timeType: String,
32+
isOpen: String
33+
) {
34+
val slope = slopeRepository.findBySkiResortResortIdAndName(resortId, slopeName)
35+
?: throw Exception("Slope not found")
36+
37+
when (timeType) {
38+
"주간" -> slope.isDayOperating = isOpen == "Y"
39+
"야간" -> slope.isNightOperating = isOpen == "Y"
40+
"심야" -> slope.isLateNightOperating = isOpen == "Y"
41+
"새벽" -> slope.isDawnOperating = isOpen == "Y"
42+
"자정" -> slope.isMidnightOperating = isOpen == "Y"
43+
else -> throw Exception("Invalid time type")
44+
}
45+
slopeRepository.save(slope)
46+
}
2047
}

0 commit comments

Comments
 (0)