feat: add firearm type filtering to page queries in Firearm and Modification services
This commit is contained in:
@@ -2,6 +2,7 @@ package com.onixbyte.deltaforceguide.controller;
|
|||||||
|
|
||||||
import com.onixbyte.deltaforceguide.domain.dto.FirearmResponse;
|
import com.onixbyte.deltaforceguide.domain.dto.FirearmResponse;
|
||||||
import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
|
import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
|
||||||
|
import com.onixbyte.deltaforceguide.enumeration.FirearmType;
|
||||||
import com.onixbyte.deltaforceguide.service.FirearmService;
|
import com.onixbyte.deltaforceguide.service.FirearmService;
|
||||||
import jakarta.validation.constraints.Max;
|
import jakarta.validation.constraints.Max;
|
||||||
import jakarta.validation.constraints.Min;
|
import jakarta.validation.constraints.Min;
|
||||||
@@ -30,9 +31,10 @@ public class FirearmController {
|
|||||||
@RequestParam(defaultValue = "0") @Min(0) int page,
|
@RequestParam(defaultValue = "0") @Min(0) int page,
|
||||||
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int size,
|
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int size,
|
||||||
@RequestParam(defaultValue = "id") String sortBy,
|
@RequestParam(defaultValue = "id") String sortBy,
|
||||||
@RequestParam(defaultValue = "DESC") Sort.Direction direction
|
@RequestParam(defaultValue = "DESC") Sort.Direction direction,
|
||||||
|
@RequestParam(required = false) FirearmType type
|
||||||
) {
|
) {
|
||||||
return firearmService.pageQuery(PageRequest.of(page, size, Sort.by(direction, sortBy)));
|
return firearmService.pageQuery(type, PageRequest.of(page, size, Sort.by(direction, sortBy)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
|
|||||||
import com.onixbyte.deltaforceguide.service.ModificationService;
|
import com.onixbyte.deltaforceguide.service.ModificationService;
|
||||||
import jakarta.validation.constraints.Max;
|
import jakarta.validation.constraints.Max;
|
||||||
import jakarta.validation.constraints.Min;
|
import jakarta.validation.constraints.Min;
|
||||||
|
import jakarta.validation.constraints.Positive;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
@@ -29,10 +30,11 @@ public class ModificationController {
|
|||||||
public PageResponse<ModificationResponse> pageQuery(
|
public PageResponse<ModificationResponse> pageQuery(
|
||||||
@RequestParam(defaultValue = "0") @Min(0) int page,
|
@RequestParam(defaultValue = "0") @Min(0) int page,
|
||||||
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int size,
|
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int size,
|
||||||
|
@RequestParam(required = false) @Positive Long firearmId,
|
||||||
@RequestParam(defaultValue = "id") String sortBy,
|
@RequestParam(defaultValue = "id") String sortBy,
|
||||||
@RequestParam(defaultValue = "DESC") Sort.Direction direction
|
@RequestParam(defaultValue = "DESC") Sort.Direction direction
|
||||||
) {
|
) {
|
||||||
return modificationService.pageQuery(PageRequest.of(page, size, Sort.by(direction, sortBy)));
|
return modificationService.pageQuery(firearmId, PageRequest.of(page, size, Sort.by(direction, sortBy)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
package com.onixbyte.deltaforceguide.repository;
|
package com.onixbyte.deltaforceguide.repository;
|
||||||
|
|
||||||
import com.onixbyte.deltaforceguide.domain.entity.Firearm;
|
import com.onixbyte.deltaforceguide.domain.entity.Firearm;
|
||||||
|
import com.onixbyte.deltaforceguide.enumeration.FirearmType;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface FirearmRepository extends JpaRepository<Firearm, Long> {
|
public interface FirearmRepository extends JpaRepository<Firearm, Long> {
|
||||||
|
|
||||||
|
Page<Firearm> findAllByType(FirearmType type, Pageable pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ public interface ModificationRepository extends JpaRepository<Modification, Long
|
|||||||
@EntityGraph(attributePaths = {"firearm"})
|
@EntityGraph(attributePaths = {"firearm"})
|
||||||
Page<Modification> findAllBy(Pageable pageable);
|
Page<Modification> findAllBy(Pageable pageable);
|
||||||
|
|
||||||
|
@EntityGraph(attributePaths = {"firearm"})
|
||||||
|
Page<Modification> findAllByFirearm_Id(Long firearmId, Pageable pageable);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@EntityGraph(attributePaths = {"firearm"})
|
@EntityGraph(attributePaths = {"firearm"})
|
||||||
Optional<Modification> findById(Long id);
|
Optional<Modification> findById(Long id);
|
||||||
|
|||||||
@@ -2,7 +2,10 @@ package com.onixbyte.deltaforceguide.service;
|
|||||||
|
|
||||||
import com.onixbyte.deltaforceguide.domain.dto.FirearmResponse;
|
import com.onixbyte.deltaforceguide.domain.dto.FirearmResponse;
|
||||||
import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
|
import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
|
||||||
|
import com.onixbyte.deltaforceguide.domain.entity.Firearm;
|
||||||
|
import com.onixbyte.deltaforceguide.enumeration.FirearmType;
|
||||||
import com.onixbyte.deltaforceguide.repository.FirearmRepository;
|
import com.onixbyte.deltaforceguide.repository.FirearmRepository;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -19,11 +22,12 @@ public class FirearmService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public PageResponse<FirearmResponse> pageQuery(Pageable pageable) {
|
public PageResponse<FirearmResponse> pageQuery(FirearmType type, Pageable pageable) {
|
||||||
return PageResponse.from(
|
Page<Firearm> page = type == null
|
||||||
firearmRepository.findAll(pageable)
|
? firearmRepository.findAll(pageable)
|
||||||
.map(FirearmResponse::from)
|
: firearmRepository.findAllByType(type, pageable);
|
||||||
);
|
|
||||||
|
return PageResponse.from(page.map(FirearmResponse::from));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package com.onixbyte.deltaforceguide.service;
|
|||||||
|
|
||||||
import com.onixbyte.deltaforceguide.domain.dto.ModificationResponse;
|
import com.onixbyte.deltaforceguide.domain.dto.ModificationResponse;
|
||||||
import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
|
import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
|
||||||
|
import com.onixbyte.deltaforceguide.domain.entity.Modification;
|
||||||
import com.onixbyte.deltaforceguide.repository.ModificationRepository;
|
import com.onixbyte.deltaforceguide.repository.ModificationRepository;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -19,11 +21,12 @@ public class ModificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public PageResponse<ModificationResponse> pageQuery(Pageable pageable) {
|
public PageResponse<ModificationResponse> pageQuery(Long firearmId, Pageable pageable) {
|
||||||
return PageResponse.from(
|
Page<Modification> page = firearmId == null
|
||||||
modificationRepository.findAllBy(pageable)
|
? modificationRepository.findAllBy(pageable)
|
||||||
.map(ModificationResponse::from)
|
: modificationRepository.findAllByFirearm_Id(firearmId, pageable);
|
||||||
);
|
|
||||||
|
return PageResponse.from(page.map(ModificationResponse::from));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
|
|||||||
Reference in New Issue
Block a user