diff --git a/src/main/java/etunicorn/BaseController.java b/src/main/java/etunicorn/BaseController.java index b222de9..e650755 100644 --- a/src/main/java/etunicorn/BaseController.java +++ b/src/main/java/etunicorn/BaseController.java @@ -1,9 +1,16 @@ package etunicorn; +import com.fasterxml.jackson.annotation.JsonProperty; +import net.minidev.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; /** * etunicorn-server @@ -33,4 +40,69 @@ public class BaseController { Permission permission = permissionRepository.findByNom(nomPermission); return hasPermission(permission); } + + // Utilités pour générer des erreurs + ResponseEntity generateError(HttpStatus status, String message) { + JSONObject json = new JSONObject(); + json.put("status", status.value()); + json.put("message", message); + return new ResponseEntity(json, status); + } + + ResponseEntity generateError(HttpStatus status) { + return generateError(status, String.format("Erreur de type %d", status.value())); + } + + ResponseEntity generateError(HttpStatus status, Exception exception, String message) { + JSONObject json = new JSONObject(); + json.put("status", status.value()); + json.put("message", message); + json.put("errorMessage", exception.getLocalizedMessage()); + json.put("stacktrace", exception.getStackTrace().toString()); + return new ResponseEntity(json, status); + } + + ResponseEntity generateError(Exception exception) { + return generateError(HttpStatus.INTERNAL_SERVER_ERROR, exception, exception.getLocalizedMessage()); + } + + // Utilités pour merger un request dans une entity + void mergeRequestInEntity(Object request, Object entity) throws EntityRequestMismatchException { + for (Method getMethode : request.getClass().getMethods()) { + String getMethodName = getMethode.getName(); + JsonProperty annotation = getMethode.getAnnotation(JsonProperty.class); + if (getMethodName.startsWith("get") && annotation != null) { + String setMethodName = "s" + getMethodName.substring(1); + Method setMethode; + Class fieldClass; + try { + fieldClass = entity.getClass().getMethod(getMethodName).getReturnType(); + setMethode = entity.getClass().getMethod(setMethodName, fieldClass); + } catch (NoSuchMethodException e) { + throw new EntityRequestMismatchException(); + } + try { + if (getMethode.invoke(request) != null) { + Object data = getMethode.invoke(request); + if (data.getClass() != fieldClass) { + Constructor constructor = fieldClass.getConstructor(data.getClass()); + if (constructor != null) { + data = constructor.newInstance(data); + } + } + setMethode.invoke(entity, data); + } + } catch (IllegalAccessException e) { + throw new EntityRequestMismatchException(); + } catch (InvocationTargetException e) { + throw new EntityRequestMismatchException(); + } catch (NoSuchMethodException e) { + throw new EntityRequestMismatchException(); + } catch (InstantiationException e) { + throw new EntityRequestMismatchException(); + } + } + + } + } } diff --git a/src/main/java/etunicorn/EntityRequestMismatchException.java b/src/main/java/etunicorn/EntityRequestMismatchException.java new file mode 100644 index 0000000..4197a3c --- /dev/null +++ b/src/main/java/etunicorn/EntityRequestMismatchException.java @@ -0,0 +1,9 @@ +package etunicorn; + +/** + * etunicorn-server + * Copyright © 2017 Le Club Info Polytech Lille + * Tous droits réservés + */ +public class EntityRequestMismatchException extends Exception { +} diff --git a/src/main/java/etunicorn/LoginController.java b/src/main/java/etunicorn/LoginController.java index bbeccb9..081c976 100644 --- a/src/main/java/etunicorn/LoginController.java +++ b/src/main/java/etunicorn/LoginController.java @@ -1,13 +1,16 @@ package etunicorn; -import net.minidev.json.JSONObject; +import etunicorn.generated.model.UpdateLoginRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import javax.validation.Valid; + /** * etunicorn-server * Copyright © 2017 Le Club Info Polytech Lille @@ -23,6 +26,9 @@ public class LoginController implements etunicorn.generated.LoginController { @Override @RestrictedTo(authentifie = false) + public ResponseEntity updateLogin(@Valid @RequestBody UpdateLoginRequest updateLoginRequest) { + return null; + } public ResponseEntity updateLogin(@RequestParam String login, @RequestParam String password) { Personne personne = personneRepository.findByLogin(login); if (personne == null) { @@ -39,6 +45,7 @@ public class LoginController implements etunicorn.generated.LoginController { return new ResponseEntity(session, HttpStatus.OK); } + @Override public ResponseEntity deleteLoginByToken(@PathVariable String token) { return null; diff --git a/src/main/java/etunicorn/Personne.java b/src/main/java/etunicorn/Personne.java index f65e802..a95bcc8 100644 --- a/src/main/java/etunicorn/Personne.java +++ b/src/main/java/etunicorn/Personne.java @@ -12,21 +12,17 @@ import java.util.Date; public class Personne { + @ManyToOne + public Role role; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; - @Column(unique = true) private String carte; - private Date naissance; - @Column(unique = true) private String login; - @ManyToOne - private Role role; - public Personne() { } diff --git a/src/main/java/etunicorn/PersonneController.java b/src/main/java/etunicorn/PersonneController.java index 2958931..f4c9228 100644 --- a/src/main/java/etunicorn/PersonneController.java +++ b/src/main/java/etunicorn/PersonneController.java @@ -1,13 +1,17 @@ package etunicorn; +import etunicorn.generated.model.UpdatePersonneByIdRequest; +import etunicorn.generated.model.UpdatePersonneRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import javax.validation.Valid; import java.math.BigDecimal; import java.util.Date; import java.util.List; @@ -32,7 +36,6 @@ public class PersonneController extends BaseController implements etunicorn.gene return new ResponseEntity((List) this.personneRepository.findAll(), HttpStatus.OK); } - private ResponseEntity mergePersonne(Personne personne, String carte, Date naissance, String login, String role) { if (carte != null) { personne.setCarte(carte); @@ -65,6 +68,15 @@ public class PersonneController extends BaseController implements etunicorn.gene @Override @RestrictedTo("PERSONNE_ADD") + public ResponseEntity updatePersonne(@Valid @RequestBody UpdatePersonneRequest updatePersonneRequest) { + Personne personne = new Personne(); + try { + mergeRequestInEntity(updatePersonneRequest, personne); + } catch (EntityRequestMismatchException e) { + return generateError(e); + } + return new ResponseEntity(personne, HttpStatus.CREATED); + } public ResponseEntity updatePersonne(@RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false) String role) { Personne personne = new Personne(); return mergePersonne(personne, carte, naissance, login, role); @@ -82,6 +94,10 @@ public class PersonneController extends BaseController implements etunicorn.gene @Override @RestrictedTo("PERSONNE_EDIT") + public ResponseEntity updatePersonneById(@PathVariable BigDecimal idPersonne, @Valid @RequestBody UpdatePersonneByIdRequest updatePersonneByIdRequest) { + return null; + } + public ResponseEntity updatePersonneById(@PathVariable BigDecimal idPersonne, @RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false) String role) { Personne personne = personneRepository.findById(idPersonne.intValueExact()); if (personne == null) { diff --git a/src/main/java/etunicorn/RoleController.java b/src/main/java/etunicorn/RoleController.java index ad167bd..a9ea4a5 100644 --- a/src/main/java/etunicorn/RoleController.java +++ b/src/main/java/etunicorn/RoleController.java @@ -1,14 +1,19 @@ package etunicorn; +import etunicorn.generated.model.UpdateRoleByIdRequest; +import etunicorn.generated.model.UpdateRoleRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import javax.validation.Valid; +import java.math.BigDecimal; import java.util.List; /** @@ -31,6 +36,9 @@ public class RoleController extends BaseController implements etunicorn.generate @Override @RestrictedTo("ROLE_ADD") + public ResponseEntity updateRole(@Valid @RequestBody UpdateRoleRequest updateRoleRequest) { + return null; + } public ResponseEntity updateRole(@RequestParam String nom) { Role oldRole = roleRepository.findByNom(nom); if (oldRole != null) { @@ -48,6 +56,9 @@ public class RoleController extends BaseController implements etunicorn.generate @Override @RestrictedTo("ROLE_DELETE") + public ResponseEntity deleteRoleById(@PathVariable BigDecimal nomRole) { + return null; + } public ResponseEntity deleteRoleById(@PathVariable String nomRole) { Role role = roleRepository.findByNom(nomRole); if (role == null) { @@ -59,6 +70,9 @@ public class RoleController extends BaseController implements etunicorn.generate @Override @RestrictedTo("ROLE_PERMISSION_ADD") + public ResponseEntity updateRoleById(@PathVariable BigDecimal nomRole, @Valid @RequestBody UpdateRoleByIdRequest updateRoleByIdRequest) { + return null; + } public ResponseEntity updateRoleById(@PathVariable String nomRole, @RequestParam String nom) { Role role = roleRepository.findByNom(nomRole); if (role == null) { @@ -79,6 +93,9 @@ public class RoleController extends BaseController implements etunicorn.generate @Override @RestrictedTo("ROLE_PERMISSION_REMOVE") + public ResponseEntity deleteRoleByNomPermission(@PathVariable String nomPermission, @PathVariable BigDecimal nomRole) { + return null; + } public ResponseEntity deleteRoleByNomPermission(@PathVariable String nomPermission, @PathVariable String nomRole) { Role role = roleRepository.findByNom(nomRole); if (role == null) { -- libgit2 0.21.2