Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/kotlin/io/billie/countries/data/CityRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ class CityRepository {
)
}

@Transactional(readOnly = true)
fun findByNameAndCountryCode(name: String, countryCode: String): CityResponse {
return jdbcTemplate.query(
"select id, name, country_code from organisations_schema.cities where name = ? and country_code = ?",
cityResponseMapper(),
name,
countryCode
).firstOrNull() ?: throw UnableToFindCity(name, countryCode)
}

private fun cityResponseMapper() = RowMapper<CityResponse> { it: ResultSet, _: Int ->
CityResponse(
it.getObject("id", UUID::class.java),
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/io/billie/countries/data/UnableToFindCity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.billie.countries.data

class UnableToFindCity(val name: String, val countryCode: String) : RuntimeException()
2 changes: 2 additions & 0 deletions src/main/kotlin/io/billie/countries/service/CountryService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ class CountryService(val dbCountry: CountryRepository, val dbCity: CityRepositor
}
fun findCities(countryCode: String): List<CityResponse> = dbCity.findByCountryCode(countryCode)

fun findCity(name: String, countryCode: String): CityResponse = dbCity.findByNameAndCountryCode(name, countryCode)

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package io.billie.organisations.data

import io.billie.countries.model.CountryResponse
import io.billie.organisations.viewmodel.*
import io.billie.organisations.viewmodel.Address
import io.billie.organisations.viewmodel.AddressRequest
import io.billie.organisations.viewmodel.ContactDetails
import io.billie.organisations.viewmodel.ContactDetailsRequest
import io.billie.organisations.viewmodel.LegalEntityType
import io.billie.organisations.viewmodel.OrganisationRequest
import io.billie.organisations.viewmodel.OrganisationResponse
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.ResultSetExtractor
Expand All @@ -12,8 +18,7 @@ import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional
import java.sql.Date
import java.sql.ResultSet
import java.util.*

import java.util.UUID

@Repository
class OrganisationRepository {
Expand All @@ -27,12 +32,13 @@ class OrganisationRepository {
}

@Transactional
fun create(organisation: OrganisationRequest): UUID {
fun create(organisation: OrganisationRequest, addressCityId: UUID): UUID {
if(!valuesValid(organisation)) {
throw UnableToFindCountry(organisation.countryCode)
}
val id: UUID = createContactDetails(organisation.contactDetails)
return createOrganisation(organisation, id)
val contactDetailsId = createContactDetails(organisation.contactDetails)
val addressId = createAddress(organisation.address, addressCityId)
return createOrganisation(organisation, contactDetailsId, addressId)
}

private fun valuesValid(organisation: OrganisationRequest): Boolean {
Expand All @@ -47,7 +53,7 @@ class OrganisationRepository {
return (reply != null) && (reply > 0)
}

private fun createOrganisation(org: OrganisationRequest, contactDetailsId: UUID): UUID {
private fun createOrganisation(org: OrganisationRequest, contactDetailsId: UUID, addressId: UUID): UUID {
val keyHolder: KeyHolder = GeneratedKeyHolder()
jdbcTemplate.update(
{ connection ->
Expand All @@ -59,8 +65,9 @@ class OrganisationRepository {
"vat_number, " +
"registration_number, " +
"legal_entity_type, " +
"contact_details_id" +
") VALUES (?, ?, ?, ?, ?, ?, ?)",
"contact_details_id," +
"address_id" +
") VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
arrayOf("id")
)
ps.setString(1, org.name)
Expand All @@ -70,6 +77,7 @@ class OrganisationRepository {
ps.setString(5, org.registrationNumber)
ps.setString(6, org.legalEntityType.toString())
ps.setObject(7, contactDetailsId)
ps.setObject(8, addressId)
ps
}, keyHolder
)
Expand Down Expand Up @@ -99,6 +107,33 @@ class OrganisationRepository {
return keyHolder.getKeyAs(UUID::class.java)!!
}

private fun createAddress(address: AddressRequest, addressCityId: UUID): UUID {
val keyHolder: KeyHolder = GeneratedKeyHolder()
jdbcTemplate.update(
{ connection ->
val ps = connection.prepareStatement(
"insert into organisations_schema.addresses " +
"(" +
"city_id, " +
"street, " +
"house_number, " +
"postal_code, " +
"additional_info" +
") values(?,?,?,?,?)",
arrayOf("id")
)
ps.setObject(1, addressCityId)
ps.setString(2, address.street)
ps.setString(3, address.houseNumber)
ps.setString(4, address.postalCode)
ps.setString(5, address.additionalInfo)
ps
},
keyHolder
)
return keyHolder.getKeyAs(UUID::class.java)!!
}

private fun organisationQuery() = "select " +
"o.id as id, " +
"o.name as name, " +
Expand All @@ -112,11 +147,20 @@ class OrganisationRepository {
"o.contact_details_id as contact_details_id, " +
"cd.phone_number as phone_number, " +
"cd.fax as fax, " +
"cd.email as email " +
"cd.email as email, " +
"a.id as address_id, " +
"ct.country_code as address_country_code, " +
"ct.name as address_city, " +
"a.street as address_street, " +
"a.house_number as address_house_number, " +
"a.postal_code as address_postal_code, " +
"a.additional_info as address_additional_info " +
"from " +
"organisations_schema.organisations o " +
"INNER JOIN organisations_schema.contact_details cd on o.contact_details_id::uuid = cd.id::uuid " +
"INNER JOIN organisations_schema.countries c on o.country_code = c.country_code "
"INNER JOIN organisations_schema.countries c on o.country_code = c.country_code " +
"INNER JOIN organisations_schema.addresses a on o.address_id::uuid = a.id::uuid " +
"INNER JOIN organisations_schema.cities ct on a.city_id::uuid = ct.id::uuid"

private fun organisationMapper() = RowMapper<OrganisationResponse> { it: ResultSet, _: Int ->
OrganisationResponse(
Expand All @@ -127,7 +171,8 @@ class OrganisationRepository {
it.getString("vat_number"),
it.getString("registration_number"),
LegalEntityType.valueOf(it.getString("legal_entity_type")),
mapContactDetails(it)
mapContactDetails(it),
mapAddress(it)
)
}

Expand All @@ -148,4 +193,16 @@ class OrganisationRepository {
)
}

private fun mapAddress(it: ResultSet): Address {
return Address(
it.getObject("address_id", UUID::class.java),
it.getString("address_country_code"),
it.getString("address_city"),
it.getString("address_street"),
it.getString("address_house_number"),
it.getString("address_postal_code"),
it.getString("address_additional_info")
)
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package io.billie.organisations.service

import io.billie.countries.service.CountryService
import io.billie.organisations.data.OrganisationRepository
import io.billie.organisations.viewmodel.OrganisationRequest
import io.billie.organisations.viewmodel.OrganisationResponse
import org.springframework.stereotype.Service
import java.util.*

@Service
class OrganisationService(val db: OrganisationRepository) {
class OrganisationService(val db: OrganisationRepository, val countryService: CountryService) {

fun findOrganisations(): List<OrganisationResponse> = db.findOrganisations()

fun createOrganisation(organisation: OrganisationRequest): UUID {
return db.create(organisation)
val city = countryService.findCity(organisation.address.city, organisation.address.countryCode)
return db.create(organisation, city.id)
}

}
14 changes: 14 additions & 0 deletions src/main/kotlin/io/billie/organisations/viewmodel/Address.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.billie.organisations.viewmodel

import com.fasterxml.jackson.annotation.JsonProperty
import java.util.UUID

class Address(
val id: UUID,
@JsonProperty("country_code") val countryCode: String,
val city: String,
val street: String,
@JsonProperty("house_number") val houseNumber: String,
@JsonProperty("postal_code") val postalCode: String,
@JsonProperty("additional_info") val additionalInfo: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.billie.organisations.viewmodel

import com.fasterxml.jackson.annotation.JsonProperty

data class AddressRequest(
@JsonProperty("country_code") val countryCode: String,
val city: String,
val street: String,
@JsonProperty("house_number") val houseNumber: String,
@JsonProperty("postal_code") val postalCode: String,
@JsonProperty("additional_info") val additionalInfo: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ data class OrganisationRequest(
@JsonProperty("registration_number") val registrationNumber: String?,
@JsonProperty("legal_entity_type") val legalEntityType: LegalEntityType,
@JsonProperty("contact_details") val contactDetails: ContactDetailsRequest,
@JsonProperty("address") val address: AddressRequest
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ data class OrganisationResponse(
@JsonProperty("registration_number") val registrationNumber: String?,
@JsonProperty("legal_entity_type") val legalEntityType: LegalEntityType,
@JsonProperty("contact_details") val contactDetails: ContactDetails,
val address: Address
)
12 changes: 12 additions & 0 deletions src/main/resources/db/migration/V9__Add_address.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE IF NOT EXISTS organisations_schema.addresses
(
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
city_id UUID REFERENCES organisations_schema.cities(id),
street VARCHAR(255) NOT NULL,
house_number VARCHAR(255) NOT NULL,
postal_code VARCHAR(255) NOT NULL,
additional_info VARCHAR(255) NOT NULL
);

ALTER TABLE organisations_schema.organisations
ADD COLUMN address_id UUID REFERENCES organisations_schema.addresses(id);
Loading