Merge pull request #4 from HediMjid/hedii

modif class membre et team
This commit is contained in:
Hédi 2022-01-08 18:34:20 +01:00 committed by GitHub
commit d4208af0e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 159 additions and 37 deletions

View File

@ -1,9 +1,9 @@
package fr.organizee.controller; package fr.organizee.controller;
import fr.organizee.model.Membre; import fr.organizee.model.Membre;
import fr.organizee.model.Team; //import fr.organizee.model.Team;
import fr.organizee.repository.MembreRepository; 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.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -16,15 +16,16 @@ import java.util.Optional;
/* toto */ /* toto */
@RestController @RestController
@CrossOrigin("*") @CrossOrigin("*")
@RequestMapping("/membres")
public class MembreController { public class MembreController {
@Autowired @Autowired
private MembreRepository membreRepo; private MembreRepository membreRepo;
@Autowired // @Autowired
private TeamRepository teamRepo; // private TeamRepository teamRepo;
@RequestMapping("/") // @RequestMapping("/membres")
@ResponseBody @ResponseBody
public String home() public String home()
{ {
@ -34,7 +35,7 @@ public class MembreController {
return sb.toString(); return sb.toString();
} }
@GetMapping(value = "/membres/all") @GetMapping(value = "/all")
public ResponseEntity<?> getAll(){ public ResponseEntity<?> getAll(){
List<Membre> liste = null; List<Membre> liste = null;
try try
@ -47,20 +48,20 @@ public class MembreController {
return ResponseEntity.status(HttpStatus.OK).body(liste); return ResponseEntity.status(HttpStatus.OK).body(liste);
} }
@GetMapping(value = "/team/all") // @GetMapping(value = "/team/all")
public ResponseEntity<?> getAllTeam(){ // public ResponseEntity<?> getAllTeam(){
List<Team> liste = null; // List<Team> liste = null;
try // try
{ // {
liste = teamRepo.findAll(); // liste = teamRepo.findAll();
} catch (Exception e) { // } catch (Exception e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); // return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
} // }
//
// return ResponseEntity.status(HttpStatus.OK).body(liste);
// }
return ResponseEntity.status(HttpStatus.OK).body(liste); @GetMapping(value = "/{id}")
}
@GetMapping(value = "/membres/{id}")
public ResponseEntity<?> findById(@PathVariable int id){ public ResponseEntity<?> findById(@PathVariable int id){
Optional<Membre> membre = null; Optional<Membre> membre = null;
try try
@ -105,16 +106,64 @@ public class MembreController {
return ResponseEntity.status(HttpStatus.CREATED).body(resultMembre); return ResponseEntity.status(HttpStatus.CREATED).body(resultMembre);
} }
@GetMapping(value = "/team/{id}") @PutMapping("/update/{id}")
public ResponseEntity<?> findTeamById(@PathVariable int id){ public ResponseEntity<?> updateMembre(@RequestBody Membre membre, @PathVariable Integer id) throws Exception {
Optional<Team> liste = null; Membre resultMembre = null;
try try {
{ resultMembre = membreRepo.save(membre);
liste = teamRepo.findById(id);
} catch (Exception e) { } 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<Team> 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);
} }
} }

View File

@ -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("<h1>Affichages des teams</h1>");
sb.append("<ul><li><a href='http://localhost:8080/teams/all'>Liste des <strong>teams</strong></a></li>");
return sb.toString();
}
@GetMapping(value = "/all")
public ResponseEntity<?> getAllTeam(){
List<Team> 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<Team> 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);
}
}

View File

@ -1,5 +1,7 @@
package fr.organizee.model; package fr.organizee.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*; import javax.persistence.*;
import java.time.LocalDate; import java.time.LocalDate;
@ -14,20 +16,22 @@ public class Contact {
private String email; private String email;
private String adresse; private String adresse;
private LocalDate dateNaissance; private LocalDate dateNaissance;
/* @ManyToOne(fetch= FetchType.EAGER) @ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name="TEAM_ID") @JoinColumn(name="TEAM_ID")
private Team team;*/ @JsonIgnoreProperties("contact")
private Team team;
public Contact() { 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.nom = nom;
this.prenom = prenom; this.prenom = prenom;
this.telephone = telephone; this.telephone = telephone;
this.email = email; this.email = email;
this.adresse = adresse; this.adresse = adresse;
this.dateNaissance = dateNaissance; this.dateNaissance = dateNaissance;
this.team = team;
} }
public int getId() { public int getId() {
@ -86,6 +90,13 @@ public class Contact {
this.dateNaissance = dateNaissance; this.dateNaissance = dateNaissance;
} }
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
@Override @Override
public String toString() { public String toString() {
return "Contact{" + return "Contact{" +

View File

@ -1,5 +1,7 @@
package fr.organizee.model; package fr.organizee.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*; import javax.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -10,11 +12,12 @@ public class Team {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id; private int id;
private String nom; private String nom;
@OneToMany(mappedBy = "team", cascade = CascadeType.ALL, orphanRemoval = true) @OneToMany(mappedBy = "team", fetch=FetchType.LAZY)
//@OneToMany @JsonIgnoreProperties("team")
private List<Membre> membres = new ArrayList<>(); private List<Membre> membres = new ArrayList<>();
@OneToMany @OneToMany(mappedBy = "team", fetch=FetchType.LAZY)
@JoinTable(name = "repertoire") @JsonIgnoreProperties("team")
//@JoinTable(name = "repertoire")
private List<Contact> contacts = new ArrayList<>(); private List<Contact> contacts = new ArrayList<>();
@OneToMany @OneToMany
@JoinTable(name = "team_todolist") @JoinTable(name = "team_todolist")

View File

@ -6,5 +6,5 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public interface MembreRepository extends JpaRepository<Membre, Integer> { public interface MembreRepository extends JpaRepository<Membre, Integer> {
Membre findByNom(String nom);
} }

View File

@ -1,7 +1,7 @@
# base de données MySQL # 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.url=jdbc:mysql://192.168.1.16:3306/jpa?useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
spring.datasource.username= spring.datasource.username=desktop
spring.datasource.password= spring.datasource.password=Cosmoc4t$77
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# log # log
#logging.level.root=INFO #logging.level.root=INFO