From e2a9c90f032b01681347f62afa6f07cc6e18d5a2 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 11 Aug 2024 21:05:18 +0200 Subject: [PATCH] fix(perf): optimize select all query for performance --- .../org/pvh/repository/GlassesRepository.java | 8 ++-- .../org/pvh/rest/GlassesRestController.java | 47 +++++++------------ .../java/org/pvh/service/MainServiceImpl.java | 12 ++--- 3 files changed, 29 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/pvh/repository/GlassesRepository.java b/src/main/java/org/pvh/repository/GlassesRepository.java index 9869e1e..490bbe8 100644 --- a/src/main/java/org/pvh/repository/GlassesRepository.java +++ b/src/main/java/org/pvh/repository/GlassesRepository.java @@ -1,18 +1,20 @@ package org.pvh.repository; import org.pvh.model.entity.Glasses; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; import java.util.List; import java.util.Optional; public interface GlassesRepository extends JpaRepository, CustomGlassesRepository, JpaSpecificationExecutor { - Page findByDispensedAndGlassesTypeAndLocation(boolean dispensed, String location, String glassesType, Pageable pageable); + @Query(value = "SELECT distinct g from Glasses g " + + "join fetch g.od join fetch g.os join fetch g.dispense " + + "where g.dispensed = :dispensed and g.location = :location ") + List findByDispensedAndLocation(boolean dispensed, String location); List findByGlassesTypeContaining(String glassesType, Sort sort); diff --git a/src/main/java/org/pvh/rest/GlassesRestController.java b/src/main/java/org/pvh/rest/GlassesRestController.java index d726197..87496ef 100644 --- a/src/main/java/org/pvh/rest/GlassesRestController.java +++ b/src/main/java/org/pvh/rest/GlassesRestController.java @@ -1,18 +1,9 @@ - package org.pvh.rest; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; -import java.util.stream.Collectors; - +import cz.jirutka.rsql.parser.RSQLParser; +import cz.jirutka.rsql.parser.ast.Node; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.validation.Valid; import org.pvh.error.NoSkusLeftException; import org.pvh.error.PVHException; import org.pvh.model.dto.ChangeValueDTO; @@ -41,23 +32,13 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.web.util.UriComponentsBuilder; -import cz.jirutka.rsql.parser.RSQLParser; -import cz.jirutka.rsql.parser.ast.Node; -import jakarta.servlet.http.HttpServletResponse; -import jakarta.validation.Valid; +import java.util.*; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.stream.Collectors; @RestController @CrossOrigin(exposedHeaders = "errors, content-type") @@ -131,6 +112,14 @@ public ResponseEntity> getAllGlassesPaginated( return new ResponseEntity<>(response, HttpStatus.OK); } + @PreAuthorize("hasRole('USER') or hasRole('ADMIN')") + @GetMapping(path = "/{location}/all") + public Collection getAllGlasses(@PathVariable("location") String location) { + List pageGlasses = mainService.findByDispensedAndLocation(false, location); + + return pageGlasses.stream().map(a -> GlassesMapperImpl.getInstance().glassesToGlassesResponseDTO(a)).toList(); + } + @PreAuthorize("hasRole('USER') or hasRole('ADMIN')") @GetMapping(path = "/{location}.csv") @ResponseBody @@ -349,4 +338,4 @@ public ResponseEntity addUnsuccessfulSearch(@PathVariable("location") Stri } -} +} \ No newline at end of file diff --git a/src/main/java/org/pvh/service/MainServiceImpl.java b/src/main/java/org/pvh/service/MainServiceImpl.java index f174830..ea3caab 100644 --- a/src/main/java/org/pvh/service/MainServiceImpl.java +++ b/src/main/java/org/pvh/service/MainServiceImpl.java @@ -1,9 +1,5 @@ package org.pvh.service; -import java.util.Date; -import java.util.List; -import java.util.Optional; - import org.pvh.error.NoSkusLeftException; import org.pvh.model.entity.Dispense; import org.pvh.model.entity.Glasses; @@ -23,6 +19,10 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.Date; +import java.util.List; +import java.util.Optional; + @Service public class MainServiceImpl implements MainService { @@ -134,7 +134,7 @@ public Page findByDispensedAndLocation(boolean dispensed, String locati @Override public List findByDispensedAndLocation(boolean dispensed, String location) throws DataAccessException { - return glassesRepository.findAll(Specification.where(GlassesSpecs.hasLocation(location)).and(GlassesSpecs.isDispensed(dispensed))); + return glassesRepository.findByDispensedAndLocation(dispensed, location); } @Override @@ -158,4 +158,4 @@ public UnsuccessfulSearch saveUnsuccessfulSearch(UnsuccessfulSearch search) { return unsuccessfulSearchRepository.save(search); } -} +} \ No newline at end of file