Commit 2cc8026e7673405eb59537c4daf8fa56780d6255

Authored by Geoffrey PREUD'HOMME
1 parent ce758498

WIP Achat

src/main/java/etunicorn/Application.java
... ... @@ -51,11 +51,14 @@ public class Application {
51 51 permissionRepository.save(new Permission("CONSO_GET"));
52 52 permissionRepository.save(new Permission("CONSO_LIST"));
53 53 permissionRepository.save(new Permission("CONSO_REMOVE"));
  54 + permissionRepository.save(new Permission("CONSO_ACHAT"));
54 55 permissionRepository.save(new Permission("EVNMT_ADD"));
55 56 permissionRepository.save(new Permission("EVNMT_EDIT"));
56 57 permissionRepository.save(new Permission("EVNMT_GET"));
57 58 permissionRepository.save(new Permission("EVNMT_LIST"));
58 59 permissionRepository.save(new Permission("EVNMT_REMOVE"));
  60 + permissionRepository.save(new Permission("EVNMT_RESERVE"));
  61 + permissionRepository.save(new Permission("EVNMT_PARTICIPE"));
59 62 // ...
60 63  
61 64 //roleRepository.save(new Role("admin", (List<Permission>) permissionRepository.findAll()));
... ...
src/main/java/etunicorn/controller/ConsommationController.java
... ... @@ -2,10 +2,12 @@ package etunicorn.controller;
2 2  
3 3 import etunicorn.RestrictedTo;
4 4 import etunicorn.entity.Consommation;
  5 +import etunicorn.entity.Personne;
5 6 import etunicorn.generated.model.UpdateAcheteRequest;
6 7 import etunicorn.generated.model.UpdateConsommationByIdRequest;
7 8 import etunicorn.generated.model.UpdateConsommationRequest;
8 9 import etunicorn.repository.ConsommationRepository;
  10 +import etunicorn.service.PersonneService;
9 11 import org.springframework.beans.factory.annotation.Autowired;
10 12 import org.springframework.http.HttpStatus;
11 13 import org.springframework.http.ResponseEntity;
... ... @@ -14,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestBody;
14 16 import org.springframework.web.bind.annotation.RestController;
15 17  
16 18 import javax.validation.Valid;
  19 +import java.util.LinkedHashMap;
17 20  
18 21 /**
19 22 * etunicorn-server
... ... @@ -25,6 +28,9 @@ public class ConsommationController extends BaseController implements etunicorn.
25 28 @Autowired
26 29 ConsommationRepository consommationRepository;
27 30  
  31 + @Autowired
  32 + PersonneService personneService;
  33 +
28 34 @Override
29 35 @RestrictedTo("CONSO_LIST")
30 36 public ResponseEntity<?> getConsommation() {
... ... @@ -86,6 +92,18 @@ public class ConsommationController extends BaseController implements etunicorn.
86 92 @Override
87 93 @RestrictedTo("CONSO_ACHAT")
88 94 public ResponseEntity<?> updateAchete(@PathVariable Long idConsommation, @Valid @RequestBody UpdateAcheteRequest updateAcheteRequest) {
  95 + Consommation consommation = consommationRepository.findById(idConsommation.intValue());
  96 + if (consommation == null) {
  97 + return generateError(HttpStatus.NOT_FOUND, "Consommation inconnue");
  98 + }
  99 + if (!(updateAcheteRequest.getParticipant() instanceof LinkedHashMap)) {
  100 + return generateError(HttpStatus.BAD_REQUEST, "Participant doit être un objet");
  101 + }
  102 + Personne personne = personneService.getFromObject((LinkedHashMap<String, Object>) updateAcheteRequest.getParticipant());
  103 + if (personne == null) {
  104 + return generateError(HttpStatus.NOT_FOUND, "Personne inconnue");
  105 + }
  106 + // TODO To be continued...
89 107 return null;
90 108 }
91 109 }
... ...
src/main/java/etunicorn/controller/PersonneController.java
... ... @@ -72,7 +72,7 @@ public class PersonneController extends BaseController implements etunicorn.gene
72 72 }
73 73 }
74 74 } else {
75   - return generateError(HttpStatus.BAD_REQUEST, "Le rôle est un objet");
  75 + return generateError(HttpStatus.BAD_REQUEST, "Rôle doit être un objet");
76 76 }
77 77 }
78 78  
... ...
src/main/java/etunicorn/service/PersonneService.java 0 → 100644
... ... @@ -0,0 +1,34 @@
  1 +package etunicorn.service;
  2 +
  3 +import etunicorn.entity.Personne;
  4 +import etunicorn.repository.PersonneRepository;
  5 +import org.springframework.beans.factory.annotation.Autowired;
  6 +import org.springframework.stereotype.Service;
  7 +
  8 +import java.util.LinkedHashMap;
  9 +
  10 +/**
  11 + * etunicorn-server
  12 + * Copyright © 2017 Le Club Info Polytech Lille
  13 + * Tous droits réservés
  14 + */
  15 +@Service("personneService")
  16 +public class PersonneService {
  17 + @Autowired
  18 + PersonneRepository personneRepository;
  19 +
  20 + public PersonneService() {
  21 + }
  22 +
  23 + public Personne getFromObject(LinkedHashMap<String, Object> object) {
  24 + String carte = (String) object.get("carte");
  25 + if (carte != null) {
  26 + return personneRepository.findByCarte(carte);
  27 + }
  28 + Integer id = (Integer) object.get("id");
  29 + if (id != null) {
  30 + return personneRepository.findById(id);
  31 + }
  32 + return null;
  33 + }
  34 +}
... ...