Skip to content

Commit 9b4cf62

Browse files
authored
Merge pull request #104 from sfc-gh-pnescior/accommodation-performance-fix
Inline accommodation offer host language
2 parents 1a8828e + 0500e61 commit 9b4cf62

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

src/main/java/pl/gov/coi/pomocua/ads/accomodations/AccommodationOffer.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
import javax.validation.constraints.Min;
1111
import javax.validation.constraints.NotEmpty;
1212
import javax.validation.constraints.NotNull;
13+
import java.util.Arrays;
1314
import java.util.List;
1415

16+
import static java.util.Collections.emptyList;
17+
import static java.util.stream.Collectors.joining;
18+
import static java.util.stream.Collectors.toList;
1519
import static javax.persistence.EnumType.STRING;
1620

1721
@EqualsAndHashCode(callSuper = true)
@@ -32,10 +36,8 @@ public class AccommodationOffer extends BaseOffer {
3236
@NotNull
3337
public LengthOfStay lengthOfStay;
3438

35-
@ElementCollection(targetClass = Language.class, fetch = FetchType.EAGER)
36-
@CollectionTable
37-
@Enumerated(STRING)
3839
@NotEmpty
40+
@Convert(converter = LanguageConverter.class)
3941
public List<Language> hostLanguage;
4042

4143
@NotNull
@@ -58,5 +60,22 @@ public enum LengthOfStay {
5860
public enum Language {
5961
UA, PL, EN, RU
6062
}
61-
}
6263

64+
public static class LanguageConverter implements AttributeConverter<List<Language>, String> {
65+
@Override
66+
public String convertToDatabaseColumn(List<Language> values) {
67+
if (values == null || values.isEmpty()) {
68+
return "";
69+
}
70+
return values.stream().map(Enum::name).collect(joining(","));
71+
}
72+
73+
@Override
74+
public List<Language> convertToEntityAttribute(String value) {
75+
if (value == null || value.isBlank()) {
76+
return emptyList();
77+
}
78+
return Arrays.stream(value.split(",")).map(Language::valueOf).collect(toList());
79+
}
80+
}
81+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
drop table accommodation_offer_host_language;
2+
drop table accommodation_offer_host_language_AUD;
3+
4+
alter table accommodation_offer add column host_language text;
5+
alter table accommodation_offer_AUD add column host_language text;
6+
7+
update accommodation_offer set host_language = 'PL' where host_language is null;

src/test/java/pl/gov/coi/pomocua/ads/accomodations/AccommodationsResourceTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,36 @@ void shouldIgnoreDeactivatedOfferByCapacityOnly() {
161161

162162
assertThat(offers).isEmpty();
163163
}
164+
165+
@Test
166+
void shouldSearchWithManyHostLanguages() {
167+
AccommodationOffer offer = sampleOfferRequest();
168+
offer.hostLanguage = List.of(Language.EN, Language.PL);
169+
postOffer(offer);
170+
171+
var offers = listOffers();
172+
173+
assertThat(offers)
174+
.hasSize(1)
175+
.element(0)
176+
.extracting(o -> o.hostLanguage)
177+
.isEqualTo(List.of(Language.EN, Language.PL));
178+
}
179+
180+
@Test
181+
void shouldSearchWithOneHostLanguage() {
182+
AccommodationOffer offer = sampleOfferRequest();
183+
offer.hostLanguage = List.of(Language.PL);
184+
postOffer(offer);
185+
186+
var offers = listOffers();
187+
188+
assertThat(offers)
189+
.hasSize(1)
190+
.element(0)
191+
.extracting(o -> o.hostLanguage)
192+
.isEqualTo(List.of(Language.PL));
193+
}
164194
}
165195

166196
@Nested

0 commit comments

Comments
 (0)