diff --git a/src/main/java/pl/gov/coi/pomocua/ads/accomodations/AccommodationOffer.java b/src/main/java/pl/gov/coi/pomocua/ads/accomodations/AccommodationOffer.java index b5bb22c5..e1661571 100644 --- a/src/main/java/pl/gov/coi/pomocua/ads/accomodations/AccommodationOffer.java +++ b/src/main/java/pl/gov/coi/pomocua/ads/accomodations/AccommodationOffer.java @@ -10,8 +10,12 @@ import javax.validation.constraints.Min; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; +import java.util.Arrays; import java.util.List; +import static java.util.Collections.emptyList; +import static java.util.stream.Collectors.joining; +import static java.util.stream.Collectors.toList; import static javax.persistence.EnumType.STRING; @EqualsAndHashCode(callSuper = true) @@ -32,10 +36,8 @@ public class AccommodationOffer extends BaseOffer { @NotNull public LengthOfStay lengthOfStay; - @ElementCollection(targetClass = Language.class, fetch = FetchType.EAGER) - @CollectionTable - @Enumerated(STRING) @NotEmpty + @Convert(converter = LanguageConverter.class) public List hostLanguage; @NotNull @@ -58,5 +60,22 @@ public enum LengthOfStay { public enum Language { UA, PL, EN, RU } -} + public static class LanguageConverter implements AttributeConverter, String> { + @Override + public String convertToDatabaseColumn(List values) { + if (values == null || values.isEmpty()) { + return ""; + } + return values.stream().map(Enum::name).collect(joining(",")); + } + + @Override + public List convertToEntityAttribute(String value) { + if (value == null || value.isBlank()) { + return emptyList(); + } + return Arrays.stream(value.split(",")).map(Language::valueOf).collect(toList()); + } + } +} diff --git a/src/main/resources/db/migration/V18__inline_host_language.sql b/src/main/resources/db/migration/V18__inline_host_language.sql new file mode 100644 index 00000000..fce4fad3 --- /dev/null +++ b/src/main/resources/db/migration/V18__inline_host_language.sql @@ -0,0 +1,7 @@ +drop table accommodation_offer_host_language; +drop table accommodation_offer_host_language_AUD; + +alter table accommodation_offer add column host_language text; +alter table accommodation_offer_AUD add column host_language text; + +update accommodation_offer set host_language = 'PL' where host_language is null; diff --git a/src/test/java/pl/gov/coi/pomocua/ads/accomodations/AccommodationsResourceTest.java b/src/test/java/pl/gov/coi/pomocua/ads/accomodations/AccommodationsResourceTest.java index 4921395c..c2accee6 100644 --- a/src/test/java/pl/gov/coi/pomocua/ads/accomodations/AccommodationsResourceTest.java +++ b/src/test/java/pl/gov/coi/pomocua/ads/accomodations/AccommodationsResourceTest.java @@ -161,6 +161,36 @@ void shouldIgnoreDeactivatedOfferByCapacityOnly() { assertThat(offers).isEmpty(); } + + @Test + void shouldSearchWithManyHostLanguages() { + AccommodationOffer offer = sampleOfferRequest(); + offer.hostLanguage = List.of(Language.EN, Language.PL); + postOffer(offer); + + var offers = listOffers(); + + assertThat(offers) + .hasSize(1) + .element(0) + .extracting(o -> o.hostLanguage) + .isEqualTo(List.of(Language.EN, Language.PL)); + } + + @Test + void shouldSearchWithOneHostLanguage() { + AccommodationOffer offer = sampleOfferRequest(); + offer.hostLanguage = List.of(Language.PL); + postOffer(offer); + + var offers = listOffers(); + + assertThat(offers) + .hasSize(1) + .element(0) + .extracting(o -> o.hostLanguage) + .isEqualTo(List.of(Language.PL)); + } } @Nested