From a011920296fe7bb60d94bd25d9be4259bb18a076 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anesa=20Fazlagi=C4=87?= <xfazlag@fi.muni.cz>
Date: Thu, 13 Apr 2023 09:08:25 +0200
Subject: [PATCH] added two custom queries - avg price low/high

---
 .../ElectricityPriceController.java           | 40 +++++++++++++++++++
 .../ElectricityPriceRepository.java           |  6 +++
 .../ElectricityPriceService.java              |  4 ++
 .../ElectricityPriceServiceImpl.java          | 11 ++++-
 4 files changed, 60 insertions(+), 1 deletion(-)

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 34dda1c..b931939 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 fdefa83..fdc9789 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 a11b04c..8f8d932 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 f1855c9..f583e3d 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();
+  }
 }
-- 
GitLab