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

Respect REST naming convention

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