1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| package com.example.FirstSpringProject.web;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.example.FirstSpringProject.domain.Car;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
@Controller
public class CarController {
public static List<Car> cars = new ArrayList<>();
public CarController(){
Car car1 = new Car("AA33BB", false, 0, null, null, "Nice BMW");
cars.add(car1);
Car car2 = new Car("AA34ND", true, 0, null, null, "Red Ferrari");
cars.add(car2);
Car car3 = new Car("FR00SS", false, 0, null, null, "Blue Mini");
cars.add(car3);
Car car4 = new Car("FR11SS", false, 0, null, null, "Blue Mini 2");
cars.add(car4);
}
@RequestMapping(value = "/cars", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public List<Car> getAllCars(){
List<Car> cars2 = new ArrayList<>();
for(Car car: cars){
if(car.isRented() == false){
cars2.add(car);
}
}
return cars2;
}
@RequestMapping(value = "/cars/{plateNumber}", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Car aCar(@PathVariable("plateNumber") String plateNumber) throws Exception{
for(Car car: cars){
if(car.getPlateNumber() == plateNumber){
return car;
}
}
throw new IOException("Car not found");
}
@RequestMapping(value = "/cars/{plateNumber}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK)
public void getBack(@PathVariable("plateNumber") String plateNumber) throws Exception{
}
@RequestMapping(value = "/cars/{plateNumber}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
public void rent(@PathVariable("plateNumber") String plateNumber) throws Exception{
for(Car car: cars){
if(car.getPlateNumber() == plateNumber){
car.setRented(true);
}
}
throw new IOException("Car not found");
}
@RequestMapping(value = "/voiture/{plateNumber}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
public void rentAndGetBack(@PathVariable("plateNumber") String plateNumber,
@RequestParam(value="rent", required = true)boolean rent) throws Exception{
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public String send(){
return "hello";
}
} |
Partager