Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.mif13.authServer.controllers;
import com.mif13.authServer.dao.UsersDao;
import com.mif13.authServer.model.User;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("users")
public class UserRestController {
private final UsersDao usersRepo;
@Autowired
public UserRestController(UsersDao usersRepo) {
this.usersRepo = usersRepo;
}
@GetMapping(value = "/{id}", produces = "application/json")
public ResponseEntity<User> getUser(@PathVariable String id) {
ResponseEntity<User> response;
Optional<User> optionalUser = usersRepo.get(id);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
response = new ResponseEntity<>(user, HttpStatus.OK);
}
else {
response = new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return response;
}
}