diff --git a/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceController.java b/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceController.java index 34dda1cb278d9566cd2de1f84ed02b7cfae88734..b931939edf9a9d158f0a6ec9ce99baeca139db42 100644 --- a/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceController.java +++ b/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceController.java @@ -161,4 +161,44 @@ public class ElectricityPriceController { public Page<ElectricityPriceGetFullDto> getAllElectricityPricePagable(Pageable pageable) { return electricityPriceService.getAllElectricityPricePagable(pageable); } + + @Operation( + summary = "Get average high tariff electricity price of all companies", + description = "Returns average high tariff electricity price of all companies from the database.", + tags = "{electricity price}") + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "Found average high tariff electricity price", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ElectricityPrice.class)) + }) + }) + @GetMapping("/avg/hightariff") + public double getAveragePriceHighTariff() { + return electricityPriceService.averagePriceHighTariff(); + } + + @Operation( + summary = "Get average low tariff electricity price of all companies", + description = "Returns average low tariff electricity price of all companies from the database.", + tags = "{electricity price}") + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "Found average low tariff electricity price", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = ElectricityPrice.class)) + }) + }) + @GetMapping("/avg/lowtariff") + public double getAveragePriceLowTariff() { + return electricityPriceService.averagePriceLowTariff(); + } } diff --git a/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceRepository.java b/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceRepository.java index fdefa83d2654f71a7326eddbd346ce9ec7740fd0..fdc9789833217b338eba8307109600208374b6be 100644 --- a/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceRepository.java +++ b/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceRepository.java @@ -12,4 +12,10 @@ public interface ElectricityPriceRepository extends JpaRepository<ElectricityPri @Modifying @Query(value = "DELETE FROM electricityprices WHERE company_id = ?1", nativeQuery = true) void deleteCompany(Long companyId); + + @Query(value = "SELECT AVG(price_low_tariff) FROM electricityprices", nativeQuery = true) + double averagePriceLowTariff(); + + @Query(value = "SELECT AVG(price_high_tariff) FROM electricityprices", nativeQuery = true) + double averagePriceHighTariff(); } diff --git a/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceService.java b/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceService.java index a11b04c5b9e220d8498592597e0ee88d8fb57fc7..8f8d93238448d999eb113d83cb6faa6fadb14de8 100644 --- a/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceService.java +++ b/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceService.java @@ -8,6 +8,10 @@ import org.springframework.data.domain.Pageable; import java.util.List; public interface ElectricityPriceService { + double averagePriceLowTariff(); + + public double averagePriceHighTariff(); + ElectricityPriceGetFullDto getElectricityPriceObj(Long companyId); double getElectricityPrice(Long companyId); // dependent on time - low or high tariff diff --git a/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceServiceImpl.java b/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceServiceImpl.java index f1855c91543818d079c2858e51738929c95f57e9..f583e3d724f68e4809f26566119bc5e85bd8dfa1 100644 --- a/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceServiceImpl.java +++ b/electricityTarifMicroservice/src/main/java/cz/muni/fi/pa165/microservice4/electricityprices/ElectricityPriceServiceImpl.java @@ -7,7 +7,6 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.bind.annotation.ExceptionHandler; import java.time.DayOfWeek; import java.time.LocalDate; @@ -95,5 +94,15 @@ public class ElectricityPriceServiceImpl implements ElectricityPriceService { return electricityPriceRepository.save(mapStructMapper.electricityPricePostDtoToElectricityPrice(req)); } + @Override + @Transactional(readOnly = true) + public double averagePriceHighTariff() { + return electricityPriceRepository.averagePriceHighTariff(); + } + @Override + @Transactional(readOnly = true) + public double averagePriceLowTariff() { + return electricityPriceRepository.averagePriceLowTariff(); + } }