Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 13589c2

Browse files
funziona
1 parent 15e8161 commit 13589c2

14 files changed

+427
-242
lines changed

src/main/java/com/example/postgresdemo/controller/AnswerController.java

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

src/main/java/com/example/postgresdemo/controller/QuestionController.java

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example.postgresdemo.controller;
2+
3+
import com.example.postgresdemo.exception.ResourceNotFoundException;
4+
import com.example.postgresdemo.model.TicketGroup;
5+
import com.example.postgresdemo.repository.TicketGroupRepository;
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.web.bind.annotation.*;
10+
11+
import java.util.List;
12+
13+
@RestController
14+
@RequestMapping("/ticketgroups")
15+
public class TicketGroupController {
16+
17+
@Autowired
18+
private TicketGroupRepository ticketGroupRepository;
19+
20+
// Recupera tutti i TicketGroup in modo paginato
21+
@GetMapping
22+
public Page<TicketGroup> getAllTickets(Pageable pageable) {
23+
return ticketGroupRepository.findAll(pageable);
24+
}
25+
26+
// Recupera tutti i TicketGroup senza paginazione
27+
@GetMapping("/all")
28+
public List<TicketGroup> getAllTicketGroups() {
29+
return ticketGroupRepository.findAllTicketGroups();
30+
}
31+
32+
// Recupera un TicketGroup in base all'id del ticket
33+
@GetMapping("/{id}")
34+
public TicketGroup getTicketGroupByTicketId(@PathVariable Integer id) {
35+
return ticketGroupRepository.findByIdTicket(id)
36+
.orElseThrow(() -> new ResourceNotFoundException("TicketGroup not found with id_ticket " + id));
37+
}
38+
}

src/main/java/com/example/postgresdemo/model/Answer.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/main/java/com/example/postgresdemo/model/AuditModel.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,69 @@
88
import java.io.Serializable;
99
import java.util.Date;
1010

11+
/**
12+
* Classe astratta che fornisce funzionalità di audit (creazione e aggiornamento) per le entità.
13+
* La classe gestisce automaticamente i timestamp di creazione e modifica di un'entità.
14+
*/
1115
@MappedSuperclass
1216
@EntityListeners(AuditingEntityListener.class)
1317
@JsonIgnoreProperties(
1418
value = {"createdAt", "updatedAt"},
1519
allowGetters = true
1620
)
1721
public abstract class AuditModel implements Serializable {
22+
23+
/**
24+
* Data e ora di creazione dell'entità.
25+
* Questo campo non può essere aggiornato dopo la creazione.
26+
*/
1827
@Temporal(TemporalType.TIMESTAMP)
1928
@Column(name = "created_at", nullable = false, updatable = false)
2029
@CreatedDate
2130
private Date createdAt;
2231

32+
/**
33+
* Data e ora dell'ultimo aggiornamento dell'entità.
34+
* Questo campo viene aggiornato automaticamente ogni volta che l'entità viene modificata.
35+
*/
2336
@Temporal(TemporalType.TIMESTAMP)
2437
@Column(name = "updated_at", nullable = false)
2538
@LastModifiedDate
2639
private Date updatedAt;
2740

41+
/**
42+
* Restituisce la data di creazione dell'entità.
43+
*
44+
* @return La data di creazione.
45+
*/
2846
public Date getCreatedAt() {
2947
return createdAt;
3048
}
3149

50+
/**
51+
* Imposta la data di creazione dell'entità.
52+
*
53+
* @param createdAt La data di creazione da impostare.
54+
*/
3255
public void setCreatedAt(Date createdAt) {
3356
this.createdAt = createdAt;
3457
}
3558

59+
/**
60+
* Restituisce la data dell'ultimo aggiornamento dell'entità.
61+
*
62+
* @return La data dell'ultimo aggiornamento.
63+
*/
3664
public Date getUpdatedAt() {
3765
return updatedAt;
3866
}
3967

68+
/**
69+
* Imposta la data dell'ultimo aggiornamento dell'entità.
70+
*
71+
* @param updatedAt La data dell'ultimo aggiornamento da impostare.
72+
*/
4073
public void setUpdatedAt(Date updatedAt) {
4174
this.updatedAt = updatedAt;
4275
}
43-
}
76+
}

0 commit comments

Comments
 (0)