modif team et membre controller
This commit is contained in:
parent
294712656a
commit
20382c2d1b
@ -6,11 +6,9 @@ import fr.organizee.exception.ExistingUsernameException;
|
||||
import fr.organizee.exception.InvalidCredentialsException;
|
||||
import fr.organizee.exception.MembreNotFoundException;
|
||||
import fr.organizee.model.Membre;
|
||||
import fr.organizee.model.Menu;
|
||||
import fr.organizee.model.Team;
|
||||
import fr.organizee.repository.MembreRepository;
|
||||
import fr.organizee.service.MembreService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
@ -24,42 +22,39 @@ import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@RestController
|
||||
@CrossOrigin("*")
|
||||
@CrossOrigin(origins = "*")
|
||||
@RequestMapping(value = "/membres")
|
||||
@RestController
|
||||
public class MembreController {
|
||||
|
||||
@Autowired
|
||||
private MembreRepository membreRepo;
|
||||
|
||||
@Autowired
|
||||
private MembreRepository membreRepository;
|
||||
private MembreService membreService;
|
||||
@Autowired
|
||||
private BCryptPasswordEncoder passwordEncoder;
|
||||
|
||||
public MembreController() {}
|
||||
/**
|
||||
* Contrôleur Membre
|
||||
*/
|
||||
public MembreController(MembreRepository membreRepository,
|
||||
MembreService membreService,
|
||||
BCryptPasswordEncoder passwordEncoder) {
|
||||
this.membreRepository = membreRepository;
|
||||
this.membreService = membreService;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
|
||||
@ResponseBody
|
||||
public String home()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<h1>Affichages des membres</h1>");
|
||||
sb.append("<ul><li><a href='http://localhost:8088/membres/all'>Liste des <strong>membres</strong></a></li>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rechercher tous les membres
|
||||
* @return
|
||||
* http://localhost:8088/membres/all
|
||||
*
|
||||
* @return http://localhost:8088/membres/all
|
||||
*/
|
||||
@GetMapping(value = "/all")
|
||||
//@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
|
||||
public ResponseEntity<?> getAllMembres() {
|
||||
List<Membre> listeMembres;
|
||||
try
|
||||
{
|
||||
listeMembres = membreRepo.findAll();
|
||||
try {
|
||||
listeMembres = membreRepository.findAll();
|
||||
} catch (Exception e) {
|
||||
MembreNotFoundException commandeNotFoundException = new MembreNotFoundException();
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(commandeNotFoundException.getMessage());
|
||||
@ -70,8 +65,8 @@ public class MembreController {
|
||||
|
||||
/**
|
||||
* Rechercher tous les membres admin
|
||||
* @return
|
||||
* http://localhost:8088/membres/admin/all
|
||||
*
|
||||
* @return http://localhost:8088/membres/admin/all
|
||||
*/
|
||||
|
||||
@GetMapping(value = "/admin/all")
|
||||
@ -84,8 +79,8 @@ public class MembreController {
|
||||
|
||||
/**
|
||||
* Rechercher un membre par son Id
|
||||
* @return
|
||||
* http://localhost:8088/membres/1
|
||||
*
|
||||
* @return http://localhost:8088/membres/1
|
||||
*/
|
||||
|
||||
@GetMapping(value = "/{id}")
|
||||
@ -93,7 +88,7 @@ public class MembreController {
|
||||
public ResponseEntity<?> getMembreById(@PathVariable int id) {
|
||||
Optional<Membre> membre;
|
||||
try {
|
||||
membre = membreRepo.findById(id);
|
||||
membre = membreRepository.findById(id);
|
||||
if (membre.isPresent()) {
|
||||
return ResponseEntity.status(HttpStatus.OK).body(membre);
|
||||
} else {
|
||||
@ -107,15 +102,15 @@ public class MembreController {
|
||||
|
||||
/**
|
||||
* Rechercher un membre par l'Id de sa team
|
||||
* @return
|
||||
* http://localhost:8088/membres/1
|
||||
*
|
||||
* @return http://localhost:8088/membres/1
|
||||
*/
|
||||
@GetMapping(value = "team/{team_id}")
|
||||
//@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
|
||||
public ResponseEntity<?> findByTeamId(@PathVariable int team_id) {
|
||||
List<Membre> membres = null;
|
||||
try {
|
||||
membres = membreRepo.FindMembresByTeam(team_id);
|
||||
membres = membreRepository.FindMembresByTeam(team_id);
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||
}
|
||||
@ -124,8 +119,8 @@ public class MembreController {
|
||||
|
||||
/**
|
||||
* Inscription
|
||||
* @return
|
||||
* http://localhost:8088/membres/sign-up
|
||||
*
|
||||
* @return http://localhost:8088/membres/sign-up
|
||||
*/
|
||||
|
||||
@PostMapping(value = "/sign-up")
|
||||
@ -139,8 +134,8 @@ public class MembreController {
|
||||
|
||||
/**
|
||||
* Ajout d'un membre
|
||||
* @return
|
||||
* http://localhost:8088/membres/add
|
||||
*
|
||||
* @return http://localhost:8088/membres/add/1
|
||||
*/
|
||||
@PostMapping(value = "/add/{team_id}", produces = "application/json", consumes = "application/json")
|
||||
//@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
|
||||
@ -150,7 +145,7 @@ public class MembreController {
|
||||
Team team = new Team();
|
||||
team.setId(team_id);
|
||||
membre.setTeam(team);
|
||||
//resultMembre = membreRepo.saveAndFlush(membre);
|
||||
//resultMembre = membreRepository.saveAndFlush(membre);
|
||||
return ResponseEntity.ok(new JsonWebToken(membreService.signup(membre)));
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
|
||||
@ -160,21 +155,20 @@ public class MembreController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifier une commande par son Id
|
||||
* @return
|
||||
* http://localhost:8088/membres/update/1
|
||||
* Modifier un membre par son Id
|
||||
*
|
||||
* @return http://localhost:8088/membres/update/1
|
||||
*/
|
||||
@PutMapping("/update/{id}")
|
||||
//@PreAuthorize("hasRole('ROLE_PARENT')")
|
||||
public ResponseEntity<?> updateMembre(@RequestBody Membre membre, @PathVariable int id) {
|
||||
Optional<Membre> membreUpdate;
|
||||
try {
|
||||
membreUpdate = membreRepo.findById(id);
|
||||
membreUpdate = membreRepository.findById(id);
|
||||
// membre trouvé
|
||||
if (membreUpdate.isPresent()) {
|
||||
membre.setId(membreUpdate.get().getId());
|
||||
membreRepo.save(membre);
|
||||
membreRepository.save(membre);
|
||||
}
|
||||
//membre inconnu
|
||||
else {
|
||||
@ -191,15 +185,15 @@ public class MembreController {
|
||||
|
||||
/**
|
||||
* Supprimer un membre par son Id
|
||||
* @return
|
||||
* http://localhost:8088/membres/delete/1
|
||||
*
|
||||
* @return http://localhost:8088/membres/delete/1
|
||||
*/
|
||||
@DeleteMapping(value = "/delete/{id}")
|
||||
//@PreAuthorize("hasRole('ROLE_PARENT')")
|
||||
public ResponseEntity<?> deleteMembre(@PathVariable int id) {
|
||||
try {
|
||||
membreRepo.getById(id);
|
||||
membreRepo.deleteById(id);
|
||||
membreRepository.getById(id);
|
||||
membreRepository.deleteById(id);
|
||||
return ResponseEntity.status(HttpStatus.OK).body("Membre supprimé !");
|
||||
} catch (Exception e) {
|
||||
MembreNotFoundException membreNotFoundException = new MembreNotFoundException(id);
|
||||
@ -225,10 +219,10 @@ public class MembreController {
|
||||
public ResponseEntity<?> findUserByEmail(@RequestBody Membre membre) {
|
||||
Membre resultMembre = null;
|
||||
try {
|
||||
resultMembre = membreRepo.chercheEmail(membre.getEmail());
|
||||
resultMembre = membreRepository.chercheEmail(membre.getEmail());
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
resultMembre.setPassword(uuid);
|
||||
membreRepo.saveAndFlush(resultMembre);
|
||||
membreRepository.saveAndFlush(resultMembre);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(uuid);
|
||||
} catch (EntityNotFoundException e) {
|
||||
|
||||
@ -242,9 +236,9 @@ public class MembreController {
|
||||
public ResponseEntity<?> updatePassword(@RequestBody Membre membre, @PathVariable String uuid) throws Exception {
|
||||
Membre resultMembre = null;
|
||||
try {
|
||||
resultMembre = membreRepo.findByUUID(uuid);
|
||||
resultMembre = membreRepository.findByUUID(uuid);
|
||||
resultMembre.setPassword(passwordEncoder.encode(membre.getPassword()));
|
||||
membreRepo.saveAndFlush(resultMembre);
|
||||
membreRepository.saveAndFlush(resultMembre);
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
|
||||
}
|
||||
|
@ -2,43 +2,41 @@ 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.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/* toto */
|
||||
@RestController
|
||||
@CrossOrigin("*")
|
||||
|
||||
@CrossOrigin(origins = "*")
|
||||
@RequestMapping("/teams")
|
||||
@RestController
|
||||
public class TeamController {
|
||||
|
||||
@Autowired
|
||||
private TeamRepository teamRepo;
|
||||
private TeamRepository teamRepository;
|
||||
|
||||
// @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();
|
||||
/**
|
||||
* Contrôleur Team
|
||||
*/
|
||||
public TeamController(TeamRepository teamRepository) {
|
||||
this.teamRepository = teamRepository;
|
||||
}
|
||||
|
||||
// Récupération de toutes les teams
|
||||
|
||||
/**
|
||||
* Rechercher toutes les teams
|
||||
*
|
||||
* @return http://localhost:8088/teams/all
|
||||
*/
|
||||
@GetMapping(value = "/all")
|
||||
//@PreAuthorize("hasRole('ROLE_PARENT')")
|
||||
public ResponseEntity<?> getAllTeam() {
|
||||
List<Team> liste = null;
|
||||
try
|
||||
{
|
||||
liste = teamRepo.findAll();
|
||||
try {
|
||||
liste = teamRepository.findAll();
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||
}
|
||||
@ -46,13 +44,18 @@ public class TeamController {
|
||||
return ResponseEntity.status(HttpStatus.OK).body(liste);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rechercher une team par son Id
|
||||
*
|
||||
* @return http://localhost:8088/teams/1
|
||||
*/
|
||||
@GetMapping(value = "/{id}")
|
||||
//@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
|
||||
public ResponseEntity<?> findTeamById(@PathVariable int id) {
|
||||
Optional<Team> liste = null;
|
||||
try
|
||||
{
|
||||
liste = teamRepo.findById(id);
|
||||
try {
|
||||
liste = teamRepository.findById(id);
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||
}
|
||||
@ -60,12 +63,17 @@ public class TeamController {
|
||||
return ResponseEntity.status(HttpStatus.OK).body(liste);
|
||||
}
|
||||
|
||||
/**
|
||||
* Création d'une team
|
||||
*
|
||||
* @return http://localhost:8088/teams/add
|
||||
*/
|
||||
@PostMapping(value = "/add", produces = "application/json", consumes = "application/json")
|
||||
//@PreAuthorize("hasRole('ROLE_PARENT')")
|
||||
public ResponseEntity<?> addTeam(@RequestBody Team team) {
|
||||
Team resultTeam = null;
|
||||
try {
|
||||
resultTeam = teamRepo.saveAndFlush(team);
|
||||
resultTeam = teamRepository.saveAndFlush(team);
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
|
||||
}
|
||||
@ -73,12 +81,17 @@ public class TeamController {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(resultTeam);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifier une teamm par son Id
|
||||
*
|
||||
* @return http://localhost:8088/teams/update/1
|
||||
*/
|
||||
@PutMapping("/update/{id}")
|
||||
//@PreAuthorize("hasRole('ROLE_PARENT')")
|
||||
public ResponseEntity<?> updateTeam(@RequestBody Team team, @PathVariable Integer id) throws Exception {
|
||||
Team resultTeam = null;
|
||||
try {
|
||||
resultTeam = teamRepo.save(team);
|
||||
resultTeam = teamRepository.save(team);
|
||||
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
|
||||
@ -87,12 +100,17 @@ public class TeamController {
|
||||
return ResponseEntity.status(HttpStatus.OK).body(resultTeam);
|
||||
}
|
||||
|
||||
/**
|
||||
* Supprimer une team par son Id
|
||||
*
|
||||
* @return http://localhost:8088/teams/delete/1
|
||||
*/
|
||||
@DeleteMapping(value = "/delete/{id}")
|
||||
//@PreAuthorize("hasRole('ROLE_PARENT')")
|
||||
public ResponseEntity<?> deleteTeam(@PathVariable int id) {
|
||||
try {
|
||||
teamRepo.delete(teamRepo.getById(id));
|
||||
//membreRepo.deleteById(id);
|
||||
teamRepository.delete(teamRepository.getById(id));
|
||||
//membreRepository.deleteById(id);
|
||||
return ResponseEntity.status(HttpStatus.OK).body("Team effacée !");
|
||||
|
||||
} catch (EntityNotFoundException e) {
|
||||
|
Loading…
Reference in New Issue
Block a user