Blame view

src/main/java/etunicorn/controller/EvenementController.java 4.43 KB
4fd7a7fc   Geoffrey PREUD'HOMME   /evenement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  package etunicorn.controller;
  
  import etunicorn.RestrictedTo;
  import etunicorn.entity.Evenement;
  import etunicorn.generated.model.UpdateParticipeRequest;
  import etunicorn.generated.model.UpdateReserveRequest;
  import etunicorn.generated.model.UpdateEvenementByIdRequest;
  import etunicorn.generated.model.UpdateEvenementRequest;
  import etunicorn.repository.EvenementRepository;
  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.RestController;
  
  import javax.validation.Valid;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  
  /**
   * etunicorn-server
   * Copyright © 2017 Le Club Info Polytech Lille
   * Tous droits réservés
   */
  @RestController
  public class EvenementController extends BaseController implements etunicorn.generated.EvenementController {
      @Autowired
      EvenementRepository evenementRepository;
  
      @Override
      @RestrictedTo("EVNMT_LIST")
      public ResponseEntity<?> getEvenement() {
          return new ResponseEntity<Object>(this.evenementRepository.findAll(), HttpStatus.OK);
      }
  
      private ResponseEntity<?> mergeEvenement(Evenement evenement, UpdateEvenementRequest updateEvenementRequest) {
          if (updateEvenementRequest.getNom() != null) {
              evenement.setNom(updateEvenementRequest.getNom());
          }
          if (updateEvenementRequest.getPrix() != null) {
              evenement.setPrix(updateEvenementRequest.getPrix());
          }
          if (updateEvenementRequest.getDate() != null) {
              SimpleDateFormat format = new SimpleDateFormat("Y-M-d H:m:s");
              try {
                  evenement.setDate(format.parse(updateEvenementRequest.getDate()));
              } catch (ParseException e) {
                  return generateError(HttpStatus.BAD_REQUEST, e, "La date n'est pas au format " + format.toString());
              }
          }
          evenementRepository.save(evenement);
          return new ResponseEntity(evenement, HttpStatus.CREATED);
      }
  
      @Override
      @RestrictedTo("EVNMT_ADD")
      public ResponseEntity<?> updateEvenement(@Valid @RequestBody UpdateEvenementRequest updateEvenementRequest) {
          Evenement evenement = new Evenement();
          return mergeEvenement(evenement, updateEvenementRequest);
      }
  
      @Override
      @RestrictedTo("EVNMT_EDIT")
      public ResponseEntity<?> updateEvenementById(@PathVariable Long idEvenement, @Valid @RequestBody UpdateEvenementByIdRequest updateEvenementByIdRequest) {
          Evenement evenement = evenementRepository.findById(idEvenement.intValue());
          if (evenement == null) {
              return generateError(HttpStatus.NOT_FOUND, "Évènement inconnu");
          }
          UpdateEvenementRequest updateEvenementRequest = new UpdateEvenementRequest();
          updateEvenementRequest.setNom(updateEvenementByIdRequest.getNom());
          updateEvenementRequest.setPrix(updateEvenementByIdRequest.getPrix());
          updateEvenementRequest.setDate(updateEvenementByIdRequest.getDate());
          return mergeEvenement(evenement, updateEvenementRequest);
      }
  
      @Override
      @RestrictedTo("EVNMT_GET")
      public ResponseEntity<?> getEvenementById(@PathVariable Long idEvenement) {
          Evenement evenement = evenementRepository.findById(idEvenement.intValue());
          if (evenement == null) {
              return generateError(HttpStatus.NOT_FOUND, "Évènement inconnu");
          }
          return new ResponseEntity(evenement, HttpStatus.OK);
      }
  
      @Override
      @RestrictedTo("EVNMT_REMOVE")
      public ResponseEntity<?> deleteEvenementById(@PathVariable Long idEvenement) {
          Evenement evenement = evenementRepository.findById(idEvenement.intValue());
          if (evenement == null) {
              return generateError(HttpStatus.NOT_FOUND, "Évènement inconnu");
          }
          evenementRepository.delete(evenement);
          return null;
      }
  
      @Override
      @RestrictedTo("EVNMT_PARTICIPE")
      public ResponseEntity<?> updateParticipe(@PathVariable Long idEvenement, @Valid @RequestBody UpdateParticipeRequest updateParticipeRequest) {
          return null;
      }
  
      @Override
      @RestrictedTo("EVNMT_RESERVE")
      public ResponseEntity<?> updateReserve(@PathVariable Long idEvenement, @Valid @RequestBody UpdateReserveRequest updateReserveRequest) {
          return null;
      }
  }