Skip to content

Commit

Permalink
fix: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Aug 2, 2024
1 parent 8618e0b commit 1ba63f7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -40,9 +39,6 @@ public class CompanySiteController {
private final CompanySiteService companySiteService;
private final EntityDtoMapper entityDtoMapper;

private record Selections(boolean withPolygons, boolean withRings, boolean withLocations) {
}

public CompanySiteController(CompanySiteService companySiteService, EntityDtoMapper entityDtoMapper) {
this.companySiteService = companySiteService;
this.entityDtoMapper = entityDtoMapper;
Expand All @@ -51,31 +47,16 @@ public CompanySiteController(CompanySiteService companySiteService, EntityDtoMap
@QueryMapping
public List<CompanySiteDto> getCompanySiteByTitle(@Argument("title") String title, @Argument("year") Long year,
DataFetchingEnvironment dataFetchingEnvironment) {
Selections selections = createSelections(dataFetchingEnvironment);
List<CompanySiteDto> companySiteDtos = this.companySiteService
.findCompanySiteByTitleAndYear(title, year, selections.withPolygons(), selections.withRings(),
selections.withLocations())
.findCompanySiteByTitleAndYear(title, year)
.stream().map(this.entityDtoMapper::mapToDto).collect(Collectors.toList());
return companySiteDtos;
}

private Selections createSelections(DataFetchingEnvironment dataFetchingEnvironment) {
boolean addPolygons = dataFetchingEnvironment.getSelectionSet().contains("polygons");
boolean addRings = dataFetchingEnvironment.getSelectionSet().getFields().stream()
.anyMatch(sf -> "rings".equalsIgnoreCase(sf.getName()));
boolean addLocations = dataFetchingEnvironment.getSelectionSet().getFields().stream()
.filter(sf -> "rings".equalsIgnoreCase(sf.getName())).flatMap(sf -> Stream.of(sf.getSelectionSet()))
.anyMatch(sf -> sf.contains("locations"));
Selections selections = new Selections(addPolygons, addRings, addLocations);
return selections;
}

@QueryMapping
public CompanySiteDto getCompanySiteById(@Argument("id") Long id, DataFetchingEnvironment dataFetchingEnvironment) {
Selections selections = createSelections(dataFetchingEnvironment);
return this.companySiteService
.findCompanySiteByIdDetached(id, selections.withPolygons(), selections.withRings(),
selections.withLocations())
.findCompanySiteByIdDetached(id)
.stream().map(this.entityDtoMapper::mapToDto).findFirst()
.orElseThrow(() -> new ResourceNotFoundException(String.format("No CompanySite found for id: %d", id)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public CompanySiteService(CompanySiteRepository companySiteRepository, PolygonRe
this.entityManager = entityManager;
}

public Collection<CompanySite> findCompanySiteByTitleAndYear(String title, Long year, boolean withPolygons,
boolean withRings, boolean withLocations) {
public Collection<CompanySite> findCompanySiteByTitleAndYear(String title, Long year) {
if (title == null || title.length() < 2 || year == null) {
return List.of();
}
Expand All @@ -76,8 +75,7 @@ public Optional<CompanySite> findCompanySiteById(Long id) {
.findFirst();
}

public Optional<CompanySite> findCompanySiteByIdDetached(Long id, boolean withPolygons, boolean withRings,
boolean withLocations) {
public Optional<CompanySite> findCompanySiteByIdDetached(Long id) {
return Optional.ofNullable(id).flatMap(myId -> this.companySiteRepository.findById(myId)).stream()
.findFirst();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Optional;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.graphql.tester.AutoConfigureGraphQlTester;
Expand Down Expand Up @@ -58,8 +58,7 @@ public void init() {

// @Test
public void getCompanySiteByIdFound() throws Exception {
Mockito.when(this.companySiteService.findCompanySiteByIdDetached(any(Long.class), anyBoolean(), anyBoolean(),
anyBoolean())).thenReturn(Optional.of(this.createCompanySiteEntity()));
Mockito.when(this.companySiteService.findCompanySiteByIdDetached(any(Long.class))).thenReturn(Optional.of(this.createCompanySiteEntity()));
String myDocument = "{ getCompanySiteById(id:1) \n" + " { id, title, atDate, \n"
+ " polygons { id, fillColor, borderColor, title, longitude, latitude,\n"
+ " rings{ id, primaryRing,\n" + " locations { id, longitude, latitude}}}}}";
Expand Down

0 comments on commit 1ba63f7

Please sign in to comment.