Skip to content

Commit

Permalink
Update code formatting for consistency
Browse files Browse the repository at this point in the history
This commit includes changes in various files to maintain the consistency in code. Extraneous line breaks have been removed in some files and needed line breaks have been added in others for readability. The sequence of the import statements have been altered in some files for it to be in proper alphabetical order. In files like 'PopulationStatServiceImpl.java', 'GoldPriceStatServiceImpl.java' and more, indentation has been adjusted to maintain consistency across code base. The rearrangement does not change any functionality, it is purely for readability and code maintainability.
  • Loading branch information
Thoroldvix committed Jul 16, 2023
1 parent d3732d8 commit a24fac5
Show file tree
Hide file tree
Showing 74 changed files with 360 additions and 336 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.thoroldvix.economatic.goldprice;

import com.thoroldvix.economatic.search.SearchRequest;
import com.thoroldvix.economatic.dto.TimeRange;
import com.thoroldvix.economatic.search.SearchRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.thoroldvix.economatic.goldprice;

import com.thoroldvix.economatic.server.Server;
import com.thoroldvix.economatic.dto.PaginationInfo;
import com.thoroldvix.economatic.server.Server;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.thoroldvix.economatic.goldprice;

import com.thoroldvix.economatic.search.SearchRequest;
import com.thoroldvix.economatic.dto.TimeRange;
import com.thoroldvix.economatic.search.SearchRequest;
import jakarta.validation.Valid;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.thoroldvix.economatic.goldprice;

import com.thoroldvix.economatic.dto.TimeRange;
import com.thoroldvix.economatic.search.SearchRequest;
import com.thoroldvix.economatic.search.SpecificationBuilder;
import com.thoroldvix.economatic.server.Faction;
import com.thoroldvix.economatic.server.Region;
import com.thoroldvix.economatic.server.ServerResponse;
import com.thoroldvix.economatic.server.ServerService;
import com.thoroldvix.economatic.search.SearchRequest;
import com.thoroldvix.economatic.dto.TimeRange;
import com.thoroldvix.economatic.search.SpecificationBuilder;
import com.thoroldvix.economatic.util.StringEnumConverter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -48,7 +48,6 @@ public GoldPriceResponse getForId(long id) {
.orElseThrow(() -> new GoldPriceNotFoundException("No gold price found with id " + id));
}


@Override
public GoldPricePageResponse getAll(TimeRange timeRange, Pageable pageable) {
validateInputs(timeRange, pageable);
Expand All @@ -58,6 +57,34 @@ public GoldPricePageResponse getAll(TimeRange timeRange, Pageable pageable) {
return goldPriceMapper.toPageResponse(page);
}

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

private void validateInputs(TimeRange timeRange, Pageable pageable) {
requireNonNull(timeRange, TIME_RANGE_CANNOT_BE_NULL);
requireNonNull(pageable, PAGEABLE_CANNOT_BE_NULL);
}

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

ServerResponse server = serverService.getServer(serverIdentifier);
Page<GoldPrice> prices = findAllForServer(server, timeRange, pageable);

notEmpty(prices.getContent(),
() -> new GoldPriceNotFoundException("No prices found for server identifier %s and time range: %s-%s".formatted(
serverIdentifier, timeRange.start(), timeRange.end())));

return goldPriceMapper.toPageResponse(prices);
}

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 @@ -79,21 +106,12 @@ public GoldPricePageResponse search(@Valid SearchRequest searchRequest, Pageable
return goldPriceMapper.toPageResponse(prices);
}

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

ServerResponse server = serverService.getServer(serverIdentifier);
Page<GoldPrice> prices = findAllForServer(server, timeRange, pageable);

notEmpty(prices.getContent(),
() -> new GoldPriceNotFoundException("No prices found for server identifier %s and time range: %s-%s".formatted(
serverIdentifier, timeRange.start(), timeRange.end())));

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);
Expand All @@ -105,6 +123,10 @@ public GoldPriceResponse getRecentForServer(String 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);
Expand All @@ -115,6 +137,11 @@ public GoldPriceListResponse getRecentForRegion(String 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);
Expand All @@ -125,6 +152,11 @@ public GoldPriceListResponse getRecentForFaction(String 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");
Expand All @@ -136,54 +168,21 @@ public GoldPriceListResponse getRecentForServerList(@Valid GoldPriceRequest requ
return goldPriceMapper.toGoldPriceList(prices);
}

@Override
@Transactional
public void saveAll(List<GoldPrice> pricesToSave) {
notEmpty(pricesToSave, () -> new IllegalArgumentException("Prices cannot be null or empty"));

goldPriceRepository.saveAll(pricesToSave);
}

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

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

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

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

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

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

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

private Set<Integer> getServerIds(Set<String> serverList) {
return serverList.stream()
.map(server -> serverService.getServer(server).id())
.collect(Collectors.toSet());
}

private void validateInputs(TimeRange timeRange, Pageable pageable) {
requireNonNull(timeRange, TIME_RANGE_CANNOT_BE_NULL);
requireNonNull(pageable, PAGEABLE_CANNOT_BE_NULL);
private List<GoldPrice> findRecentForServerIds(Set<Integer> serverIds) {
return goldPriceRepository.findRecentForServerIds(serverIds);
}

@Override
@Transactional
public void saveAll(List<GoldPrice> pricesToSave) {
notEmpty(pricesToSave, () -> new IllegalArgumentException("Prices cannot be null or empty"));

goldPriceRepository.saveAll(pricesToSave);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class GoldPriceUpdateService {
public static final String UPDATE_ON_STARTUP_OR_DEFAULT = "#{${economatic.update-on-startup} ? -1 : ${economatic.gold-price.update-rate}}";
public static final String UPDATE_RATE = "${economatic.gold-price.update-rate}";

@PersistenceContext
private final EntityManager entityManager;
private final GoldPriceServiceImpl goldPriceServiceImpl;
Expand All @@ -44,7 +45,6 @@ protected void update() {
List<GoldPrice> pricesToSave = getPriceList(servers, prices);

goldPriceServiceImpl.saveAll(pricesToSave);

log.info("Finished updating gold prices in {} ms", elapsedTimeInMillis(start));
}

Expand Down
50 changes: 24 additions & 26 deletions src/main/java/com/thoroldvix/economatic/item/ItemServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.thoroldvix.economatic.item;

import com.thoroldvix.economatic.search.SpecificationBuilder;
import com.thoroldvix.economatic.search.SearchRequest;
import com.thoroldvix.economatic.search.SpecificationBuilder;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -28,7 +28,6 @@
class ItemServiceImpl implements ItemService {

public static final String ITEMS_NOT_FOUND = "Items not found";

private final ItemMapper itemMapper;
private final ItemRepository itemRepository;

Expand All @@ -43,6 +42,11 @@ public ItemPageResponse search(@Valid SearchRequest searchRequest, Pageable page
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);
Expand All @@ -53,7 +57,6 @@ public ItemPageResponse getAll(Pageable pageable) {
return itemMapper.toPageResponse(page);
}


@Override
public ItemResponse getItem(String itemIdentifier) {
notEmpty(itemIdentifier, ITEM_IDENTIFIER_CANNOT_BE_NULL_OR_EMPTY);
Expand All @@ -63,18 +66,13 @@ public ItemResponse getItem(String itemIdentifier) {
.orElseThrow(() -> new ItemNotFoundException("No item found for identifier " + itemIdentifier));
}


@Override
@Transactional
public ItemResponse addItem(@Valid ItemRequest itemRequest) {
requireNonNull(itemRequest, "Item request cannot be null");

Item item = itemMapper.fromRequest(itemRequest);
itemRepository.findById(item.getId()).ifPresent(i -> {
throw new ItemAlreadyExistsException("Item with id " + item.getId() + " already exists");
});

return itemMapper.toResponse(itemRepository.save(item));
private Optional<Item> findItem(String itemIdentifier) {
try {
int itemId = Integer.parseInt(itemIdentifier);
return itemRepository.findById(itemId);
} catch (NumberFormatException ignored) {
return itemRepository.findByUniqueName(itemIdentifier);
}
}

@Override
Expand All @@ -89,18 +87,18 @@ public ItemResponse deleteItem(String itemIdentifier) {
return itemMapper.toResponse(item);
}

private Page<Item> findAllForSearch(SearchRequest searchRequest, Pageable pageable) {
Specification<Item> spec = SpecificationBuilder.from(searchRequest);
return itemRepository.findAll(spec, pageable);
}
@Override
@Transactional
public ItemResponse addItem(@Valid ItemRequest itemRequest) {
requireNonNull(itemRequest, "Item request cannot be null");

private Optional<Item> findItem(String itemIdentifier) {
try {
int itemId = Integer.parseInt(itemIdentifier);
return itemRepository.findById(itemId);
} catch (NumberFormatException ignored) {
return itemRepository.findByUniqueName(itemIdentifier);
}
Item item = itemMapper.fromRequest(itemRequest);
itemRepository.findById(item.getId()).ifPresent(i -> {
throw new ItemAlreadyExistsException("Item with id " + item.getId() + " already exists");
});

return itemMapper.toResponse(itemRepository.save(item));
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.thoroldvix.economatic.itemprice;

import com.thoroldvix.economatic.search.SearchRequest;
import com.thoroldvix.economatic.dto.TimeRange;
import com.thoroldvix.economatic.search.SearchRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.thoroldvix.economatic.itemprice;

import com.thoroldvix.economatic.dto.PaginationInfo;
import com.thoroldvix.economatic.item.Item;
import com.thoroldvix.economatic.server.Server;
import com.thoroldvix.economatic.dto.PaginationInfo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.thoroldvix.economatic.itemprice;

import com.thoroldvix.economatic.search.SearchRequest;
import com.thoroldvix.economatic.dto.TimeRange;
import com.thoroldvix.economatic.search.SearchRequest;
import jakarta.validation.Valid;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
Expand Down
Loading

0 comments on commit a24fac5

Please sign in to comment.