|
| 1 | +package com.example.postgresdemo.controller; |
| 2 | + |
| 3 | +import com.example.postgresdemo.exception.ResourceNotFoundException; |
| 4 | +import com.example.postgresdemo.model.Operatore; |
| 5 | +import com.example.postgresdemo.repository.OperatoreRepository; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.data.domain.Page; |
| 8 | +import org.springframework.data.domain.Pageable; |
| 9 | +import org.springframework.http.ResponseEntity; |
| 10 | +import org.springframework.web.bind.annotation.*; |
| 11 | + |
| 12 | +import javax.validation.Valid; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +/** |
| 16 | + * Controller REST per la gestione degli operatori. |
| 17 | + * Espone endpoint per recuperare, aggiungere, aggiornare ed eliminare operatori. |
| 18 | + */ |
| 19 | +@RestController |
| 20 | +@RequestMapping("/operatori") |
| 21 | +public class OperatoreController { |
| 22 | + |
| 23 | + @Autowired |
| 24 | + private OperatoreRepository operatoreRepository; |
| 25 | + |
| 26 | + /** |
| 27 | + * Recupera tutti gli operatori in modo paginato. |
| 28 | + * |
| 29 | + * @param pageable Oggetto che specifica la paginazione e l'ordinamento. |
| 30 | + * @return Una pagina contenente gli operatori. |
| 31 | + */ |
| 32 | + @GetMapping |
| 33 | + public Page<Operatore> getOperatori(Pageable pageable) { |
| 34 | + return operatoreRepository.findAll(pageable); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Recupera un operatore in base al suo ID. |
| 39 | + * |
| 40 | + * @param id ID dell'operatore da recuperare. |
| 41 | + * @return L'operatore richiesto. |
| 42 | + * @throws ResourceNotFoundException Se l'operatore non viene trovato. |
| 43 | + */ |
| 44 | + @GetMapping("/{id}") |
| 45 | + public Operatore getOperatoreById(@PathVariable Integer id) { |
| 46 | + return operatoreRepository.findOperatoreByIdOperatore(id) |
| 47 | + .orElseThrow(() -> new ResourceNotFoundException("Operatore not found with id " + id)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Ricerca operatori per nome. |
| 52 | + * |
| 53 | + * @param nome Nome dell'operatore da cercare. |
| 54 | + * @return Una lista di operatori che corrispondono al nome specificato. |
| 55 | + */ |
| 56 | + @GetMapping("/search") |
| 57 | + public ResponseEntity<List<Operatore>> findOperatoriByNome(@RequestParam String nome) { |
| 58 | + List<Operatore> operatori = operatoreRepository.findOperatoreByNome(nome); |
| 59 | + if (operatori.isEmpty()) { |
| 60 | + return ResponseEntity.notFound().build(); |
| 61 | + } |
| 62 | + return ResponseEntity.ok(operatori); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Crea un nuovo operatore. |
| 67 | + * |
| 68 | + * @param operatore L'oggetto Operatore da salvare. |
| 69 | + * @return L'oggetto Operatore salvato. |
| 70 | + */ |
| 71 | + @PostMapping |
| 72 | + public Operatore createOperatore(@Valid @RequestBody Operatore operatore) { |
| 73 | + return operatoreRepository.save(operatore); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Aggiorna un operatore esistente. |
| 78 | + * |
| 79 | + * @param id L'ID dell'operatore da aggiornare. |
| 80 | + * @param operatoreRequest L'oggetto Operatore con i nuovi dati. |
| 81 | + * @return L'oggetto Operatore aggiornato. |
| 82 | + * @throws ResourceNotFoundException Se l'operatore non viene trovato. |
| 83 | + */ |
| 84 | + @PutMapping("up/{id}") |
| 85 | + public Operatore updateOperatore(@PathVariable Integer id, |
| 86 | + @Valid @RequestBody Operatore operatoreRequest) { |
| 87 | + return operatoreRepository.findById(Long.valueOf(id)) |
| 88 | + .map(operatore -> { |
| 89 | + operatore.setNome(operatoreRequest.getNome()); |
| 90 | + operatore.setEmail(operatoreRequest.getEmail()); |
| 91 | + operatore.setPassword(operatoreRequest.getPassword()); |
| 92 | + return operatoreRepository.save(operatore); |
| 93 | + }).orElseThrow(() -> new ResourceNotFoundException("Operatore not found with id " + id)); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Elimina un operatore esistente. |
| 98 | + * |
| 99 | + * @param id L'ID dell'operatore da eliminare. |
| 100 | + * @return ResponseEntity con stato OK se l'eliminazione avviene con successo. |
| 101 | + * @throws ResourceNotFoundException Se l'operatore non viene trovato. |
| 102 | + */ |
| 103 | + @DeleteMapping("del/{id}") |
| 104 | + public ResponseEntity<?> deleteOperatore(@PathVariable Integer id) { |
| 105 | + return operatoreRepository.findById(Long.valueOf(id)) |
| 106 | + .map(operatore -> { |
| 107 | + operatoreRepository.delete(operatore); |
| 108 | + return ResponseEntity.ok().build(); |
| 109 | + }).orElseThrow(() -> new ResourceNotFoundException("Operatore not found with id " + id)); |
| 110 | + } |
| 111 | +} |
0 commit comments