diff --git a/src/main/java/fr/organizee/controller/MembreController.java b/src/main/java/fr/organizee/controller/MembreController.java index 135413e..d200dc3 100644 --- a/src/main/java/fr/organizee/controller/MembreController.java +++ b/src/main/java/fr/organizee/controller/MembreController.java @@ -1,9 +1,9 @@ package fr.organizee.controller; import fr.organizee.model.Membre; -import fr.organizee.model.Team; +//import fr.organizee.model.Team; import fr.organizee.repository.MembreRepository; -import fr.organizee.repository.TeamRepository; +//import fr.organizee.repository.TeamRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -16,15 +16,16 @@ import java.util.Optional; /* toto */ @RestController @CrossOrigin("*") +@RequestMapping("/membres") public class MembreController { @Autowired private MembreRepository membreRepo; - @Autowired - private TeamRepository teamRepo; +// @Autowired +// private TeamRepository teamRepo; - @RequestMapping("/") +// @RequestMapping("/membres") @ResponseBody public String home() { @@ -34,7 +35,7 @@ public class MembreController { return sb.toString(); } - @GetMapping(value = "/membres/all") + @GetMapping(value = "/all") public ResponseEntity getAll(){ List liste = null; try @@ -47,20 +48,20 @@ public class MembreController { return ResponseEntity.status(HttpStatus.OK).body(liste); } - @GetMapping(value = "/team/all") - public ResponseEntity getAllTeam(){ - List liste = null; - try - { - liste = teamRepo.findAll(); - } catch (Exception e) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); - } +// @GetMapping(value = "/team/all") +// public ResponseEntity getAllTeam(){ +// List liste = null; +// try +// { +// liste = teamRepo.findAll(); +// } catch (Exception e) { +// return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); +// } +// +// return ResponseEntity.status(HttpStatus.OK).body(liste); +// } - return ResponseEntity.status(HttpStatus.OK).body(liste); - } - - @GetMapping(value = "/membres/{id}") + @GetMapping(value = "/{id}") public ResponseEntity findById(@PathVariable int id){ Optional membre = null; try @@ -105,16 +106,64 @@ public class MembreController { return ResponseEntity.status(HttpStatus.CREATED).body(resultMembre); } - @GetMapping(value = "/team/{id}") - public ResponseEntity findTeamById(@PathVariable int id){ - Optional liste = null; - try - { - liste = teamRepo.findById(id); + @PutMapping("/update/{id}") + public ResponseEntity updateMembre(@RequestBody Membre membre, @PathVariable Integer id) throws Exception { + Membre resultMembre = null; + try { + resultMembre = membreRepo.save(membre); + } catch (Exception e) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); } - return ResponseEntity.status(HttpStatus.OK).body(liste); + return ResponseEntity.status(HttpStatus.OK).body(resultMembre); + } + +// UPDATE SUR UN CHAMPS +// @PutMapping("/update/{id}") +// public ResponseEntity updateMembre(@RequestBody Membre membre, @PathVariable Integer id) throws Exception { +// Membre resultMembre = null; +// Membre oldMembre = membreRepo.getById(id); +// oldMembre.setNom(membre.getNom()); +// //Membre resultMembre = membreRepo.getById(id); +// try { +// resultMembre = membreRepo.save(oldMembre); +// +// } catch (Exception e) { +// return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); +// } +// +// return ResponseEntity.status(HttpStatus.OK).body(resultMembre); +// } + +// @GetMapping(value = "/team/{id}") +// public ResponseEntity findTeamById(@PathVariable int id){ +// Optional liste = null; +// try +// { +// liste = teamRepo.findById(id); +// } catch (Exception e) { +// return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); +// } +// +// return ResponseEntity.status(HttpStatus.OK).body(liste); +// } + + @PostMapping(value="/login", produces="application/json", consumes="application/json") + public ResponseEntity login(@RequestBody Membre membre){ + Membre resultMembre = null; + try { + resultMembre = membreRepo.findByNom(membre.getNom()); + if(resultMembre == null){ + throw new RuntimeException("User inexistant."); + } + if(!resultMembre.getPassword().equals(membre.getPassword())){ + throw new RuntimeException("mauvais password."); + } + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + } + + return ResponseEntity.status(HttpStatus.CREATED).body(resultMembre); } } diff --git a/src/main/java/fr/organizee/controller/TeamController.java b/src/main/java/fr/organizee/controller/TeamController.java new file mode 100644 index 0000000..68a2e1f --- /dev/null +++ b/src/main/java/fr/organizee/controller/TeamController.java @@ -0,0 +1,59 @@ +package fr.organizee.controller; + +import fr.organizee.model.Team; +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 javax.persistence.EntityNotFoundException; +import java.util.List; +import java.util.Optional; + +/* toto */ +@RestController +@CrossOrigin("*") +@RequestMapping("/teams") +public class TeamController { + + @Autowired + private TeamRepository teamRepo; + +// @RequestMapping("/teams") + @ResponseBody + public String home() + { + StringBuilder sb = new StringBuilder(); + sb.append("

Affichages des teams

"); + sb.append("
  • Liste des teams
  • "); + return sb.toString(); + } + + @GetMapping(value = "/all") + public ResponseEntity getAllTeam(){ + List liste = null; + try + { + liste = teamRepo.findAll(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } + + return ResponseEntity.status(HttpStatus.OK).body(liste); + } + + @GetMapping(value = "/{id}") + public ResponseEntity findTeamById(@PathVariable int id){ + Optional liste = null; + try + { + liste = teamRepo.findById(id); + } 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/model/Contact.java b/src/main/java/fr/organizee/model/Contact.java index 38a8111..ccd14c6 100644 --- a/src/main/java/fr/organizee/model/Contact.java +++ b/src/main/java/fr/organizee/model/Contact.java @@ -1,5 +1,7 @@ package fr.organizee.model; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + import javax.persistence.*; import java.time.LocalDate; @@ -14,20 +16,22 @@ public class Contact { private String email; private String adresse; private LocalDate dateNaissance; -/* @ManyToOne(fetch= FetchType.EAGER) + @ManyToOne(cascade = CascadeType.MERGE) @JoinColumn(name="TEAM_ID") - private Team team;*/ + @JsonIgnoreProperties("contact") + private Team team; public Contact() { } - public Contact(String nom, String prenom, String telephone, String email, String adresse, LocalDate dateNaissance) { + public Contact(String nom, String prenom, String telephone, String email, String adresse, LocalDate dateNaissance, Team team) { this.nom = nom; this.prenom = prenom; this.telephone = telephone; this.email = email; this.adresse = adresse; this.dateNaissance = dateNaissance; + this.team = team; } public int getId() { @@ -86,6 +90,13 @@ public class Contact { this.dateNaissance = dateNaissance; } + public Team getTeam() { + return team; + } + public void setTeam(Team team) { + this.team = team; + } + @Override public String toString() { return "Contact{" + diff --git a/src/main/java/fr/organizee/model/Team.java b/src/main/java/fr/organizee/model/Team.java index ce562e2..e21f803 100644 --- a/src/main/java/fr/organizee/model/Team.java +++ b/src/main/java/fr/organizee/model/Team.java @@ -1,5 +1,7 @@ package fr.organizee.model; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + import javax.persistence.*; import java.util.ArrayList; import java.util.List; @@ -10,11 +12,12 @@ public class Team { @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String nom; - @OneToMany(mappedBy = "team", cascade = CascadeType.ALL, orphanRemoval = true) - //@OneToMany + @OneToMany(mappedBy = "team", fetch=FetchType.LAZY) + @JsonIgnoreProperties("team") private List membres = new ArrayList<>(); - @OneToMany - @JoinTable(name = "repertoire") + @OneToMany(mappedBy = "team", fetch=FetchType.LAZY) + @JsonIgnoreProperties("team") + //@JoinTable(name = "repertoire") private List contacts = new ArrayList<>(); @OneToMany @JoinTable(name = "team_todolist") diff --git a/src/main/java/fr/organizee/repository/MembreRepository.java b/src/main/java/fr/organizee/repository/MembreRepository.java index 61ec145..a8aafe8 100644 --- a/src/main/java/fr/organizee/repository/MembreRepository.java +++ b/src/main/java/fr/organizee/repository/MembreRepository.java @@ -6,5 +6,5 @@ import org.springframework.stereotype.Repository; @Repository public interface MembreRepository extends JpaRepository { - + Membre findByNom(String nom); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4af3c4e..30b7fd6 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ # base de donnĂ©es MySQL spring.datasource.url=jdbc:mysql://192.168.1.16:3306/jpa?useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC -spring.datasource.username= -spring.datasource.password= +spring.datasource.username=desktop +spring.datasource.password=Cosmoc4t$77 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # log #logging.level.root=INFO