Commit 86088e8a authored by Alexandre Ducarne's avatar Alexandre Ducarne

Error handling

parent f07049b1
......@@ -33,8 +33,10 @@ public class CompteController {
@GetMapping(path = "/{id}")
public @ResponseBody
Compte getCompteById(@RequestBody Integer id) {
return compteRepository.findById(id).get();
Compte getCompteById(@PathVariable Integer id) {
return compteRepository
.findById(id)
.orElseGet(Compte::new);
}
@GetMapping(path="/")
......
......@@ -3,6 +3,7 @@ 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;
......@@ -28,8 +29,10 @@ public class UserController {
@GetMapping(path = "/{id}")
public @ResponseBody
User getUserById(@RequestBody Integer id) {
return userRepository.findById(id).get();
ResponseEntity<User> getUserById(@PathVariable Integer id) throws NotFoundException {
return userRepository.findById(id)
.map(result -> new ResponseEntity<>(result, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@GetMapping(path="/")
......@@ -47,4 +50,9 @@ public class UserController {
}
return new ResponseEntity<>(id, HttpStatus.OK);
}
@PatchMapping(path = "/")
public ResponseEntity<User> update(@RequestBody User user) {
return new ResponseEntity<>(userRepository.save(user), HttpStatus.OK);
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ package com.RESTAPI.RESTAPI.Entities;
import com.RESTAPI.RESTAPI.Enums.TypeUtilisateur;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
......@@ -12,6 +13,7 @@ import java.util.List;
@Entity
@Getter
@Setter
@NoArgsConstructor
public class User {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
......
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