ConsommationController.java 3.96 KB
package etunicorn.controller;

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

/**
 * 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;

    @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 consommationId, @PathVariable String idConsommation, @Valid @RequestBody UpdateConsommationByIdRequest updateConsommationByIdRequest) {
        Consommation consommation = consommationRepository.findById(consommationId.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 consommationId, @PathVariable String idConsommation) {
        Consommation consommation = consommationRepository.findById(consommationId.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 consommationId, @PathVariable String idConsommation) {
        Consommation consommation = consommationRepository.findById(consommationId.intValue());
        if (consommation == null) {
            return generateError(HttpStatus.NOT_FOUND, "Consommation inconnue");
        }
        consommationRepository.delete(consommation);
        return null;
    }

    @Override
    @RestrictedTo("CONSO_ACHAT")
    public ResponseEntity<?> updateAchete(@PathVariable Long consommationId, @PathVariable String idConsommation, @Valid @RequestBody UpdateAcheteRequest updateAcheteRequest) {
        return null;
    }
}