-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
198 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/com/asap/asapbackend/batch/classroom/ClassroomInfoProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.asap.asapbackend.batch.classroom | ||
|
||
import com.asap.asapbackend.domain.classroom.domain.model.Classroom | ||
import com.asap.asapbackend.domain.school.domain.model.School | ||
|
||
interface ClassroomInfoProvider { | ||
fun retrieveClassroomInfo(batchSize: Int, startIndex: Int): ClassroomDataContainer | ||
|
||
data class ClassroomDataContainer( | ||
val classroomInfo: List<ClassroomInfo>, | ||
val hasNext: Boolean | ||
) | ||
|
||
data class ClassroomInfo( | ||
val school: School, | ||
val grade: Int, | ||
val classNumber: String | ||
) { | ||
fun toClassroom(): Classroom { | ||
return Classroom( | ||
school = school, | ||
grade = grade, | ||
className = classNumber | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun List<ClassroomInfoProvider.ClassroomInfo>.toClassrooms(): List<Classroom> { | ||
return this.map { it.toClassroom() } | ||
} |
61 changes: 0 additions & 61 deletions
61
src/main/kotlin/com/asap/asapbackend/batch/classroom/ClassroomOpenApiClient.kt
This file was deleted.
Oops, something went wrong.
25 changes: 18 additions & 7 deletions
25
src/main/kotlin/com/asap/asapbackend/batch/classroom/ClassroomScheduler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,27 @@ | ||
package com.asap.asapbackend.batch.classroom | ||
|
||
import com.asap.asapbackend.domain.classroom.domain.service.ClassroomAppender | ||
import com.asap.asapbackend.global.util.TransactionUtils | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.springframework.stereotype.Component | ||
|
||
@RestController | ||
@Component | ||
class ClassroomScheduler( | ||
private val classroomService: ClassroomService | ||
private val classroomInfoProvider: ClassroomInfoProvider, | ||
private val classroomAppender: ClassroomAppender | ||
) { | ||
@Scheduled(cron = "0 0 4 1 3 ?") // 매년 3월 1일 04:00:00에 실행 | ||
@GetMapping("/addClassroom") | ||
fun addClassroom(){ | ||
classroomService.addClassroom() | ||
fun addClassroom() { | ||
val batchSize = 100 | ||
var startIndex = 1 | ||
do { | ||
val classroomDataContainer = classroomInfoProvider.retrieveClassroomInfo(batchSize, startIndex) | ||
|
||
startIndex += batchSize | ||
|
||
TransactionUtils.writable { | ||
classroomAppender.addClassroom(classroomDataContainer.classroomInfo.toClassrooms()) | ||
} | ||
} while (classroomDataContainer.hasNext) | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
src/main/kotlin/com/asap/asapbackend/batch/classroom/ClassroomService.kt
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
src/main/kotlin/com/asap/asapbackend/batch/classroom/dto/ClassInfo.kt
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/main/kotlin/com/asap/asapbackend/batch/classroom/dto/ClassRoomInfo.kt
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
src/main/kotlin/com/asap/asapbackend/batch/classroom/dto/ClassroomResponse.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
src/main/kotlin/com/asap/asapbackend/batch/school/exception/NotFoundSchoolException.kt
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/com/asap/asapbackend/client/openapi/classroom/ClassroomOpenApiClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.asap.asapbackend.client.openapi.classroom | ||
|
||
import com.asap.asapbackend.batch.classroom.ClassroomInfoProvider | ||
import com.asap.asapbackend.client.openapi.classroom.dto.ClassroomOpenApiResponse | ||
import com.asap.asapbackend.domain.school.domain.repository.SchoolRepository | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import org.springframework.web.util.UriBuilder | ||
import java.time.Year | ||
|
||
@Component | ||
class ClassroomOpenApiClient( | ||
private val schoolRepository: SchoolRepository | ||
) : ClassroomInfoProvider { | ||
override fun retrieveClassroomInfo(batchSize: Int, startIndex: Int): ClassroomInfoProvider.ClassroomDataContainer { | ||
val endIndex = startIndex + batchSize | ||
val schools = schoolRepository.findAll().subList(startIndex, endIndex) | ||
val classroomInfoList = mutableListOf<ClassroomInfoProvider.ClassroomInfo>() | ||
schools.forEach { school -> | ||
val apiUrl = "https://open.neis.go.kr/hub/classInfo" | ||
val classroomInfoResult = WebClient.create(apiUrl).get() | ||
.uri { uriBuilder: UriBuilder -> | ||
uriBuilder | ||
.queryParam("KEY", "32e897d4054342b19fd68dfb1b9ba621") | ||
.queryParam("ATPT_OFCDC_SC_CODE", school.eduOfficeCode) | ||
.queryParam("SD_SCHUL_CODE", school.schoolCode) | ||
.queryParam("AY", Year.now()) | ||
.queryParam("Type", "json") | ||
.build() | ||
} | ||
.retrieve() | ||
.bodyToMono(String::class.java) | ||
.map { | ||
jacksonObjectMapper().readValue(it, ClassroomOpenApiResponse::class.java) | ||
} | ||
.block() | ||
val classroomInfo = classroomInfoResult?.classInfo?.firstOrNull() | ||
classroomInfo?.row?.forEach { row -> | ||
classroomInfoList.add(row.toClassroomInfo(school)) | ||
} | ||
} | ||
val hasNext = schoolRepository.count() > endIndex | ||
|
||
return ClassroomInfoProvider.ClassroomDataContainer( | ||
classroomInfo = classroomInfoList, | ||
hasNext = hasNext | ||
) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...main/kotlin/com/asap/asapbackend/client/openapi/classroom/dto/ClassroomOpenApiResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.asap.asapbackend.client.openapi.classroom.dto | ||
|
||
import com.asap.asapbackend.batch.classroom.ClassroomInfoProvider | ||
import com.asap.asapbackend.domain.school.domain.model.School | ||
|
||
data class ClassroomOpenApiResponse( | ||
val classInfo: List<ClassInfo> | ||
) | ||
|
||
data class ClassInfo( | ||
val head: List<Head>?, | ||
val row: List<Row>? | ||
) | ||
|
||
data class Head( | ||
val list_total_count: Int? | ||
) | ||
|
||
data class Row( | ||
val ATPT_OFCDC_SC_CODE: String?, | ||
val ATPT_OFCDC_SC_NM: String?, | ||
val SD_SCHUL_CODE: String?, | ||
val SCHUL_NM: String?, | ||
val AY: String?, | ||
val GRADE: Int, | ||
val DGHT_CRSE_SC_NM: String?, | ||
val SCHUL_CRSE_SC_NM: String?, | ||
val ORD_SC_NM: String?, | ||
val DDDEP_NM: String?, | ||
val CLASS_NM: String, | ||
val LOAD_DTM: String? | ||
) { | ||
fun toClassroomInfo(school: School): ClassroomInfoProvider.ClassroomInfo { | ||
return ClassroomInfoProvider.ClassroomInfo( | ||
school, | ||
GRADE, | ||
CLASS_NM | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.