PersonneController.java 3.25 KB
package etunicorn;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by geoffrey on 28/01/17.
 */

@RestController
public class PersonneController implements etunicorn.generated.PersonneController {
    @Autowired
    private PersonneRepository personneRepository;

    @Override
    public ResponseEntity<?> getPersonne() {
        return new ResponseEntity<List>((List) this.personneRepository.findAll(), HttpStatus.OK);
    }

    @Override
    public ResponseEntity<?> updatePersonne(@RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false, defaultValue = "etudiant") String role) {
        Personne personne = new Personne();
        if (carte != null) {
            personne.setCarte(carte);
        }
        if (naissance != null) {
            personne.setNaissance(naissance);
        }
        if (login != null) {
            personne.setLogin(login);
        }
        this.personneRepository.save(personne);
        return new ResponseEntity<Object>(personne, HttpStatus.CREATED);
    }

    @Override
    public ResponseEntity<?> getPersonneById(@PathVariable BigDecimal idPersonne) {
        Personne personne = personneRepository.findById(idPersonne.intValueExact());
        if (personne == null) {
            return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Object>(personne, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<?> updatePersonneById(@PathVariable BigDecimal idPersonne, @RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false) String role) {
        Personne personne = personneRepository.findById(idPersonne.intValueExact());
        if (personne == null) {
            return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
        }
        if (carte != null) {
            personne.setCarte(carte);
        }
        if (naissance != null) {
            personne.setNaissance(naissance);
        }
        if (login != null) {
            personne.setLogin(login);
        }
        this.personneRepository.save(personne);
        return new ResponseEntity<Object>(personne, HttpStatus.CREATED);
    }

    @Override
    public ResponseEntity<?> deletePersonneById(@PathVariable BigDecimal idPersonne) {
        Personne personne = personneRepository.findById(idPersonne.intValueExact());
        if (personne == null) {
            return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
        }
        personneRepository.delete(personne);
        return new ResponseEntity<Object>(personne, HttpStatus.NO_CONTENT);
    }

}