feat: added paginated request

This commit is contained in:
Marcin Alchimowicz 2024-01-17 23:11:39 +01:00
parent 4f630016ca
commit c01e29d6d2
4 changed files with 65 additions and 12 deletions

View File

@ -1,14 +1,11 @@
package com.example.prapro2spring.controller;
import com.example.prapro2spring.model.Person;
import com.example.prapro2spring.dto.PersonValue;
import com.example.prapro2spring.dto.AllPeopleValues;
import com.example.prapro2spring.repository.PeopleRepository;
import com.example.prapro2spring.service.PersonValueService;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.prapro2spring.repository.PersonValueRepository;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/main")
public class MainController {
@ -16,16 +13,16 @@ public class MainController {
@Autowired
private PeopleRepository peopleRepository;
@Autowired
private PersonValueRepository personValueRepository;
private PersonValueService PersonValueService;
@GetMapping
public List<PersonValue> getAllPeopleValues() {
return personValueRepository.queryAllPersonValues();
@GetMapping("/")
public AllPeopleValues getAllPeopleValues(@RequestParam(value = "startIndex", defaultValue = "0") Integer startIndex) {
return PersonValueService.getAllPersonValues(startIndex);
}
@GetMapping("/{id}")
public Integer getPersonValuesById(@PathVariable Integer id){
return personValueRepository.queryPersonValuesById(id);
return PersonValueService.getPersonValuesById(id);
}
}

View File

@ -0,0 +1,24 @@
package com.example.prapro2spring.dto;
import java.util.List;
public class AllPeopleValues {
private List<PersonValue> peopleValues;
private Integer lastIndex;
public AllPeopleValues(List<PersonValue> peopleValues, Integer lastIndex) {
this.peopleValues = peopleValues;
this.lastIndex = lastIndex;
}
public AllPeopleValues() {
}
public List<PersonValue> getPeopleValues() {
return peopleValues;
}
public Integer getLastIndex() {
return lastIndex;
}
}

View File

@ -11,6 +11,6 @@ public interface PersonValueRepository extends CrudRepository<PersonDTO, Integer
@Query(value = "SELECT people_scoreboard.get_human_value(?1)", nativeQuery = true)
Integer queryPersonValuesById(Integer id);
@Query(value = "SELECT * FROM people_scoreboard.get_all_people_values()", nativeQuery = true)
List<PersonValue> queryAllPersonValues();
@Query(value = "SELECT * FROM people_scoreboard.get_all_people_values(?1, 3)", nativeQuery = true)
List<Object[]> queryAllPersonValues(Integer startIndex);
}

View File

@ -0,0 +1,32 @@
package com.example.prapro2spring.service;
import com.example.prapro2spring.dto.AllPeopleValues;
import com.example.prapro2spring.dto.PersonValue;
import com.example.prapro2spring.repository.PersonValueRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class PersonValueService {
@Autowired
PersonValueRepository personValueRepository;
public AllPeopleValues getAllPersonValues(Integer lastIndex) {
List<Object[]> results = personValueRepository.queryAllPersonValues(lastIndex);
List<PersonValue> personValues = new ArrayList<>();
for (Object[] result : results) {
PersonValue personValue = new PersonValue((Integer) result[0], (Integer) result[1]);
personValues.add(personValue);
}
return new AllPeopleValues(personValues, personValues.size());
}
public Integer getPersonValuesById(Integer id) {
return personValueRepository.queryPersonValuesById(id);
}
}