You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.7 KiB
51 lines
1.7 KiB
package com.example.wfwebapi.controller;
|
|
|
|
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
|
import com.example.wfwebapi.model.Caracter;
|
|
import com.example.wfwebapi.repository.CaracterRepository;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/v1/caracter")
|
|
public class CaracterController {
|
|
|
|
@Autowired
|
|
private CaracterRepository caracterRepository;
|
|
|
|
@GetMapping("/{id}")
|
|
public Caracter getCaracterById(@PathVariable Long id) {
|
|
return caracterRepository.findById(id)
|
|
.orElseThrow(() -> new ResourceNotFoundException("Caracter non trouvé : " + id));
|
|
}
|
|
|
|
@GetMapping("/")
|
|
public List<Caracter> getCaracters() {
|
|
return caracterRepository.findAll();
|
|
}
|
|
|
|
@PostMapping
|
|
public Caracter createCaracter(@RequestBody Caracter caracter) {
|
|
return caracterRepository.save(caracter);
|
|
}
|
|
|
|
@PutMapping
|
|
public Caracter updateCaracter(@RequestBody Caracter updatedCaracter) {
|
|
return caracterRepository.findById(updatedCaracter.getId())
|
|
.map(c -> {
|
|
c.setCaracter(updatedCaracter.getCaracter());
|
|
c.setImage(updatedCaracter.getImage());
|
|
return caracterRepository.save(c);
|
|
}).orElseThrow(() -> new ResourceNotFoundException("Caracter non trouvé : " + updatedCaracter.getId()));
|
|
}
|
|
|
|
@DeleteMapping
|
|
public void deleteCaracter(@RequestParam Long id) {
|
|
if (!caracterRepository.existsById(id))
|
|
throw new ResourceNotFoundException("Caracter non trouvé : " + id);
|
|
caracterRepository.deleteById(id);
|
|
}
|
|
}
|