Skip to content
Snippets Groups Projects
Commit a0119202 authored by Anesa Fazlagić's avatar Anesa Fazlagić
Browse files

added two custom queries - avg price low/high

parent a052979f
No related branches found
No related tags found
3 merge requests!79Final merge to main,!43Testing user,!36Electricity tarif microservice hibernate add
Pipeline #
......@@ -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();
}
}
......@@ -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();
}
......@@ -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
......
......@@ -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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment