diff --git a/src/main/java/com/example/postgresdemo/controller/AnswerController.java b/src/main/java/com/example/postgresdemo/controller/AnswerController.java deleted file mode 100644 index 7cfaa47..0000000 --- a/src/main/java/com/example/postgresdemo/controller/AnswerController.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.example.postgresdemo.controller; - -import com.example.postgresdemo.exception.ResourceNotFoundException; -import com.example.postgresdemo.model.Answer; -import com.example.postgresdemo.repository.AnswerRepository; -import com.example.postgresdemo.repository.QuestionRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; -import javax.validation.Valid; -import java.util.List; - -@RestController -public class AnswerController { - - @Autowired - private AnswerRepository answerRepository; - - @Autowired - private QuestionRepository questionRepository; - - @GetMapping("/questions/{questionId}/answers") - public List getAnswersByQuestionId(@PathVariable Long questionId) { - return answerRepository.findByQuestionId(questionId); - } - - @PostMapping("/questions/{questionId}/answers") - public Answer addAnswer(@PathVariable Long questionId, - @Valid @RequestBody Answer answer) { - return questionRepository.findById(questionId) - .map(question -> { - answer.setQuestion(question); - return answerRepository.save(answer); - }).orElseThrow(() -> new ResourceNotFoundException("Question not found with id " + questionId)); - } - - @PutMapping("/questions/{questionId}/answers/{answerId}") - public Answer updateAnswer(@PathVariable Long questionId, - @PathVariable Long answerId, - @Valid @RequestBody Answer answerRequest) { - if(!questionRepository.existsById(questionId)) { - throw new ResourceNotFoundException("Question not found with id " + questionId); - } - - return answerRepository.findById(answerId) - .map(answer -> { - answer.setText(answerRequest.getText()); - return answerRepository.save(answer); - }).orElseThrow(() -> new ResourceNotFoundException("Answer not found with id " + answerId)); - } - - @DeleteMapping("/questions/{questionId}/answers/{answerId}") - public ResponseEntity deleteAnswer(@PathVariable Long questionId, - @PathVariable Long answerId) { - if(!questionRepository.existsById(questionId)) { - throw new ResourceNotFoundException("Question not found with id " + questionId); - } - - return answerRepository.findById(answerId) - .map(answer -> { - answerRepository.delete(answer); - return ResponseEntity.ok().build(); - }).orElseThrow(() -> new ResourceNotFoundException("Answer not found with id " + answerId)); - - } -} diff --git a/src/main/java/com/example/postgresdemo/controller/OperatoreController.java b/src/main/java/com/example/postgresdemo/controller/OperatoreController.java new file mode 100644 index 0000000..2fa9842 --- /dev/null +++ b/src/main/java/com/example/postgresdemo/controller/OperatoreController.java @@ -0,0 +1,111 @@ +package com.example.postgresdemo.controller; + +import com.example.postgresdemo.exception.ResourceNotFoundException; +import com.example.postgresdemo.model.Operatore; +import com.example.postgresdemo.repository.OperatoreRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.List; + +/** + * Controller REST per la gestione degli operatori. + * Espone endpoint per recuperare, aggiungere, aggiornare ed eliminare operatori. + */ +@RestController +@RequestMapping("/operatori") +public class OperatoreController { + + @Autowired + private OperatoreRepository operatoreRepository; + + /** + * Recupera tutti gli operatori in modo paginato. + * + * @param pageable Oggetto che specifica la paginazione e l'ordinamento. + * @return Una pagina contenente gli operatori. + */ + @GetMapping + public Page getOperatori(Pageable pageable) { + return operatoreRepository.findAll(pageable); + } + + /** + * Recupera un operatore in base al suo ID. + * + * @param id ID dell'operatore da recuperare. + * @return L'operatore richiesto. + * @throws ResourceNotFoundException Se l'operatore non viene trovato. + */ + @GetMapping("/{id}") + public Operatore getOperatoreById(@PathVariable Integer id) { + return operatoreRepository.findOperatoreByIdOperatore(id) + .orElseThrow(() -> new ResourceNotFoundException("Operatore not found with id " + id)); + } + + /** + * Ricerca operatori per nome. + * + * @param nome Nome dell'operatore da cercare. + * @return Una lista di operatori che corrispondono al nome specificato. + */ + @GetMapping("/search") + public ResponseEntity> findOperatoriByNome(@RequestParam String nome) { + List operatori = operatoreRepository.findOperatoreByNome(nome); + if (operatori.isEmpty()) { + return ResponseEntity.notFound().build(); + } + return ResponseEntity.ok(operatori); + } + + /** + * Crea un nuovo operatore. + * + * @param operatore L'oggetto Operatore da salvare. + * @return L'oggetto Operatore salvato. + */ + @PostMapping + public Operatore createOperatore(@Valid @RequestBody Operatore operatore) { + return operatoreRepository.save(operatore); + } + + /** + * Aggiorna un operatore esistente. + * + * @param id L'ID dell'operatore da aggiornare. + * @param operatoreRequest L'oggetto Operatore con i nuovi dati. + * @return L'oggetto Operatore aggiornato. + * @throws ResourceNotFoundException Se l'operatore non viene trovato. + */ + @PutMapping("up/{id}") + public Operatore updateOperatore(@PathVariable Integer id, + @Valid @RequestBody Operatore operatoreRequest) { + return operatoreRepository.findById(Long.valueOf(id)) + .map(operatore -> { + operatore.setNome(operatoreRequest.getNome()); + operatore.setEmail(operatoreRequest.getEmail()); + operatore.setPassword(operatoreRequest.getPassword()); + return operatoreRepository.save(operatore); + }).orElseThrow(() -> new ResourceNotFoundException("Operatore not found with id " + id)); + } + + /** + * Elimina un operatore esistente. + * + * @param id L'ID dell'operatore da eliminare. + * @return ResponseEntity con stato OK se l'eliminazione avviene con successo. + * @throws ResourceNotFoundException Se l'operatore non viene trovato. + */ + @DeleteMapping("del/{id}") + public ResponseEntity deleteOperatore(@PathVariable Integer id) { + return operatoreRepository.findById(Long.valueOf(id)) + .map(operatore -> { + operatoreRepository.delete(operatore); + return ResponseEntity.ok().build(); + }).orElseThrow(() -> new ResourceNotFoundException("Operatore not found with id " + id)); + } +} diff --git a/src/main/java/com/example/postgresdemo/controller/QuestionController.java b/src/main/java/com/example/postgresdemo/controller/QuestionController.java deleted file mode 100644 index c231819..0000000 --- a/src/main/java/com/example/postgresdemo/controller/QuestionController.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.example.postgresdemo.controller; - -import com.example.postgresdemo.exception.ResourceNotFoundException; -import com.example.postgresdemo.model.Question; -import com.example.postgresdemo.repository.QuestionRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; -import javax.validation.Valid; - -@RestController -public class QuestionController { - - @Autowired - private QuestionRepository questionRepository; - - @GetMapping("/questions") - public Page getQuestions(Pageable pageable) { - return questionRepository.findAll(pageable); - } - - - @PostMapping("/questions") - public Question createQuestion(@Valid @RequestBody Question question) { - return questionRepository.save(question); - } - - @PutMapping("/questions/{questionId}") - public Question updateQuestion(@PathVariable Long questionId, - @Valid @RequestBody Question questionRequest) { - return questionRepository.findById(questionId) - .map(question -> { - question.setTitle(questionRequest.getTitle()); - question.setDescription(questionRequest.getDescription()); - return questionRepository.save(question); - }).orElseThrow(() -> new ResourceNotFoundException("Question not found with id " + questionId)); - } - - - @DeleteMapping("/questions/{questionId}") - public ResponseEntity deleteQuestion(@PathVariable Long questionId) { - return questionRepository.findById(questionId) - .map(question -> { - questionRepository.delete(question); - return ResponseEntity.ok().build(); - }).orElseThrow(() -> new ResourceNotFoundException("Question not found with id " + questionId)); - } -} diff --git a/src/main/java/com/example/postgresdemo/controller/TicketGroupController.java b/src/main/java/com/example/postgresdemo/controller/TicketGroupController.java new file mode 100644 index 0000000..84b196c --- /dev/null +++ b/src/main/java/com/example/postgresdemo/controller/TicketGroupController.java @@ -0,0 +1,38 @@ +package com.example.postgresdemo.controller; + +import com.example.postgresdemo.exception.ResourceNotFoundException; +import com.example.postgresdemo.model.TicketGroup; +import com.example.postgresdemo.repository.TicketGroupRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/ticketgroups") +public class TicketGroupController { + + @Autowired + private TicketGroupRepository ticketGroupRepository; + + // Recupera tutti i TicketGroup in modo paginato + @GetMapping + public Page getAllTickets(Pageable pageable) { + return ticketGroupRepository.findAll(pageable); + } + + // Recupera tutti i TicketGroup senza paginazione + @GetMapping("/all") + public List getAllTicketGroups() { + return ticketGroupRepository.findAllTicketGroups(); + } + + // Recupera un TicketGroup in base all'id del ticket + @GetMapping("/{id}") + public TicketGroup getTicketGroupByTicketId(@PathVariable Integer id) { + return ticketGroupRepository.findByIdTicket(id) + .orElseThrow(() -> new ResourceNotFoundException("TicketGroup not found with id_ticket " + id)); + } +} diff --git a/src/main/java/com/example/postgresdemo/model/Answer.java b/src/main/java/com/example/postgresdemo/model/Answer.java deleted file mode 100644 index b5e48d0..0000000 --- a/src/main/java/com/example/postgresdemo/model/Answer.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.example.postgresdemo.model; - -import com.fasterxml.jackson.annotation.JsonIgnore; -import org.hibernate.annotations.OnDelete; -import org.hibernate.annotations.OnDeleteAction; - -import javax.persistence.*; - -@Entity -@Table(name = "answers") -public class Answer extends AuditModel { - @Id - @GeneratedValue(generator = "answer_generator") - @SequenceGenerator( - name = "answer_generator", - sequenceName = "answer_sequence", - initialValue = 1000 - ) - private Long id; - - @Column(columnDefinition = "text") - private String text; - - @ManyToOne(fetch = FetchType.LAZY, optional = false) - @JoinColumn(name = "question_id", nullable = false) - @OnDelete(action = OnDeleteAction.CASCADE) - @JsonIgnore - private Question question; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public Question getQuestion() { - return question; - } - - public void setQuestion(Question question) { - this.question = question; - } -} diff --git a/src/main/java/com/example/postgresdemo/model/AuditModel.java b/src/main/java/com/example/postgresdemo/model/AuditModel.java index a61a3b8..82bea3c 100644 --- a/src/main/java/com/example/postgresdemo/model/AuditModel.java +++ b/src/main/java/com/example/postgresdemo/model/AuditModel.java @@ -8,6 +8,10 @@ import java.io.Serializable; import java.util.Date; +/** + * Classe astratta che fornisce funzionalità di audit (creazione e aggiornamento) per le entità. + * La classe gestisce automaticamente i timestamp di creazione e modifica di un'entità. + */ @MappedSuperclass @EntityListeners(AuditingEntityListener.class) @JsonIgnoreProperties( @@ -15,29 +19,58 @@ allowGetters = true ) public abstract class AuditModel implements Serializable { + + /** + * Data e ora di creazione dell'entità. + * Questo campo non può essere aggiornato dopo la creazione. + */ @Temporal(TemporalType.TIMESTAMP) @Column(name = "created_at", nullable = false, updatable = false) @CreatedDate private Date createdAt; + /** + * Data e ora dell'ultimo aggiornamento dell'entità. + * Questo campo viene aggiornato automaticamente ogni volta che l'entità viene modificata. + */ @Temporal(TemporalType.TIMESTAMP) @Column(name = "updated_at", nullable = false) @LastModifiedDate private Date updatedAt; + /** + * Restituisce la data di creazione dell'entità. + * + * @return La data di creazione. + */ public Date getCreatedAt() { return createdAt; } + /** + * Imposta la data di creazione dell'entità. + * + * @param createdAt La data di creazione da impostare. + */ public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; } + /** + * Restituisce la data dell'ultimo aggiornamento dell'entità. + * + * @return La data dell'ultimo aggiornamento. + */ public Date getUpdatedAt() { return updatedAt; } + /** + * Imposta la data dell'ultimo aggiornamento dell'entità. + * + * @param updatedAt La data dell'ultimo aggiornamento da impostare. + */ public void setUpdatedAt(Date updatedAt) { this.updatedAt = updatedAt; } -} \ No newline at end of file +} diff --git a/src/main/java/com/example/postgresdemo/model/Operatore.java b/src/main/java/com/example/postgresdemo/model/Operatore.java new file mode 100644 index 0000000..f948dba --- /dev/null +++ b/src/main/java/com/example/postgresdemo/model/Operatore.java @@ -0,0 +1,76 @@ +package com.example.postgresdemo.model; + +import javax.persistence.*; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; + +/** + * Entity che rappresenta l'operatore. + */ +@Entity +@Table(name = "operatore") +public class Operatore { + + /** + * Identificativo univoco dell'operatore. + */ + @Id + @Column(name = "id_operatore") + private Integer idOperatore; + + /** + * Nome dell'operatore, campo obbligatorio e massimo 20 caratteri. + */ + @NotBlank + @Size(max = 20) + @Column(name = "nome", nullable = false, length = 20) + private String nome; + + /** + * Email dell'operatore, massimo 50 caratteri. + */ + @Size(max = 50) + @Column(name = "email", length = 50) + private String email; + + /** + * Password dell'operatore, massimo 200 caratteri. + */ + @Size(max = 200) + @Column(name = "password", length = 200) + private String password; + + // Getter e Setter + + public Integer getIdOperatore() { + return idOperatore; + } + + public void setIdOperatore(Integer idOperatore) { + this.idOperatore = idOperatore; + } + + public String getNome() { + return nome; + } + + public void setNome(String nome) { + this.nome = nome; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/src/main/java/com/example/postgresdemo/model/Question.java b/src/main/java/com/example/postgresdemo/model/Question.java deleted file mode 100644 index d16a459..0000000 --- a/src/main/java/com/example/postgresdemo/model/Question.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.example.postgresdemo.model; - -import javax.persistence.*; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.Size; - -@Entity -@Table(name = "questions") -public class Question extends AuditModel { - @Id - @GeneratedValue(generator = "question_generator") - @SequenceGenerator( - name = "question_generator", - sequenceName = "question_sequence", - initialValue = 1000 - ) - private Long id; - - @NotBlank - @Size(min = 3, max = 100) - private String title; - - @Column(columnDefinition = "text") - private String description; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } -} diff --git a/src/main/java/com/example/postgresdemo/model/TicketGroup.java b/src/main/java/com/example/postgresdemo/model/TicketGroup.java new file mode 100644 index 0000000..5385515 --- /dev/null +++ b/src/main/java/com/example/postgresdemo/model/TicketGroup.java @@ -0,0 +1,132 @@ +package com.example.postgresdemo.model; + +import javax.persistence.*; +import javax.validation.constraints.NotNull; +import java.time.LocalDate; + +/** + * Entity che rappresenta un gruppo di ticket. + */ +@Entity +@Table(name = "ticketgroup") +public class TicketGroup { + + /** + * Identificativo univoco del ticket. + */ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id_ticket") + private Integer idTicket; + + + /** + * Tempo di apertura del ticket, campo obbligatorio. + */ + @NotNull + @Column(name = "t_open", nullable = false) + private Integer tOpen; + + /** + * Tempo di chiusura del ticket. + */ + @Column(name = "t_close") + private Integer tClose; + + /** + * Saldo iniziale. + */ + @Column(name = "saldo_i") + private Integer saldoI; + + /** + * Data del ticket. + */ + @Column(name = "data") + private LocalDate data; + + /** + * Operatore associato al ticket. + */ + @ManyToOne + @JoinColumn(name = "id_operatore", referencedColumnName = "id_operatore") + private Operatore operatore; + + /** + * Numero del gruppo. + */ + @Column(name = "ngruppo") + private Integer nGruppo; + + /** + * Numero del referente. + */ + @Column(name = "nreferente") + private Integer nReferente; + + // Getter e Setter + + public Integer getIdTicket() { + return idTicket; + } + + public void setIdTicket(Integer idTicket) { + this.idTicket = idTicket; + } + + public Integer getTOpen() { + return tOpen; + } + + public void setTOpen(Integer tOpen) { + this.tOpen = tOpen; + } + + public Integer getTClose() { + return tClose; + } + + public void setTClose(Integer tClose) { + this.tClose = tClose; + } + + public Integer getSaldoI() { + return saldoI; + } + + public void setSaldoI(Integer saldoI) { + this.saldoI = saldoI; + } + + public LocalDate getData() { + return data; + } + + public void setData(LocalDate data) { + this.data = data; + } + + public Operatore getOperatore() { + return operatore; + } + + public void setOperatore(Operatore operatore) { + this.operatore = operatore; + } + + public Integer getNGruppo() { + return nGruppo; + } + + public void setNGruppo(Integer nGruppo) { + this.nGruppo = nGruppo; + } + + public Integer getNReferente() { + return nReferente; + } + + public void setNReferente(Integer nReferente) { + this.nReferente = nReferente; + } +} diff --git a/src/main/java/com/example/postgresdemo/repository/AnswerRepository.java b/src/main/java/com/example/postgresdemo/repository/AnswerRepository.java deleted file mode 100644 index 761f91f..0000000 --- a/src/main/java/com/example/postgresdemo/repository/AnswerRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.example.postgresdemo.repository; - -import com.example.postgresdemo.model.Answer; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -import java.util.List; - -@Repository -public interface AnswerRepository extends JpaRepository { - List findByQuestionId(Long questionId); -} diff --git a/src/main/java/com/example/postgresdemo/repository/OperatoreRepository.java b/src/main/java/com/example/postgresdemo/repository/OperatoreRepository.java new file mode 100644 index 0000000..53c072b --- /dev/null +++ b/src/main/java/com/example/postgresdemo/repository/OperatoreRepository.java @@ -0,0 +1,14 @@ +package com.example.postgresdemo.repository; + +import com.example.postgresdemo.model.Operatore; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface OperatoreRepository extends JpaRepository { + List findOperatoreByNome(String nome); + Optional findOperatoreByIdOperatore(Integer id); +} diff --git a/src/main/java/com/example/postgresdemo/repository/QuestionRepository.java b/src/main/java/com/example/postgresdemo/repository/QuestionRepository.java deleted file mode 100644 index 290373d..0000000 --- a/src/main/java/com/example/postgresdemo/repository/QuestionRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.postgresdemo.repository; - -import com.example.postgresdemo.model.Question; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface QuestionRepository extends JpaRepository { -} diff --git a/src/main/java/com/example/postgresdemo/repository/TicketGroupRepository.java b/src/main/java/com/example/postgresdemo/repository/TicketGroupRepository.java new file mode 100644 index 0000000..d554eb3 --- /dev/null +++ b/src/main/java/com/example/postgresdemo/repository/TicketGroupRepository.java @@ -0,0 +1,19 @@ +package com.example.postgresdemo.repository; + +import com.example.postgresdemo.model.TicketGroup; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface TicketGroupRepository extends JpaRepository { + + Optional findByIdTicket(Integer idTicket); + + @Query(value = "SELECT * FROM ticketgroup", nativeQuery = true) + List findAllTicketGroups(); + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 35b376a..fe70a23 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) -spring.datasource.url=jdbc:postgresql://localhost:5432/postgres_demo -spring.datasource.username= postgres -spring.datasource.password= +spring.datasource.url=jdbc:postgresql://localhost:5432/ssc +spring.datasource.username= Maury +spring.datasource.password=2006 # The SQL dialect makes Hibernate generate better SQL for the chosen database spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect