ConsommationController.java 4.69 KB
package etunicorn.controller;

import etunicorn.RestrictedTo;
import etunicorn.entity.Consommation;
import etunicorn.entity.Personne;
import etunicorn.generated.model.UpdateAcheteRequest;
import etunicorn.generated.model.UpdateConsommationByIdRequest;
import etunicorn.generated.model.UpdateConsommationRequest;
import etunicorn.repository.ConsommationRepository;
import etunicorn.service.PersonneService;
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.util.LinkedHashMap;

/**
 * etunicorn-server
 * Copyright © 2017 Le Club Info Polytech Lille
 * Tous droits réservés
 */
@RestController
public class ConsommationController extends BaseController implements etunicorn.generated.ConsommationController {
    @Autowired
    ConsommationRepository consommationRepository;

    @Autowired
    PersonneService personneService;

    @Override
    @RestrictedTo("CONSO_LIST")
    public ResponseEntity<?> getConsommation() {
        return new ResponseEntity<Object>(this.consommationRepository.findAll(), HttpStatus.OK);
    }

    private ResponseEntity<?> mergeConsommation(Consommation consommation, UpdateConsommationRequest updateConsommationRequest) {
        if (updateConsommationRequest.getNom() != null) {
            consommation.setNom(updateConsommationRequest.getNom());
        }
        if (updateConsommationRequest.getPrix() != null) {
            consommation.setPrix(updateConsommationRequest.getPrix());
        }
        consommationRepository.save(consommation);
        return new ResponseEntity(consommation, HttpStatus.CREATED);
    }

    @Override
    @RestrictedTo("CONSO_ADD")
    public ResponseEntity<?> updateConsommation(@Valid @RequestBody UpdateConsommationRequest updateConsommationRequest) {
        Consommation consommation = new Consommation();
        return mergeConsommation(consommation, updateConsommationRequest);
    }

    @Override
    @RestrictedTo("CONSO_EDIT")
    public ResponseEntity<?> updateConsommationById(@PathVariable Long idConsommation, @Valid @RequestBody UpdateConsommationByIdRequest updateConsommationByIdRequest) {
        Consommation consommation = consommationRepository.findById(idConsommation.intValue());
        if (consommation == null) {
            return generateError(HttpStatus.NOT_FOUND, "Consommation inconnue");
        }
        UpdateConsommationRequest updateConsommationRequest = new UpdateConsommationRequest();
        updateConsommationRequest.setNom(updateConsommationByIdRequest.getNom());
        updateConsommationRequest.setPrix(updateConsommationByIdRequest.getPrix());
        return mergeConsommation(consommation, updateConsommationRequest);
    }

    @Override
    @RestrictedTo("CONSO_GET")
    public ResponseEntity<?> getConsommationById(@PathVariable Long idConsommation) {
        Consommation consommation = consommationRepository.findById(idConsommation.intValue());
        if (consommation == null) {
            return generateError(HttpStatus.NOT_FOUND, "Consommation inconnue");
        }
        return new ResponseEntity(consommation, HttpStatus.OK);
    }

    @Override
    @RestrictedTo("CONSO_REMOVE")
    public ResponseEntity<?> deleteConsommationById(@PathVariable Long idConsommation) {
        Consommation consommation = consommationRepository.findById(idConsommation.intValue());
        if (consommation == null) {
            return generateError(HttpStatus.NOT_FOUND, "Consommation inconnue");
        }
        consommationRepository.delete(consommation);
        return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
    }

    @Override
    @RestrictedTo("CONSO_ACHAT")
    public ResponseEntity<?> updateAchete(@PathVariable Long idConsommation, @Valid @RequestBody UpdateAcheteRequest updateAcheteRequest) {
        Consommation consommation = consommationRepository.findById(idConsommation.intValue());
        if (consommation == null) {
            return generateError(HttpStatus.NOT_FOUND, "Consommation inconnue");
        }
        if (!(updateAcheteRequest.getParticipant() instanceof LinkedHashMap)) {
            return generateError(HttpStatus.BAD_REQUEST, "Participant doit être un objet");
        }
        Personne personne = personneService.getFromObject((LinkedHashMap<String, Object>) updateAcheteRequest.getParticipant());
        if (personne == null) {
            return generateError(HttpStatus.NOT_FOUND, "Personne inconnue");
        }
        // TODO To be continued...
        return null;
    }
}