Merge pull request #8 from HediMjid/Blandine

contact controller et repo
This commit is contained in:
Blandine Bajard 2022-01-19 10:25:00 +01:00 committed by GitHub
commit 8d30dae023
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 28 deletions

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -6,11 +6,13 @@
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="Organizee" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="Organizee" options="-parameters" />
<module name="organizee" options="-parameters" />
</option>
</component>

View File

@ -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> 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<Contact> 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 !");
}
}
}

View File

@ -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);
}
}

View File

@ -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<Contact, Integer> {
@Query(value = "select * from contact where team_id = :team_id", nativeQuery = true)
List<Contact> FindContactsByTeam(@Param("team_id") int team_id);
}