EvenementController.java 4.43 KB
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;
    }
}