From 4fd7a7fc4595c3d4eb7507f320660d9a4f209ce5 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Mon, 6 Feb 2017 00:11:22 +0100 Subject: [PATCH] /evenement --- src/main/java/etunicorn/Application.java | 5 +++++ src/main/java/etunicorn/controller/EvenementController.java | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/etunicorn/controller/PersonneController.java | 2 +- src/main/java/etunicorn/entity/Evenement.java | 4 ++-- 4 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 src/main/java/etunicorn/controller/EvenementController.java diff --git a/src/main/java/etunicorn/Application.java b/src/main/java/etunicorn/Application.java index 7609212..9edcbf5 100644 --- a/src/main/java/etunicorn/Application.java +++ b/src/main/java/etunicorn/Application.java @@ -51,6 +51,11 @@ public class Application { permissionRepository.save(new Permission("CONSO_GET")); permissionRepository.save(new Permission("CONSO_LIST")); permissionRepository.save(new Permission("CONSO_REMOVE")); + permissionRepository.save(new Permission("EVNMT_ADD")); + permissionRepository.save(new Permission("EVNMT_EDIT")); + permissionRepository.save(new Permission("EVNMT_GET")); + permissionRepository.save(new Permission("EVNMT_LIST")); + permissionRepository.save(new Permission("EVNMT_REMOVE")); // ... //roleRepository.save(new Role("admin", (List) permissionRepository.findAll())); diff --git a/src/main/java/etunicorn/controller/EvenementController.java b/src/main/java/etunicorn/controller/EvenementController.java new file mode 100644 index 0000000..2919d0c --- /dev/null +++ b/src/main/java/etunicorn/controller/EvenementController.java @@ -0,0 +1,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(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; + } +} diff --git a/src/main/java/etunicorn/controller/PersonneController.java b/src/main/java/etunicorn/controller/PersonneController.java index bf96139..8444738 100644 --- a/src/main/java/etunicorn/controller/PersonneController.java +++ b/src/main/java/etunicorn/controller/PersonneController.java @@ -44,7 +44,7 @@ public class PersonneController extends BaseController implements etunicorn.gene private ResponseEntity mergePersonne(Personne personne, UpdatePersonneRequest updatePersonneRequest) { if (updatePersonneRequest.getNaissance() != null) { - SimpleDateFormat format = new SimpleDateFormat("y-M-d"); + SimpleDateFormat format = new SimpleDateFormat("Y-M-d"); try { personne.setNaissance(format.parse(updatePersonneRequest.getNaissance())); } catch (ParseException e) { diff --git a/src/main/java/etunicorn/entity/Evenement.java b/src/main/java/etunicorn/entity/Evenement.java index 5189cd9..22315f3 100644 --- a/src/main/java/etunicorn/entity/Evenement.java +++ b/src/main/java/etunicorn/entity/Evenement.java @@ -28,8 +28,8 @@ public class Evenement { @ManyToMany(mappedBy = "participations") private List participants; - public Evenement(String nom) { - this.nom = nom; + public Evenement() { + } public int getId() { -- libgit2 0.21.2