Skip to content

Commit

Permalink
Merge pull request #243 from catenax-ng/fix/api_wrong_schema_for_erro…
Browse files Browse the repository at this point in the history
…r_response

fix(api): #222 Wrong Schema for Entities with Error Response
  • Loading branch information
nicoprow authored Jun 6, 2023
2 parents 205f8f3 + a23bfa2 commit 4432563
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,72 @@ package org.eclipse.tractusx.bpdm.pool.api.model.response
import io.swagger.v3.oas.annotations.media.Schema
import org.eclipse.tractusx.bpdm.common.dto.response.LogisticAddressResponse

@Schema(name = "EntitiesWithErrorsResponse", description = "Holds information about successfully and failed entities after the creating/updating of several objects")

data class EntitiesWithErrors<ENTITY, out ERROR : ErrorCode>(
open class EntitiesWithErrors<ENTITY, out ERROR : ErrorCode>(

@Schema(description = "Successfully created entities")
val entities: Collection<ENTITY>,
open val entities: Collection<ENTITY>,
@Schema(description = "Errors for not created entities")
val errors: Collection<ErrorInfo<ERROR>>
open val errors: Collection<ErrorInfo<ERROR>>
) {
val entityCount: Int
get() = entities.size
val errorCount: Int
get() = errors.size
}

typealias LegalEntityPartnerCreateResponseWrapper = EntitiesWithErrors<LegalEntityPartnerCreateResponse, LegalEntityCreateError>
typealias LegalEntityPartnerUpdateResponseWrapper = EntitiesWithErrors<LegalEntityPartnerCreateResponse, LegalEntityUpdateError>
typealias SitePartnerCreateResponseWrapper = EntitiesWithErrors<SitePartnerCreateResponse, SiteCreateError>
typealias SitePartnerUpdateResponseWrapper = EntitiesWithErrors<SitePartnerCreateResponse, SiteUpdateError>
typealias AddressPartnerCreateResponseWrapper = EntitiesWithErrors<AddressPartnerCreateResponse, AddressCreateError>
typealias AddressPartnerUpdateResponseWrapper = EntitiesWithErrors<LogisticAddressResponse, AddressUpdateError>
@Schema(
name = "LegalEntityCreateWrapper",
description = "Holds information about successfully and failed entities after the creating/updating of several objects"
)
data class LegalEntityPartnerCreateResponseWrapper(
override val entities: Collection<LegalEntityPartnerCreateResponse>,
override val errors: Collection<ErrorInfo<LegalEntityCreateError>>
) : EntitiesWithErrors<LegalEntityPartnerCreateResponse, LegalEntityCreateError>(entities, errors)

@Schema(
name = "LegalEntityUpdateWrapper",
description = "Holds information about successfully and failed entities after the creating/updating of several objects"
)
data class LegalEntityPartnerUpdateResponseWrapper(
override val entities: Collection<LegalEntityPartnerCreateResponse>,
override val errors: Collection<ErrorInfo<LegalEntityUpdateError>>
) : EntitiesWithErrors<LegalEntityPartnerCreateResponse, LegalEntityUpdateError>(entities, errors)

@Schema(
name = "SiteCreateWrapper",
description = "Holds information about successfully and failed entities after the creating/updating of several objects"
)
data class SitePartnerCreateResponseWrapper(
override val entities: Collection<SitePartnerCreateResponse>,
override val errors: Collection<ErrorInfo<SiteCreateError>>
) : EntitiesWithErrors<SitePartnerCreateResponse, SiteCreateError>(entities, errors)

@Schema(
name = "SiteUpdateWrapper",
description = "Holds information about successfully and failed entities after the creating/updating of several objects"
)
data class SitePartnerUpdateResponseWrapper(
override val entities: Collection<SitePartnerCreateResponse>,
override val errors: Collection<ErrorInfo<SiteUpdateError>>
) : EntitiesWithErrors<SitePartnerCreateResponse, SiteUpdateError>(entities, errors)

@Schema(
name = "AddressCreateWrapper",
description = "Holds information about successfully and failed entities after the creating/updating of several objects"
)
data class AddressPartnerCreateResponseWrapper(
override val entities: Collection<AddressPartnerCreateResponse>,
override val errors: Collection<ErrorInfo<AddressCreateError>>
) : EntitiesWithErrors<AddressPartnerCreateResponse, AddressCreateError>(entities, errors)

@Schema(
name = "AddressUpdateWrapper",
description = "Holds information about successfully and failed entities after the creating/updating of several objects"
)
data class AddressPartnerUpdateResponseWrapper(
override val entities: Collection<LogisticAddressResponse>,
override val errors: Collection<ErrorInfo<AddressUpdateError>>
) : EntitiesWithErrors<LogisticAddressResponse, AddressUpdateError>(entities, errors)



Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class BusinessPartnerBuildService(

val validEntities = legalEntities.map { it.toUpsertDto(legalEntityWithIndexByBpnMap[it.bpn]!!.second) }

return EntitiesWithErrors(validEntities, errors)
return LegalEntityPartnerCreateResponseWrapper(validEntities, errors)
}

@Transactional
Expand Down Expand Up @@ -125,7 +125,7 @@ class BusinessPartnerBuildService(

val validEntities = sites.map { it.toUpsertDto(siteWithIndexByBpnMap[it.bpn]!!.second) }

return EntitiesWithErrors(validEntities, errors)
return SitePartnerCreateResponseWrapper(validEntities, errors)
}

@Transactional
Expand All @@ -150,7 +150,7 @@ class BusinessPartnerBuildService(

changelogService.createChangelogEntries(addressResponses.map { ChangelogEntryDto(it.address.bpna, ChangelogType.CREATE, ChangelogSubject.ADDRESS) })

return EntitiesWithErrors(addressResponses, errors)
return AddressPartnerCreateResponseWrapper(addressResponses, errors)
}

/**
Expand Down Expand Up @@ -184,7 +184,7 @@ class BusinessPartnerBuildService(

val validEntities = legalEntityRepository.saveAll(legalEntities).map { it.toUpsertDto(null) }

return EntitiesWithErrors(validEntities, errors)
return LegalEntityPartnerUpdateResponseWrapper(validEntities, errors)
}

@Transactional
Expand Down Expand Up @@ -212,7 +212,7 @@ class BusinessPartnerBuildService(
}
val validEntities = siteRepository.saveAll(sites).map { it.toUpsertDto(null) }

return EntitiesWithErrors(validEntities, errors)
return SitePartnerUpdateResponseWrapper(validEntities, errors)
}

fun updateAddresses(requests: Collection<AddressPartnerUpdateRequest>): AddressPartnerUpdateResponseWrapper {
Expand All @@ -233,7 +233,7 @@ class BusinessPartnerBuildService(
changelogService.createChangelogEntries(validAddresses.map { ChangelogEntryDto(it.bpn, ChangelogType.UPDATE, ChangelogSubject.ADDRESS) })

val addressResponses = logisticAddressRepository.saveAll(validAddresses).map { it.toDto() }
return EntitiesWithErrors(addressResponses, errors)
return AddressPartnerUpdateResponseWrapper(addressResponses, errors)
}

@Transactional
Expand Down

0 comments on commit 4432563

Please sign in to comment.