From 096e9074460454f59084cc91addb4f15d185eca8 Mon Sep 17 00:00:00 2001 From: Hedi Date: Thu, 20 Jan 2022 09:41:07 +0100 Subject: [PATCH] debut de tache controller --- .../controller/EvenementController.java | 37 ++++++ .../organizee/controller/TacheController.java | 85 ++++++++++++++ .../java/fr/organizee/model/Evenement.java | 107 ++++++++++++++++++ .../repository/EvenementRepository.java | 9 ++ .../organizee/repository/TacheRepository.java | 7 +- 5 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 src/main/java/fr/organizee/controller/EvenementController.java create mode 100644 src/main/java/fr/organizee/controller/TacheController.java create mode 100644 src/main/java/fr/organizee/model/Evenement.java create mode 100644 src/main/java/fr/organizee/repository/EvenementRepository.java diff --git a/src/main/java/fr/organizee/controller/EvenementController.java b/src/main/java/fr/organizee/controller/EvenementController.java new file mode 100644 index 0000000..a5c3673 --- /dev/null +++ b/src/main/java/fr/organizee/controller/EvenementController.java @@ -0,0 +1,37 @@ +package fr.organizee.controller; + +import fr.organizee.model.Evenement; +import fr.organizee.model.Membre; +import fr.organizee.model.Team; +import fr.organizee.repository.EvenementRepository; +import fr.organizee.repository.TeamRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; + +@RestController +@CrossOrigin("*") +@RequestMapping("/evenements") +public class EvenementController { + + @Autowired + private EvenementRepository evenementRepo; + + // Recupérer tout les evenements pour une team {id} + @GetMapping(value = "/all/{id}") + public ResponseEntity getAll(){ + List liste = null; + try + { + liste = evenementRepo.findAll(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } + + return ResponseEntity.status(HttpStatus.OK).body(liste); + } +} diff --git a/src/main/java/fr/organizee/controller/TacheController.java b/src/main/java/fr/organizee/controller/TacheController.java new file mode 100644 index 0000000..8408ada --- /dev/null +++ b/src/main/java/fr/organizee/controller/TacheController.java @@ -0,0 +1,85 @@ +package fr.organizee.controller; + +import fr.organizee.model.Membre; +import fr.organizee.model.Tache; +import fr.organizee.repository.MembreRepository; +import fr.organizee.repository.TacheRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.persistence.EntityNotFoundException; +import java.util.List; +import java.util.Optional; + +@RestController +@CrossOrigin("*") +@RequestMapping("/taches") +public class TacheController { + @Autowired + private TacheRepository tacheRepo; + + @GetMapping(value = "/all") + public ResponseEntity getAll(){ + List liste = null; + try + { + liste = tacheRepo.findAll(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } + + return ResponseEntity.status(HttpStatus.OK).body(liste); + } + + @GetMapping(value = "/{id}") + public ResponseEntity findById(@PathVariable int id){ + Optional tache = null; + try + { + tache = tacheRepo.findById(id); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } + + return ResponseEntity.status(HttpStatus.OK).body(tache); + } + + @DeleteMapping(value = "/delete/{id}") + public ResponseEntity deleteTache(@PathVariable int id){ + try { + tacheRepo.delete(tacheRepo.getById(id)); + return ResponseEntity.status(HttpStatus.OK).body("Tache effacée !"); + + } catch (EntityNotFoundException e) { + + return ResponseEntity.status(HttpStatus.OK).body("Tache introuvable !"); + } + } + + @PostMapping(value="/add", produces="application/json", consumes="application/json") + public ResponseEntity addTache(@RequestBody Tache tache){ + Tache resultTache = null; + try { + resultTache = tacheRepo.saveAndFlush(tache); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + } + + return ResponseEntity.status(HttpStatus.CREATED).body(resultTache); + } + + @PutMapping("/update/{id}") + public ResponseEntity updateTache(@RequestBody Tache tache, @PathVariable Integer id) throws Exception { + Tache resultTache = null; + try { + resultTache = tacheRepo.save(tache); + + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); + } + + return ResponseEntity.status(HttpStatus.OK).body(resultTache); + } +} diff --git a/src/main/java/fr/organizee/model/Evenement.java b/src/main/java/fr/organizee/model/Evenement.java new file mode 100644 index 0000000..15a355f --- /dev/null +++ b/src/main/java/fr/organizee/model/Evenement.java @@ -0,0 +1,107 @@ +package fr.organizee.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import javax.persistence.*; +import java.time.LocalDateTime; + +@Entity +public class Evenement { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + private LocalDateTime eventDebut; + private LocalDateTime eventFin; + private int allDay; + private String libelle; + @ManyToOne(cascade = CascadeType.MERGE) + @JoinColumn(name="MEMBRE_ID") + @JsonIgnoreProperties("evenement") + private Membre membre; + @ManyToOne(cascade = CascadeType.MERGE) + @JoinColumn(name="TEAM_ID") + @JsonIgnoreProperties("evenement") + private Team team; + + public Evenement() { + } + + public Evenement(int id, LocalDateTime eventDebut, LocalDateTime eventFin, int allDay, String libelle, Membre membre, Team team) { + this.id = id; + this.eventDebut = eventDebut; + this.eventFin = eventFin; + this.allDay = allDay; + this.libelle = libelle; + this.membre = membre; + this.team = team; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public LocalDateTime getEventDebut() { + return eventDebut; + } + + public void setEventDebut(LocalDateTime eventDebut) { + this.eventDebut = eventDebut; + } + + public LocalDateTime getEventFin() { + return eventFin; + } + + public void setEventFin(LocalDateTime eventFin) { + this.eventFin = eventFin; + } + + public int getAllDay() { + return allDay; + } + + public void setAllDay(int allDay) { + this.allDay = allDay; + } + + public String getLibelle() { + return libelle; + } + + public void setLibelle(String libelle) { + this.libelle = libelle; + } + + public Membre getMembre() { + return membre; + } + + public void setMembre(Membre membre) { + this.membre = membre; + } + + public Team getTeam() { + return team; + } + + public void setTeam(Team team) { + this.team = team; + } + + @Override + public String toString() { + return "Evenement{" + + "id=" + id + + ", eventDebut=" + eventDebut + + ", eventFin=" + eventFin + + ", allDay=" + allDay + + ", libelle='" + libelle + '\'' + + ", membre=" + membre + + ", team=" + team + + '}'; + } +} diff --git a/src/main/java/fr/organizee/repository/EvenementRepository.java b/src/main/java/fr/organizee/repository/EvenementRepository.java new file mode 100644 index 0000000..51164c5 --- /dev/null +++ b/src/main/java/fr/organizee/repository/EvenementRepository.java @@ -0,0 +1,9 @@ +package fr.organizee.repository; + +import fr.organizee.model.Evenement; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface EvenementRepository extends JpaRepository { +} diff --git a/src/main/java/fr/organizee/repository/TacheRepository.java b/src/main/java/fr/organizee/repository/TacheRepository.java index 17c22b8..2e52ef2 100644 --- a/src/main/java/fr/organizee/repository/TacheRepository.java +++ b/src/main/java/fr/organizee/repository/TacheRepository.java @@ -1,4 +1,9 @@ package fr.organizee.repository; -public interface TacheRepository { +import fr.organizee.model.Tache; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface TacheRepository extends JpaRepository { }