I have this class: package com.manomano.catalog.pim.product.projection.application…
- User
I have this class: package com.manomano.catalog.pim.product.projection.application
import com.manomano.catalog.pim.product.projection.domain.MasterProductId import com.manomano.catalog.pim.product.projection.domain.MasterProductIdPreset import com.manomano.catalog.pim.product.projection.domain.Mmid import com.manomano.catalog.pim.product.projection.domain.MmidPreset import com.manomano.catalog.pim.product.projection.domain.PresetType import com.manomano.catalog.pim.product.projection.domain.ProductPresetsRepository import org.springframework.stereotype.Component import java.io.InputStream import java.util.UUID
const val MAX_PRESETS_PER_USER = 10
@Component class UploadPresetFile( private val productPresetRepository: ProductPresetsRepository, ) { fun execute(fileName: String, fileContent: InputStream, userId: UUID, type: PresetType): UploadPresetFileResult { if (productPresetRepository.countUserPresets(userId) >= MAX_PRESETS_PER_USER) { return UploadPresetFileResult.TooManyPresets }
return when (type) { PresetType.MMID -> { when (val validationResult = fileContent.toValidPreset( ::Mmid )) { is PresetValidationResult.Success<Mmid> -> { val preset = MmidPreset(fileName, validationResult.ids) UploadPresetFileResult.Success(productPresetRepository.saveMmidPreset(preset, userId)) } is PresetValidationResult.Failure -> UploadPresetFileResult.InvalidContent(validationResult.errors) is PresetValidationResult.Emptyfile -> UploadPresetFileResult.EmptyFile } } PresetType.MASTER_PRODUCT_ID -> { when (val validationResult = fileContent.toValidPreset( ::MasterProductId )) { is PresetValidationResult.Success<MasterProductId> -> { val preset = MasterProductIdPreset(fileName, validationResult.ids) UploadPresetFileResult.Success(productPresetRepository.saveMasterProductIdPreset(preset, userId)) } is PresetValidationResult.Failure -> UploadPresetFileResult.InvalidContent(validationResult.errors) is PresetValidationResult.Emptyfile -> UploadPresetFileResult.EmptyFile } } } } fun <T> InputStream.toValidPreset( idCreator: (Long) -> T ): PresetValidationResult<T> { val errors = mutableListOf<ErrorLine>() val ids = mutableSetOf<T>() bufferedReader().use { reader -> reader.lineSequence().forEachIndexed { index, line -> if (line.isNotBlank()) { val idValue = line.toLongOrNull() if (idValue == null) { if (index > 0) { errors += ErrorLine(index + 1, ValidationError.INVALID_ID) } } else { ids += idCreator(idValue) } } } } if (ids.isEmpty() && errors.isEmpty()) { return PresetValidationResult.Emptyfile("Empty file") } return if (errors.isNotEmpty()) { PresetValidationResult.Failure(errors) } else { PresetValidationResult.Success(ids) } } - Assistant