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

Error handling

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