@@ -3,6 +3,7 @@ package dsm.pick2024.domain.attendance.domain.service
33import dsm.pick2024.domain.application.enums.ApplicationType
44import dsm.pick2024.domain.attendance.domain.Attendance
55import dsm.pick2024.domain.attendance.enums.AttendanceStatus
6+ import dsm.pick2024.domain.attendance.exception.InvalidPeriodException
67import org.springframework.stereotype.Component
78import java.time.LocalTime
89
@@ -14,7 +15,7 @@ class AttendanceService {
1415 LocalTime .of(8 , 40 ) to LocalTime .of(9 , 40 ), // 1교시
1516 LocalTime .of(9 , 40 ) to LocalTime .of(10 , 40 ), // 2교시
1617 LocalTime .of(10 , 40 ) to LocalTime .of(11 , 40 ), // 3교시
17- LocalTime .of(12 , 40 ) to LocalTime .of(13 , 30 ), // 4교시
18+ LocalTime .of(11 , 40 ) to LocalTime .of(13 , 30 ), // 4교시
1819 LocalTime .of(13 , 30 ) to LocalTime .of(14 , 40 ), // 5교시
1920 LocalTime .of(14 , 30 ) to LocalTime .of(15 , 30 ), // 6교시
2021 LocalTime .of(15 , 30 ) to LocalTime .of(16 , 30 ), // 7교시
@@ -29,17 +30,70 @@ class AttendanceService {
2930 }
3031
3132 // 교시 혹은 시간을 기반으로 교시 목록을 반환하는 함수
32- fun translateApplication (start : String , end : String? , applicationType : ApplicationType ): List < String > {
33+ fun translateApplication (start : String , end : String , applicationType : ApplicationType ): Pair < String , String > {
3334 return when (applicationType) {
34- ApplicationType .PERIOD -> listOf (start, end!! )
35+ ApplicationType .PERIOD -> Pair (start, end)
3536 ApplicationType .TIME -> {
3637 val startTime = LocalTime .parse(start)
37- val endTime = end?. let { LocalTime .parse(it) }
38+ val endTime = LocalTime .parse(end)
3839 getMatchPeriods(startTime, endTime)
3940 }
4041 }
4142 }
4243
44+ fun checkApplicationTime (applicationType : ApplicationType , start : String , end : String ) {
45+ when (applicationType) {
46+ ApplicationType .TIME -> {
47+ val startTime = LocalTime .parse(start)
48+ val endTime = LocalTime .parse(end)
49+ if (startTime > endTime ||
50+ endTime > LocalTime .of(20 , 30 ) ||
51+ startTime < LocalTime .of(8 , 30 )
52+ ) {
53+ throw InvalidPeriodException
54+ }
55+ }
56+ ApplicationType .PERIOD -> {
57+ val startPeriod = start.replace(" 교시" , " " ).toIntOrNull() ? : throw InvalidPeriodException
58+ val endPeriod = end.replace(" 교시" , " " ).toIntOrNull() ? : throw InvalidPeriodException
59+ if (startPeriod > endPeriod || startPeriod < 1 || endPeriod > 10 ) {
60+ throw InvalidPeriodException
61+ }
62+ }
63+ }
64+ }
65+
66+ fun checkEarlyReturnTime (applicationType : ApplicationType , start : String ) {
67+ when (applicationType) {
68+ ApplicationType .TIME -> {
69+ val startTime = LocalTime .parse(start)
70+ if (startTime < LocalTime .of(8 , 30 )) {
71+ throw InvalidPeriodException
72+ }
73+ }
74+ ApplicationType .PERIOD -> {
75+ val startPeriod = start.replace(" 교시" , " " ).toInt()
76+ if (startPeriod < 1 || startPeriod > 10 ) {
77+ throw InvalidPeriodException
78+ }
79+ }
80+ }
81+ }
82+
83+ fun translateEarlyReturn (start : String , applicationType : ApplicationType ): String {
84+ return when (applicationType) {
85+ ApplicationType .PERIOD -> start
86+ ApplicationType .TIME -> {
87+ val startTime = LocalTime .parse(start)
88+ val startIndex = periods.indexOfFirst { (start, endAt) ->
89+ startTime >= start && startTime < endAt
90+ }
91+ if (startIndex == - 1 ) throw InvalidPeriodException
92+ periodNames[startIndex]
93+ }
94+ }
95+ }
96+
4397 // 주어진 교시 혹은 시간에 해당하는 출석 상태를 업데이트하는 함수
4498 fun updateAttendanceToApplication (
4599 start : String ,
@@ -113,27 +167,16 @@ class AttendanceService {
113167 return updateAttendance
114168 }
115169
116- fun translateEarlyReturn (start : String , applicationType : ApplicationType ): List <String > {
117- return when (applicationType) {
118- ApplicationType .PERIOD -> {
119- val startIndex = periodNames.indexOf(start).coerceAtLeast(0 )
120- periodNames.subList(startIndex, periodNames.size)
121- }
122- ApplicationType .TIME -> {
123- val startTime = LocalTime .parse(start)
124- getMatchPeriods(startTime, null )
125- }
170+ private fun getMatchPeriods (startTime : LocalTime , endTime : LocalTime ): Pair <String , String > {
171+ val startIndex = periods.indexOfFirst { (start, endAt) ->
172+ startTime in start.. endAt
173+ }
174+ val endIndex = periods.indexOfFirst { (start, endAt) ->
175+ endTime in start.. endAt
126176 }
127- }
128177
129- private fun getMatchPeriods (startTime : LocalTime , endTime : LocalTime ? ): List <String > {
130- return periods
131- .mapIndexed { index, period ->
132- if ((startTime < period.second) && (endTime == null || endTime > period.first)) {
133- periodNames[index]
134- } else {
135- null
136- }
137- }.filterNotNull()
178+ if (startIndex == - 1 || endIndex == - 1 ) throw InvalidPeriodException
179+
180+ return periodNames[startIndex] to periodNames[endIndex]
138181 }
139182}
0 commit comments