Skip to content

Commit

Permalink
Merge pull request #104 from sfc-gh-pnescior/accommodation-performanc…
Browse files Browse the repository at this point in the history
…e-fix

Inline accommodation offer host language
  • Loading branch information
mndzielski authored Mar 28, 2022
2 parents 1a8828e + 0500e61 commit 9b4cf62
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<Language> hostLanguage;

@NotNull
Expand All @@ -58,5 +60,22 @@ public enum LengthOfStay {
public enum Language {
UA, PL, EN, RU
}
}

public static class LanguageConverter implements AttributeConverter<List<Language>, String> {
@Override
public String convertToDatabaseColumn(List<Language> values) {
if (values == null || values.isEmpty()) {
return "";
}
return values.stream().map(Enum::name).collect(joining(","));
}

@Override
public List<Language> convertToEntityAttribute(String value) {
if (value == null || value.isBlank()) {
return emptyList();
}
return Arrays.stream(value.split(",")).map(Language::valueOf).collect(toList());
}
}
}
7 changes: 7 additions & 0 deletions src/main/resources/db/migration/V18__inline_host_language.sql
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9b4cf62

Please sign in to comment.