Skip to content
Snippets Groups Projects

19 car tests

Merged Andrej Šimurka requested to merge 19_car_tests into milestone_2
Files
33
package cz.muni.pa165.car.rest;
import cz.muni.pa165.car.restemplate.DbGetter;
import cz.muni.pa165.car.service.CarComponentPairService;
import cz.muni.pa165.common_library.dtos.CarComponentDto;
import cz.muni.pa165.common_library.dtos.CarRequestDto;
import cz.muni.pa165.common_library.dtos.CarComponentResponseDto;
import cz.muni.pa165.common_library.dtos.CarResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@@ -24,6 +28,9 @@ public class CarComponentPairController {
CarComponentPairService carComponentService;
@Autowired
DbGetter dbGetter;
public CarComponentPairController(CarComponentPairService carComponentService) {
this.carComponentService = carComponentService;
}
@@ -65,11 +72,47 @@ public class CarComponentPairController {
* @return List of components.
*/
@Operation(summary = "Get all components of a car")
@PutMapping(path = "/getcomponents",
@GetMapping(path = "/getcomponents",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<CarComponentDto>> getAllComponentsOfCar(@RequestParam Long carId) {
public ResponseEntity<List<CarComponentResponseDto>>
getAllComponentsOfCar(@RequestParam Long carId) {
return ResponseEntity.ok(carComponentService.getAllComponentsOfCar(carId));
}
/**
* For a given car ID and a component ID of that car returns a list of components
* of the same name.
*
* @param carId Id of a car.
* @param componentId Id of a component of the given car.
* @return list of all spare components of the same name.
*/
@Operation(summary = "Get all components from component repo")
@GetMapping(path = "/getsparecomponents",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<CarComponentResponseDto>>
getAllCarComponents(@RequestParam Long carId, @RequestParam Long componentId) {
List<CarComponentResponseDto> allComponents = dbGetter.getAllComponentsFromDb();
var carComponentsResponse = getAllComponentsOfCar(carId);
var carComponents = carComponentsResponse.getBody();
CarComponentResponseDto requestedComponent = null;
assert carComponents != null;
for (CarComponentResponseDto carComponent : allComponents) {
if (carComponent.getId().equals(componentId)) {
requestedComponent = carComponent;
break;
}
}
assert requestedComponent != null;
List<CarComponentResponseDto> allViableReplacements = new ArrayList<>();
for (CarComponentResponseDto carComponent : allComponents) {
if (carComponent.getName().equals(requestedComponent.getName())
&& !carComponent.getId().equals(componentId)) {
allViableReplacements.add(carComponent);
}
}
return ResponseEntity.ok(allViableReplacements);
}
}
Loading