Skip to content

Commit

Permalink
inlined unnecessary methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoroldvix committed Oct 30, 2023
1 parent 3a71884 commit 7aed251
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ public ItemDealsList getDealsForServer(@Valid ItemDealsRequest request) {
Objects.requireNonNull(request, "Item deals request cannot be null or empty");

ServerResponse server = serverService.getServer(request.serverIdentifier());
List<ItemDealProjection> deals = findDealsForServer(request, server);
List<ItemDealProjection> deals = itemDealsRepository.findDealsForServer(
server.id(),
request.minQuantity(),
request.minQuality(),
request.limit()
);

notEmpty(deals,
() -> new ItemDealsNotFoundException("No deal found for server " + request.serverIdentifier()));

return itemDealsMapper.toItemDealsList(deals);
}

private List<ItemDealProjection> findDealsForServer(ItemDealsRequest request, ServerResponse server) {
return itemDealsRepository.findDealsForServer(server.id(), request.minQuantity(), request.minQuality(), request.limit());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -49,23 +48,20 @@ public GoldPriceResponse getForId(long id) {
@Override
public GoldPricePageResponse getAll(TimeRange timeRange, Pageable pageable) {
validateInputs(timeRange, pageable);
Page<GoldPrice> page = findAllForTimeRange(timeRange, pageable);
Page<GoldPrice> page = goldPriceRepository.findAllForTimeRange(timeRange.start(), timeRange.end(), pageable);
notEmpty(page.getContent(),
() -> new GoldPriceNotFoundException("No prices found for time range: %s-%s".formatted(timeRange.start(), timeRange.end())));
return goldPriceMapper.toPageResponse(page);
}

private Page<GoldPrice> findAllForTimeRange(TimeRange timeRange, Pageable pageable) {
return goldPriceRepository.findAllForTimeRange(timeRange.start(), timeRange.end(), pageable);
}

@Override
public GoldPricePageResponse getForServer(String serverIdentifier, TimeRange timeRange, Pageable pageable) {
notEmpty(serverIdentifier, SERVER_IDENTIFIER_CANNOT_BE_NULL_OR_EMPTY.message);
validateInputs(timeRange, pageable);

ServerResponse server = serverService.getServer(serverIdentifier);
Page<GoldPrice> prices = findAllForServer(server, timeRange, pageable);
Page<GoldPrice> prices = goldPriceRepository
.findAllForServerAndTimeRange(server.id(), timeRange.start(), timeRange.end(), pageable);

notEmpty(prices.getContent(),
() -> new GoldPriceNotFoundException("No prices found for server identifier %s and time range: %s-%s".formatted(
Expand All @@ -79,10 +75,6 @@ private void validateInputs(TimeRange timeRange, Pageable pageable) {
requireNonNull(pageable, PAGEABLE_CANNOT_BE_NULL.message);
}

private Page<GoldPrice> findAllForServer(ServerResponse server, TimeRange timeRange, Pageable pageable) {
return goldPriceRepository.findAllForServerAndTimeRange(server.id(), timeRange.start(), timeRange.end(), pageable);
}

@Override
public GoldPriceListResponse getAllRecent() {
List<GoldPrice> prices = goldPriceRepository.findAllRecent();
Expand All @@ -97,69 +89,53 @@ public GoldPricePageResponse search(@Valid SearchRequest searchRequest, Pageable
requireNonNull(pageable, PAGEABLE_CANNOT_BE_NULL.message);
requireNonNull(searchRequest, SEARCH_REQUEST_CANNOT_BE_NULL.message);

Page<GoldPrice> prices = findAllForSearch(searchRequest, pageable);
Specification<GoldPrice> spec = SpecificationBuilder.from(searchRequest);
Page<GoldPrice> prices = goldPriceRepository.findAll(spec, pageable);
notEmpty(prices.getContent(),
() -> new GoldPriceNotFoundException(NO_PRICES_FOUND));

return goldPriceMapper.toPageResponse(prices);
}

private Page<GoldPrice> findAllForSearch(SearchRequest searchRequest, Pageable pageable) {
Specification<GoldPrice> spec = SpecificationBuilder.from(searchRequest);
return goldPriceRepository.findAll(spec, pageable);
}

@Override
public GoldPriceResponse getRecentForServer(String serverIdentifier) {
notEmpty(serverIdentifier, SERVER_IDENTIFIER_CANNOT_BE_NULL_OR_EMPTY.message);

ServerResponse server = serverService.getServer(serverIdentifier);
GoldPrice price = findRecentForServer(server)
GoldPrice price = goldPriceRepository.findRecentForServer(server.id())
.orElseThrow(() -> new GoldPriceNotFoundException("No prices found for server " + serverIdentifier));

return goldPriceMapper.toResponse(price);
}

private Optional<GoldPrice> findRecentForServer(ServerResponse server) {
return goldPriceRepository.findRecentForServer(server.id());
}

@Override
public GoldPriceListResponse getRecentForRegion(String regionName) {
notEmpty(regionName, REGION_NAME_CANNOT_BE_NULL_OR_EMPTY.message);

List<GoldPrice> prices = findRecentForRegion(regionName);
Region region = StringEnumConverter.fromString(regionName, Region.class);
List<GoldPrice> prices = goldPriceRepository.findRecentForRegion(region.ordinal());
notEmpty(prices, () -> new GoldPriceNotFoundException("No prices found for region " + regionName));

return goldPriceMapper.toGoldPriceList(prices);
}

private List<GoldPrice> findRecentForRegion(String regionName) {
Region region = StringEnumConverter.fromString(regionName, Region.class);
return goldPriceRepository.findRecentForRegion(region.ordinal());
}

@Override
public GoldPriceListResponse getRecentForFaction(String factionName) {
notEmpty(factionName, FACTION_NAME_CANNOT_BE_NULL_OR_EMPTY.message);

List<GoldPrice> prices = findRecentForFaction(factionName);
Faction faction = StringEnumConverter.fromString(factionName, Faction.class);
List<GoldPrice> prices = goldPriceRepository.findRecentForFaction(faction.ordinal());
notEmpty(prices, () -> new GoldPriceNotFoundException("No prices found for faction " + factionName));

return goldPriceMapper.toGoldPriceList(prices);
}

private List<GoldPrice> findRecentForFaction(String factionName) {
Faction faction = StringEnumConverter.fromString(factionName, Faction.class);
return goldPriceRepository.findRecentForFaction(faction.ordinal());
}

@Override
public GoldPriceListResponse getRecentForServerList(@Valid GoldPriceRequest request) {
requireNonNull(request, "Gold price request cannot be null");

Set<Integer> serverIds = getServerIds(request.serverList());
List<GoldPrice> prices = findRecentForServerIds(serverIds);
List<GoldPrice> prices = goldPriceRepository.findRecentForServerIds(serverIds);
notEmpty(prices, () -> new GoldPriceNotFoundException("No prices found for server list"));

return goldPriceMapper.toGoldPriceList(prices);
Expand All @@ -171,10 +147,6 @@ private Set<Integer> getServerIds(Set<String> serverList) {
.collect(Collectors.toSet());
}

private List<GoldPrice> findRecentForServerIds(Set<Integer> serverIds) {
return goldPriceRepository.findRecentForServerIds(serverIds);
}

@Override
@Transactional
public void saveAll(List<GoldPrice> pricesToSave) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.thoroldvix.economatic.search.SpecificationBuilder;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -33,17 +32,13 @@ public ItemPageResponse search(@Valid SearchRequest searchRequest, Pageable page
requireNonNull(searchRequest, SEARCH_REQUEST_CANNOT_BE_NULL.message);
requireNonNull(pageable, PAGEABLE_CANNOT_BE_NULL.message);

Page<Item> items = findAllForSearch(searchRequest, pageable);
Specification<Item> spec = SpecificationBuilder.from(searchRequest);
Page<Item> items = itemRepository.findAll(spec, pageable);
notEmpty(items.getContent(), () -> new ItemNotFoundException(ITEMS_NOT_FOUND));

return itemMapper.toPageResponse(items);
}

private Page<Item> findAllForSearch(SearchRequest searchRequest, Pageable pageable) {
Specification<Item> spec = SpecificationBuilder.from(searchRequest);
return itemRepository.findAll(spec, pageable);
}

@Override
public ItemPageResponse getAll(Pageable pageable) {
requireNonNull(pageable, PAGEABLE_CANNOT_BE_NULL.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,64 +48,52 @@ public ItemPricePageResponse getRecentForServer(String serverIdentifier, Pageabl
requireNonNull(pageable, PAGEABLE_CANNOT_BE_NULL.message);

ServerResponse server = serverService.getServer(serverIdentifier);
Page<ItemPrice> page = findRecentForServer(server, pageable);
Page<ItemPrice> page = itemPriceRepository.findRecentForServer(server.id(), pageable);

notEmpty(page.getContent(),
() -> new ItemPriceNotFoundException("No recent item prices found for server identifier " + serverIdentifier));

return itemPriceMapper.toPageResponse(page);
}

private Page<ItemPrice> findRecentForServer(ServerResponse server, Pageable pageable) {
return itemPriceRepository.findRecentForServer(server.id(), pageable);
}

@Override
public ItemPriceListResponse getRecentForRegion(String regionName, String itemIdentifier) {
notEmpty(regionName, REGION_NAME_CANNOT_BE_NULL_OR_EMPTY.message);
notEmpty(itemIdentifier, ITEM_IDENTIFIER_CANNOT_BE_NULL_OR_EMPTY.message);

ItemResponse item = itemService.getItem(itemIdentifier);
List<ItemPrice> itemPrices = findRecentForRegionAndItem(regionName, item);
Region region = StringEnumConverter.fromString(regionName, Region.class);
List<ItemPrice> itemPrices = itemPriceRepository.findRecentForRegionAndItem(region.ordinal(), item.id());

notEmpty(itemPrices,
() -> new ItemPriceNotFoundException("No item prices found for region and item identifier " + regionName + " " + itemIdentifier));

return itemPriceMapper.toItemPriceList(itemPrices);
}

private List<ItemPrice> findRecentForRegionAndItem(String regionName, ItemResponse item) {
Region region = StringEnumConverter.fromString(regionName, Region.class);
return itemPriceRepository.findRecentForRegionAndItem(region.ordinal(), item.id());
}

@Override
public ItemPriceListResponse getRecentForFaction(String factionName, String itemIdentifier) {
notEmpty(factionName, FACTION_NAME_CANNOT_BE_NULL_OR_EMPTY.message);
notEmpty(itemIdentifier, ITEM_IDENTIFIER_CANNOT_BE_NULL_OR_EMPTY.message);

ItemResponse item = itemService.getItem(itemIdentifier);
List<ItemPrice> itemPrices = findRecentForFactionAndItem(factionName, item);
Faction faction = StringEnumConverter.fromString(factionName, Faction.class);
List<ItemPrice> itemPrices = itemPriceRepository.findRecentForFactionAndItem(faction.ordinal(), item.id());

notEmpty(itemPrices,
() -> new ItemPriceNotFoundException("No item prices found for faction and item identifier " + factionName + " " + itemIdentifier));

return itemPriceMapper.toItemPriceList(itemPrices);
}

private List<ItemPrice> findRecentForFactionAndItem(String factionName, ItemResponse item) {
Faction faction = StringEnumConverter.fromString(factionName, Faction.class);
return itemPriceRepository.findRecentForFactionAndItem(faction.ordinal(), item.id());
}

@Override
public ItemPriceListResponse getRecentForServer(String serverIdentifier, String itemIdentifier) {
notEmpty(serverIdentifier, SERVER_IDENTIFIER_CANNOT_BE_NULL_OR_EMPTY.message);
notEmpty(itemIdentifier, ITEM_IDENTIFIER_CANNOT_BE_NULL_OR_EMPTY.message);

ServerResponse server = serverService.getServer(serverIdentifier);
ItemResponse item = itemService.getItem(itemIdentifier);
List<ItemPrice> itemPrices = findRecentForServerAndItem(server, item);
List<ItemPrice> itemPrices = itemPriceRepository.findRecentForServerAndItem(server.id(), item.id());

notEmpty(itemPrices,
() -> new ItemPriceNotFoundException("No item prices found for server identifier %s and item identifier %s"
Expand All @@ -114,26 +102,18 @@ public ItemPriceListResponse getRecentForServer(String serverIdentifier, String
return itemPriceMapper.toItemPriceList(itemPrices);
}

private List<ItemPrice> findRecentForServerAndItem(ServerResponse server, ItemResponse item) {
return itemPriceRepository.findRecentForServerAndItem(server.id(), item.id());
}

@Override
public ItemPricePageResponse search(@Valid SearchRequest searchRequest, Pageable pageable) {
requireNonNull(pageable, PAGEABLE_CANNOT_BE_NULL.message);
requireNonNull(searchRequest, SEARCH_REQUEST_CANNOT_BE_NULL.message);

Page<ItemPrice> page = findAllForSearch(searchRequest, pageable);
Specification<ItemPrice> specification = SpecificationBuilder.from(searchRequest);
Page<ItemPrice> page = itemPriceRepository.findAll(specification, pageable);
notEmpty(page.getContent(), () -> new ItemPriceNotFoundException("No item prices found for search request"));

return itemPriceMapper.toPageResponse(page);
}

private Page<ItemPrice> findAllForSearch(SearchRequest searchRequest, Pageable pageable) {
Specification<ItemPrice> specification = SpecificationBuilder.from(searchRequest);
return itemPriceRepository.findAll(specification, pageable);
}

@Override
public ItemPricePageResponse getForServer(String serverIdentifier, String itemIdentifier, TimeRange timeRange, Pageable pageable) {
notEmpty(serverIdentifier, SERVER_IDENTIFIER_CANNOT_BE_NULL_OR_EMPTY.message);
Expand All @@ -143,7 +123,14 @@ public ItemPricePageResponse getForServer(String serverIdentifier, String itemId

ServerResponse server = serverService.getServer(serverIdentifier);
ItemResponse item = itemService.getItem(itemIdentifier);
Page<ItemPrice> page = findForServerAndTimeRange(server, item, timeRange, pageable);

Page<ItemPrice> page = itemPriceRepository.findForServerAndTimeRange(
server.id(),
item.id(),
timeRange.start(),
timeRange.end(),
pageable
);

notEmpty(page.getContent(),
() -> new ItemPriceNotFoundException("No item prices found for time range %s for server identifier %s and item identifier %s"
Expand All @@ -152,14 +139,6 @@ public ItemPricePageResponse getForServer(String serverIdentifier, String itemId
return itemPriceMapper.toPageResponse(page);
}

private Page<ItemPrice> findForServerAndTimeRange(ServerResponse server,
ItemResponse item,
TimeRange timeRange,
Pageable pageable) {

return itemPriceRepository.findForServerAndTimeRange(server.id(), item.id(), timeRange.start(), timeRange.end(), pageable);
}

@Override
public ItemPricePageResponse getRecentForItemListAndServers(@Valid ItemPriceRequest request, Pageable pageable) {
requireNonNull(request, "Item price request cannot be null");
Expand Down
Loading

0 comments on commit 7aed251

Please sign in to comment.