Skip to content

Commit

Permalink
feat: add filtration on CargoType for "/cargo/all" GET endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
unior1234 committed Nov 12, 2023
1 parent c12972c commit 429208e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
5 changes: 3 additions & 2 deletions crude-server/src/main/java/ru/oil/cargo/CargoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ru.oil.cargo.dto.CargoDto;
import ru.oil.enums.CargoType;

import javax.validation.Valid;
import java.util.List;
Expand All @@ -25,8 +26,8 @@ public CargoDto add(@Valid @RequestBody CargoDto cargoDto) {
}

@GetMapping("/all")
public List<CargoDto> getAll() {
return cargoService.getAll();
public List<CargoDto> getAll(@RequestParam(required = false) CargoType type) {
return cargoService.getAll(type);
}

@PutMapping("/update")
Expand Down
4 changes: 4 additions & 0 deletions crude-server/src/main/java/ru/oil/cargo/CargoRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import ru.oil.enums.CargoType;

import java.util.List;
import java.util.Optional;

@Repository
public interface CargoRepository extends JpaRepository<Cargo, Long> {
Optional<Cargo> findByName(String name);

List<Cargo> findAllByType(CargoType type);
}
9 changes: 7 additions & 2 deletions crude-server/src/main/java/ru/oil/cargo/CargoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.oil.cargo.dto.CargoDto;
import ru.oil.enums.CargoType;
import ru.oil.units.api.Api;
import ru.oil.units.temperature.Temperature;

Expand Down Expand Up @@ -54,8 +55,12 @@ public CargoDto update(CargoDto cargoDto, Long id) {
}

@Override
public List<CargoDto> getAll() {
return repository.findAll().stream().map(CargoMapper::cargoToDto).collect(Collectors.toList());
public List<CargoDto> getAll(CargoType type) {
if (type == null) {
return repository.findAll().stream().map(CargoMapper::cargoToDto).collect(Collectors.toList());
} else {
return repository.findAllByType(type).stream().map(CargoMapper::cargoToDto).collect(Collectors.toList());
}
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion crude-server/src/main/java/ru/oil/cargo/ICargoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ru.oil.cargo.dto.CargoDto;
import org.springframework.stereotype.Service;
import ru.oil.enums.CargoType;

import java.util.List;

Expand All @@ -10,7 +11,7 @@ public interface ICargoService {

CargoDto add(CargoDto cargoDto);

List<CargoDto> getAll();
List<CargoDto> getAll(CargoType type);

CargoDto getByName(String name);

Expand Down

0 comments on commit 429208e

Please sign in to comment.