feat: add firearm type filtering to page queries in Firearm and Modification services

This commit is contained in:
2026-04-06 14:01:13 +08:00
parent 80dc5170a4
commit 33e29f48b9
6 changed files with 32 additions and 13 deletions
@@ -2,6 +2,7 @@ package com.onixbyte.deltaforceguide.controller;
import com.onixbyte.deltaforceguide.domain.dto.FirearmResponse;
import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
import com.onixbyte.deltaforceguide.enumeration.FirearmType;
import com.onixbyte.deltaforceguide.service.FirearmService;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
@@ -30,9 +31,10 @@ public class FirearmController {
@RequestParam(defaultValue = "0") @Min(0) int page,
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int size,
@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}")
@@ -5,6 +5,7 @@ import com.onixbyte.deltaforceguide.domain.dto.PageResponse;
import com.onixbyte.deltaforceguide.service.ModificationService;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Positive;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
@@ -29,10 +30,11 @@ public class ModificationController {
public PageResponse<ModificationResponse> pageQuery(
@RequestParam(defaultValue = "0") @Min(0) int page,
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int size,
@RequestParam(required = false) @Positive Long firearmId,
@RequestParam(defaultValue = "id") String sortBy,
@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}")
@@ -1,10 +1,15 @@
package com.onixbyte.deltaforceguide.repository;
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.stereotype.Repository;
@Repository
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"})
Page<Modification> findAllBy(Pageable pageable);
@EntityGraph(attributePaths = {"firearm"})
Page<Modification> findAllByFirearm_Id(Long firearmId, Pageable pageable);
@Override
@EntityGraph(attributePaths = {"firearm"})
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.PageResponse;
import com.onixbyte.deltaforceguide.domain.entity.Firearm;
import com.onixbyte.deltaforceguide.enumeration.FirearmType;
import com.onixbyte.deltaforceguide.repository.FirearmRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@@ -19,11 +22,12 @@ public class FirearmService {
}
@Transactional(readOnly = true)
public PageResponse<FirearmResponse> pageQuery(Pageable pageable) {
return PageResponse.from(
firearmRepository.findAll(pageable)
.map(FirearmResponse::from)
);
public PageResponse<FirearmResponse> pageQuery(FirearmType type, Pageable pageable) {
Page<Firearm> page = type == null
? firearmRepository.findAll(pageable)
: firearmRepository.findAllByType(type, pageable);
return PageResponse.from(page.map(FirearmResponse::from));
}
@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.PageResponse;
import com.onixbyte.deltaforceguide.domain.entity.Modification;
import com.onixbyte.deltaforceguide.repository.ModificationRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@@ -19,11 +21,12 @@ public class ModificationService {
}
@Transactional(readOnly = true)
public PageResponse<ModificationResponse> pageQuery(Pageable pageable) {
return PageResponse.from(
modificationRepository.findAllBy(pageable)
.map(ModificationResponse::from)
);
public PageResponse<ModificationResponse> pageQuery(Long firearmId, Pageable pageable) {
Page<Modification> page = firearmId == null
? modificationRepository.findAllBy(pageable)
: modificationRepository.findAllByFirearm_Id(firearmId, pageable);
return PageResponse.from(page.map(ModificationResponse::from));
}
@Transactional(readOnly = true)