Commit 4ba553cc authored by Alexandre Ducarne's avatar Alexandre Ducarne

Respect REST naming convention

parent 86088e8a
......@@ -18,8 +18,7 @@ public class CompteController {
private CompteRepository compteRepository;
@PutMapping(path="/")
public
Compte addCompte (@RequestBody CompteRequest compteRequest) {
public Compte create(@RequestBody CompteRequest compteRequest) {
Compte compte = Compte.builder()
.email(compteRequest.getEmail())
.mdp(compteRequest.getMdp())
......@@ -33,19 +32,21 @@ public class CompteController {
@GetMapping(path = "/{id}")
public @ResponseBody
Compte getCompteById(@PathVariable Integer id) {
ResponseEntity<Compte> getById(@PathVariable Integer id) {
return compteRepository
.findById(id)
.orElseGet(Compte::new);
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@GetMapping(path="/")
public @ResponseBody Iterable<Compte> getAllComptes() {
public @ResponseBody
Iterable<Compte> getAll() {
return compteRepository.findAll();
}
@DeleteMapping(path="/{id}")
public ResponseEntity<?> deleteCompte(@PathVariable int id){
public ResponseEntity<Integer> delete(@PathVariable int id) {
try {
compteRepository.deleteById(id);
}
......
......@@ -3,6 +3,8 @@ package com.RESTAPI.RESTAPI.Controllers;
import com.RESTAPI.RESTAPI.Entities.Consultation;
import com.RESTAPI.RESTAPI.Repositories.ConsultationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
......@@ -25,4 +27,12 @@ public class ConsultationController {
{
return consultationRepository.findAll();
}
@GetMapping(path = "/{id}")
public @ResponseBody
ResponseEntity<Consultation> getById(@PathVariable Integer id) {
return consultationRepository.findById(id)
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
}
\ No newline at end of file
......@@ -3,6 +3,8 @@ package com.RESTAPI.RESTAPI.Controllers;
import com.RESTAPI.RESTAPI.Entities.Proposition;
import com.RESTAPI.RESTAPI.Repositories.PropositionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
......@@ -14,9 +16,9 @@ public class PropositionController {
@PutMapping(path = "/")
public @ResponseBody
Proposition create(@RequestBody Proposition proposition)
ResponseEntity<Proposition> create(@RequestBody Proposition proposition)
{
return proposition.getSujet() != null ? propositionRepository.save(proposition) : new Proposition();
return proposition.getSujet() != null ? new ResponseEntity<>(propositionRepository.save(proposition), HttpStatus.OK) : new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}
@GetMapping(path = "/")
......@@ -28,8 +30,10 @@ public class PropositionController {
@GetMapping(path = "/{id}")
public @ResponseBody
Proposition getById(@PathVariable int id)
ResponseEntity<Proposition> getById(@PathVariable int id)
{
return propositionRepository.findById(id).get();
return propositionRepository.findById(id)
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
}
\ No newline at end of file
......@@ -3,6 +3,8 @@ package com.RESTAPI.RESTAPI.Controllers;
import com.RESTAPI.RESTAPI.Entities.Sujet;
import com.RESTAPI.RESTAPI.Repositories.SujetRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
......@@ -14,20 +16,21 @@ public class SujetController {
@PutMapping(path = "/")
public @ResponseBody
Sujet createSujet(@RequestBody Sujet sujet) {
sujetRepository.save(sujet);
return sujet;
Sujet create(@RequestBody Sujet sujet) {
return sujetRepository.save(sujet);
}
@GetMapping(path = "/{id}")
public @ResponseBody
Sujet getSujetById(@PathVariable int id) {
return sujetRepository.findById(id).get();
ResponseEntity<Sujet> getById(@PathVariable int id) {
return sujetRepository.findById(id)
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@GetMapping(path = "/")
public @ResponseBody
Iterable<Sujet> getAllSujets() {
Iterable<Sujet> getAll() {
return sujetRepository.findAll();
}
}
\ No newline at end of file
......@@ -3,7 +3,6 @@ package com.RESTAPI.RESTAPI.Controllers;
import com.RESTAPI.RESTAPI.Entities.User;
import com.RESTAPI.RESTAPI.Repositories.CompteRepository;
import com.RESTAPI.RESTAPI.Repositories.UserRepository;
import javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......@@ -20,28 +19,28 @@ public class UserController {
@PutMapping(path="/")
public @ResponseBody
User addNewUser (@RequestBody User user) {
User add(@RequestBody User user) {
compteRepository.save(user.getCompte());
userRepository.save(user);
return user;
return userRepository.save(user);
}
@GetMapping(path = "/{id}")
public @ResponseBody
ResponseEntity<User> getUserById(@PathVariable Integer id) throws NotFoundException {
ResponseEntity<User> getById(@PathVariable Integer id) {
return userRepository.findById(id)
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@GetMapping(path="/")
public @ResponseBody Iterable<User> getAllUsers() {
public @ResponseBody
Iterable<User> getAll() {
return userRepository.findAll();
}
@DeleteMapping(path="/{id}")
public ResponseEntity<?> deleteUser(@PathVariable int id){
public ResponseEntity<?> delete(@PathVariable int id) {
try {
userRepository.deleteById(id);
}
......
package com.RESTAPI.RESTAPI.Controllers;
import com.RESTAPI.RESTAPI.Entities.Proposition;
import com.RESTAPI.RESTAPI.Entities.Vote;
import com.RESTAPI.RESTAPI.Enums.ChoixVote;
import com.RESTAPI.RESTAPI.Repositories.ConsultationRepository;
import com.RESTAPI.RESTAPI.Repositories.PropositionRepository;
import com.RESTAPI.RESTAPI.Repositories.VoteRepository;
import javassist.tools.web.BadHttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping(path="/vote")
@RequestMapping(path = "/vote")
public class VoteController {
@Autowired
private PropositionRepository propositionRepository;
@Autowired
private ConsultationRepository consultationRepository;
@Autowired
private VoteRepository voteRepository;
@PutMapping(path="/")
public Vote vote(@RequestBody Vote vote) {
handleChoix(vote.getChoix(), vote.getProposition().getId());
return voteRepository.save(vote);
@PutMapping(path = "/")
public ResponseEntity<Vote> create(@RequestBody Vote vote) {
try {
handleChoice(vote.getChoix(), vote.getProposition().getId());
} catch (BadHttpRequest e) {
return new ResponseEntity<>(new Vote(), HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(voteRepository.save(vote), HttpStatus.OK);
}
private void handleChoix(ChoixVote choix, int id) {
Proposition p = propositionRepository.findById(id).get();
switch (choix){
case POUR:
p.setScorePour(p.getScorePour() + 1);
propositionRepository.save(p);
break;
case MAISPOUR:
p.setScoreMais(p.getScoreMais() + 1);
propositionRepository.save(p);
break;
case NEUTRE:
p.setScoreNeutre(p.getScoreNeutre() + 1);
propositionRepository.save(p);
break;
case NON:
p.setScoreNon(p.getScoreNon() + 1);
propositionRepository.save(p);
break;
}
@GetMapping(path = "/{id}")
public @ResponseBody
ResponseEntity<Vote> getById(@PathVariable Integer id) {
return voteRepository.findById(id)
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@GetMapping(path = "/")
public @ResponseBody
Iterable<Vote> getAll() {
return voteRepository.findAll();
}
private void handleChoice(ChoixVote choice, int id) throws BadHttpRequest {
propositionRepository.findById(id)
.map(p -> {
switch (choice) {
case POUR:
p.setScorePour(p.getScorePour() + 1);
break;
case MAISPOUR:
p.setScoreMais(p.getScoreMais() + 1);
break;
case NEUTRE:
p.setScoreNeutre(p.getScoreNeutre() + 1);
break;
case NON:
p.setScoreNon(p.getScoreNon() + 1);
break;
}
return propositionRepository.save(p);
})
.orElseThrow(BadHttpRequest::new);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment