Skip to content

Commit

Permalink
Merge pull request #552 from School-of-Company/549-add/inputstream-close
Browse files Browse the repository at this point in the history
# 549 InputStream close
  • Loading branch information
JuuuuHong authored Aug 12, 2024
2 parents 0ac0e25 + fe1551e commit 3f1ca16
Showing 1 changed file with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import team.msg.domain.user.model.User
import team.msg.domain.user.presentation.data.response.UserResponse
import team.msg.domain.user.presentation.data.response.UsersResponse
import team.msg.domain.user.repository.UserRepository
import team.msg.global.exception.InternalServerException
import java.util.*

@Service
Expand Down Expand Up @@ -115,35 +114,37 @@ class AdminServiceImpl(
*/
@Transactional(rollbackFor = [Exception::class])
override fun uploadStudentListExcel(file: MultipartFile) {
val workbook = WorkbookFactory.create(file.inputStream)
file.inputStream.use {
val workbook = WorkbookFactory.create(file.inputStream)

val sheet = workbook.getSheetAt(0)
val sheet = workbook.getSheetAt(0)

sheet.forEachIndexed { index, row ->
if (index == 0)
return@forEachIndexed
sheet.forEachIndexed { index, row ->
if (index == 0)
return@forEachIndexed

if (row.getCell(0).stringCellValue == "")
return
if (row.getCell(0).stringCellValue == "")
return

val email = row.getCell(0).stringCellValue
val name = row.getCell(1).stringCellValue
val phoneNumber = row.getCell(2).stringCellValue
val password = row.getCell(3).stringCellValue
val clubName = row.getCell(4).stringCellValue
val grade = row.getCell(5).numericCellValue.toInt()
val classRoom = row.getCell(6).numericCellValue.toInt()
val number = row.getCell(7).numericCellValue.toInt()
val admissionNumber = row.getCell(8).numericCellValue.toInt()
val subscriptionGrade = row.getCell(9).numericCellValue.toInt()
val email = row.getCell(0).stringCellValue
val name = row.getCell(1).stringCellValue
val phoneNumber = row.getCell(2).stringCellValue
val password = row.getCell(3).stringCellValue
val clubName = row.getCell(4).stringCellValue
val grade = row.getCell(5).numericCellValue.toInt()
val classRoom = row.getCell(6).numericCellValue.toInt()
val number = row.getCell(7).numericCellValue.toInt()
val admissionNumber = row.getCell(8).numericCellValue.toInt()
val subscriptionGrade = row.getCell(9).numericCellValue.toInt()

validateExcelStudentData(email, phoneNumber, password)
validateExcelStudentData(email, phoneNumber, password)

val user = userUtil.createUser(email, name, phoneNumber, password, Authority.ROLE_STUDENT)
val user = userUtil.createUser(email, name, phoneNumber, password, Authority.ROLE_STUDENT)

val club = clubRepository findByName clubName
val club = clubRepository findByName clubName

studentUtil.createStudent(user, club, grade, classRoom, number, admissionNumber, subscriptionGrade)
studentUtil.createStudent(user, club, grade, classRoom, number, admissionNumber, subscriptionGrade)
}
}
}

Expand Down

0 comments on commit 3f1ca16

Please sign in to comment.