From f6afef809d4a0f403fcdeace97598728e17601b1 Mon Sep 17 00:00:00 2001 From: Blandine Bajard <83599148+BlandineBajard@users.noreply.github.com> Date: Wed, 19 Jan 2022 10:22:28 +0100 Subject: [PATCH] contact controller et repo --- .idea/Organizee.iml | 9 -- .idea/compiler.xml | 2 + .../controller/ContactController.java | 87 +++++++++++++++++++ .../controller/MembreController.java | 18 ---- .../repository/ContactRepository.java | 17 +++- 5 files changed, 105 insertions(+), 28 deletions(-) delete mode 100644 .idea/Organizee.iml create mode 100644 src/main/java/fr/organizee/controller/ContactController.java diff --git a/.idea/Organizee.iml b/.idea/Organizee.iml deleted file mode 100644 index d6ebd48..0000000 --- a/.idea/Organizee.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index a8c4885..c75fd2a 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,11 +6,13 @@ + diff --git a/src/main/java/fr/organizee/controller/ContactController.java b/src/main/java/fr/organizee/controller/ContactController.java new file mode 100644 index 0000000..27d0d32 --- /dev/null +++ b/src/main/java/fr/organizee/controller/ContactController.java @@ -0,0 +1,87 @@ +package fr.organizee.controller; + +import fr.organizee.model.Contact; +import fr.organizee.model.Membre; +import fr.organizee.model.Team; +import fr.organizee.repository.ContactRepository; +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; + +@RestController +@CrossOrigin("*") +@RequestMapping("/contacts") +public class ContactController { + + @Autowired + private ContactRepository contactRepo; + + @GetMapping(value = "/{id}") + public ResponseEntity findById(@PathVariable int id){ + Optional contact = null; + try + { + contact = contactRepo.findById(id); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } + + return ResponseEntity.status(HttpStatus.OK).body(contact); + } + + @GetMapping(value = "team/{team_id}") + public ResponseEntity findByTeamId(@PathVariable int team_id){ + List contacts = null; + try + { + contacts = contactRepo.FindContactsByTeam(team_id); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } + + return ResponseEntity.status(HttpStatus.OK).body(contacts); + } + + @PostMapping(value="/add") + public ResponseEntity addContact(@RequestBody Contact contact){ + Contact resultContact = null; + try { + resultContact = contactRepo.saveAndFlush(contact); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + } + + return ResponseEntity.status(HttpStatus.CREATED).body(resultContact); + } + + @PutMapping("/update/{id}") + public ResponseEntity updateContact(@RequestBody Contact contact, @PathVariable Integer id) throws Exception { + Contact resultContact = null; + try { + resultContact = contactRepo.save(contact); + + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); + } + + return ResponseEntity.status(HttpStatus.OK).body(resultContact); + } + + @DeleteMapping(value = "/delete/{id}") + public ResponseEntity deleteContact(@PathVariable int id){ + try { + contactRepo.delete(contactRepo.getById(id)); + return ResponseEntity.status(HttpStatus.OK).body("Contact effacé !"); + + } catch (EntityNotFoundException e) { + + return ResponseEntity.status(HttpStatus.OK).body("Contact introuvable !"); + } + } +} diff --git a/src/main/java/fr/organizee/controller/MembreController.java b/src/main/java/fr/organizee/controller/MembreController.java index d200dc3..dc0edf7 100644 --- a/src/main/java/fr/organizee/controller/MembreController.java +++ b/src/main/java/fr/organizee/controller/MembreController.java @@ -148,22 +148,4 @@ public class MembreController { // // 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/repository/ContactRepository.java b/src/main/java/fr/organizee/repository/ContactRepository.java index b670634..ed1612a 100644 --- a/src/main/java/fr/organizee/repository/ContactRepository.java +++ b/src/main/java/fr/organizee/repository/ContactRepository.java @@ -1,4 +1,19 @@ package fr.organizee.repository; -public interface ContactRepository { +import fr.organizee.model.Contact; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface ContactRepository extends JpaRepository { + +@Query(value = "select * from contact where team_id = :team_id", nativeQuery = true) + List FindContactsByTeam(@Param("team_id") int team_id); + + }