controller
controller
controllers;
import com.simplon.dvdstore.exceptions.DvdNotFoundException;
import com.simplon.dvdstore.services.DvdServiceModel;
import com.simplon.dvdstore.services.DvdStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.ArrayList;
import java.util.Optional;
//@Controller
@RestController // donnees json ou xml
@RequestMapping("dvds")
public class DvdStoreController {
@Autowired
DvdStoreService dvdStoreService;
@PostMapping //
public boolean add(@RequestBody DvdStoreDTO dvdStoreDTO )
{
DvdServiceModel dvdServiceModel = new
DvdServiceModel(dvdStoreDTO.getName(), dvdStoreDTO.getGenre());
System.out.println(dvdServiceModel.getGenre());
return dvdStoreService.add(dvdServiceModel);
@GetMapping
public ArrayList<DvdStoreGetDTO> findAll(){
// dvdServiceModels.forEach((item)->System.out.println(item.toString()));
dvdServiceModels.forEach((item)->dvdStoreDTOSs.add(new
DvdStoreGetDTO(item.getId().get(), item.getName(), item.getGenre())) );
return dvdStoreDTOSs;
}
@GetMapping("/{id}")
public ResponseEntity<DvdStoreDTO> findById(@PathVariable Long id){
try{
DvdServiceModel dvdServiceModel = dvdStoreService.findById(id);
dvdStoreDTO.setName(dvdServiceModel.getName()) ;
dvdStoreDTO.setGenre(dvdServiceModel.getGenre());
return new ResponseEntity<>(dvdStoreDTO, HttpStatus.OK) ;
}catch(DvdNotFoundException ex){
System.out.println(ex.getReason());
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, ex.getReason() );
// if (dvdStoreService.findById(id ) != null ){
// DvdServiceModel dvdServiceModel =
dvdStoreService.findById(id);
// dvdStoreDTO.setName(dvdServiceModel.getName()) ;
// dvdStoreDTO.setGenre(dvdServiceModel.getGenre());
// return new ResponseEntity<>(dvdStoreDTO, HttpStatus.OK) ;
// }else{
// throw new DvdNotFoundException(id);
// }
@PutMapping("/{id}")
public ResponseEntity<String> update(@PathVariable Long id, @RequestBody
DvdStoreDTO dvdStoreDTO) throws DvdNotFoundException {
if (dvdStoreService.findById(id ) != null ){
DvdServiceModel dvdServiceModel = new
DvdServiceModel(Optional.ofNullable(id), dvdStoreDTO.getName(),
dvdStoreDTO.getGenre());
dvdStoreService.update(id, dvdServiceModel);
@DeleteMapping("/{id}")
public ResponseEntity<String> delete(@PathVariable Long id) throws
DvdNotFoundException{
if(dvdStoreService.findById(id) != null ){
dvdStoreService.delete(id);
return new ResponseEntity<>("le dvd id : " + id + " a été supprimé",
HttpStatus.OK);
}else{
// throw new NotFoundException(id);
return new ResponseEntity<>("le dvd id : " + id + " n'a pas été
trouvé", HttpStatus.NOT_FOUND);
}
}
@DeleteMapping
public String deleteAll(){
return dvdStoreService.deleteAll();